Files
smarty/docs/en/getting-started.xml

587 lines
19 KiB
XML
Raw Normal View History

2004-03-24 15:16:30 +00:00
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision$ -->
2004-03-18 17:16:23 +00:00
<part id="getting.started">
<title>Getting Started</title>
<chapter id="what.is.smarty">
<title>What is Smarty?</title>
<para>
Smarty is a template engine for PHP. More specifically, it facilitates a
manageable way to separate application logic and content from its
presentation. This is best described in a situation where the application
2005-05-27 16:25:02 +00:00
programmer and the template designer play different roles, or in most
cases are not the same person.
</para>
<para>
For example, let's say you are creating a web page that is displaying a
2005-05-27 16:25:02 +00:00
newspaper article. The article headline, tagline, author and body are
content elements, they contain no information about how they will be
presented. They are passed into Smarty by the application, then the
template designer edits the templates and uses a combination of
2004-03-18 17:16:23 +00:00
HTML tags and template tags to format the presentation of these elements
(HTML tables, background colors, font sizes, style sheets, etc.) One day
the programmer needs to change the way the article content is retrieved (a
change in application logic.) This change does not affect the template
designer, the content will still arrive in the template exactly the same.
Likewise, if the template designer wants to completely redesign the
templates, this requires no changes to the application logic. Therefore,
the programmer can make changes to the application logic without the need
to restructure templates, and the template designer can make changes to
templates without breaking application logic.
</para>
<para>
One design goal of Smarty is the separation of business logic and
presentation logic. This means templates can certainly contain logic under
the condition that it is for presentation only. Things such as including
other templates, altering table row colors, upper-casing a variable,
looping over an array of data and displaying it, etc. are all examples of
presentation logic. This does not mean that Smarty forces a separation of
business and presentation logic. Smarty has no knowledge of which is which,
so placing business logic in the template is your own doing. Also, if you
2005-05-27 16:25:02 +00:00
desire <emphasis>no</emphasis> logic in your templates you certainly can
do so by boiling the content down to text and variables only.
2004-03-18 17:16:23 +00:00
</para>
<para>
2005-05-27 16:25:02 +00:00
One of the unique aspects about Smarty is the template compiling. This
means Smarty reads the template files and creates PHP scripts from them.
Once they are created, they are executed from then on. Therefore there is
no costly template file parsing for each request, and each template can
take full advantage of PHP compiler cache solutions such as Zend
Accelerator (<ulink url="&url.zend;">&url.zend;</ulink>) or PHP Accelerator
(<ulink url="&url.php-accelerator;">&url.php-accelerator;</ulink>).
2004-03-18 17:16:23 +00:00
</para>
<para>
Some of Smarty's features:
</para>
<itemizedlist>
<listitem>
<para>
It is extremely fast.
</para>
</listitem>
<listitem>
<para>
It is efficient since the PHP parser does the dirty work.
</para>
</listitem>
<listitem>
<para>
No template parsing overhead, only compiles once.
</para>
</listitem>
<listitem>
<para>
It is smart about recompiling only the template files that have changed.
</para>
</listitem>
<listitem>
<para>
2005-05-27 16:25:02 +00:00
You can make <link
linkend="language.custom.functions">custom functions</link>
2005-05-27 16:25:02 +00:00
and custom <link
linkend="language.modifiers">variable modifiers</link>, so the
2004-03-18 17:16:23 +00:00
template language is extremely extensible.
</para>
</listitem>
<listitem>
<para>
Configurable template
<link linkend="variable.left.delimiter">delimiter tag</link>
syntax, so you can use
2004-03-18 17:16:23 +00:00
{}, {{}}, &lt;!--{}--&gt;, etc.
</para>
</listitem>
<listitem>
<para>
The <link
linkend="language.function.if">if/elseif/else/endif</link>
constructs are passed to the
2005-05-27 16:25:02 +00:00
PHP parser, so the {if ...} expression syntax can be as simple or as
complex as you like.
2004-03-18 17:16:23 +00:00
</para>
</listitem>
<listitem>
<para>
Unlimited nesting of
<link linkend="language.function.section">sections</link>,
ifs, etc. allowed.
2004-03-18 17:16:23 +00:00
</para>
</listitem>
<listitem>
<para>
It is possible to
<link linkend="language.function.php">embed PHP code</link>
right in your template files, although
2005-05-27 16:25:02 +00:00
this may not be needed (nor recommended) since the engine is so
customizable.
2004-03-18 17:16:23 +00:00
</para>
</listitem>
<listitem>
<para>
Built-in <link linkend="caching">caching</link> support
2004-03-18 17:16:23 +00:00
</para>
</listitem>
<listitem>
<para>
Arbitrary <link
linkend="template.resources">template</link> sources
2004-03-18 17:16:23 +00:00
</para>
</listitem>
<listitem>
<para>
Custom <link
linkend="section.template.cache.handler.func">cache handling</link>
functions
2004-03-18 17:16:23 +00:00
</para>
</listitem>
<listitem>
<para>
2005-05-27 16:25:02 +00:00
<link linkend="plugins">Plugin</link> architecture
2004-03-18 17:16:23 +00:00
</para>
</listitem>
</itemizedlist>
</chapter>
<chapter id="installation">
<title>Installation</title>
<sect1 id="installation.requirements">
<title>Requirements</title>
<para>
Smarty requires a web server running PHP 4.0.6 or later.
</para>
</sect1>
<sect1 id="installing.smarty.basic">
<title>Basic Installation</title>
<para>
2005-09-14 19:42:23 +00:00
Install the Smarty library files which are in the /libs/ sub directory of
the distribution. These are PHP files that you SHOULD NOT edit. They
2004-03-18 17:16:23 +00:00
are shared among all applications and they only get updated when you
upgrade to a new version of Smarty.
</para>
<example>
2005-05-27 16:25:02 +00:00
<title>Required Smarty library files</title>
2004-03-18 17:16:23 +00:00
<screen>
<![CDATA[
Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
/internals/*.php (all of them)
/plugins/*.php (all of them to be safe, maybe your site only needs a subset)
2004-03-18 17:16:23 +00:00
]]>
</screen>
</example>
<para>
2005-10-24 18:17:39 +00:00
Smarty uses a PHP <ulink url="&url.php-manual;define">constant</ulink> named
2005-09-14 19:42:23 +00:00
<link linkend="constant.smarty.dir">SMARTY_DIR</link> which is the
<emphasis role="bold">full system file path</emphasis> to the Smarty 'libs/' directory.
Basically, if your application
2005-05-27 16:25:02 +00:00
can find the <filename>Smarty.class.php</filename> file, you do not need
2005-09-14 19:42:23 +00:00
to set the <link linkend="constant.smarty.dir">SMARTY_DIR</link>,
2005-05-27 16:25:02 +00:00
Smarty will figure it out on its own. Therefore, if
<filename>Smarty.class.php</filename> is not in your include_path, or you
2004-03-18 17:16:23 +00:00
do not supply an absolute path to it in your application, then you must
2005-09-14 19:42:23 +00:00
define SMARTY_DIR manually. SMARTY_DIR <emphasis role="bold">must include a
trailing slash</emphasis>.
2004-03-18 17:16:23 +00:00
</para>
<para>
2005-09-14 19:42:23 +00:00
Here's how you create an instance of Smarty in your PHP scripts:
2004-03-18 17:16:23 +00:00
</para>
2004-09-12 22:44:08 +00:00
2004-03-18 17:16:23 +00:00
<example>
<title>Create Smarty instance of Smarty</title>
<programlisting role="php">
<![CDATA[
<?php
2005-09-14 19:42:23 +00:00
// NOTE: Smarty has a capital 'S'
require_once('Smarty.class.php');
$smarty = new Smarty();
2004-03-18 17:16:23 +00:00
?>
]]>
</programlisting>
</example>
<para>
Try running the above script. If you get an error saying the
<filename>Smarty.class.php</filename> file could not be found, you have to
2004-03-18 17:16:23 +00:00
do one of the following:
</para>
<example>
2005-09-14 19:42:23 +00:00
<title>Set SMARTY_DIR constant manually</title>
2004-03-18 17:16:23 +00:00
<programlisting role="php">
<![CDATA[
<?php
2005-09-14 19:42:23 +00:00
// *nix style (note capital 'S')
define('SMARTY_DIR', '/usr/local/lib/php/Smarty-v.e.r/libs/');
// windows style
define('SMARTY_DIR', 'c:/webroot/libs/Smarty-v.e.r/libs/');
// hack version example that works on both *nix and windows
// Smarty is assumend to be in 'includes/' dir under current script
2005-09-14 19:52:50 +00:00
define('SMARTY_DIR',str_replace("\\","/",getcwd()).'/includes/Smarty-v.e.r/libs/');
2005-09-14 19:42:23 +00:00
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
2004-03-18 17:16:23 +00:00
?>
]]>
</programlisting>
</example>
2005-09-14 19:42:23 +00:00
2004-03-18 17:16:23 +00:00
<example>
2005-09-14 19:42:23 +00:00
<title>Supply absolute path to library file</title>
2004-03-18 17:16:23 +00:00
<programlisting role="php">
<![CDATA[
<?php
2005-09-14 19:42:23 +00:00
// *nix style (note capital 'S')
require_once('/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php');
// windows style
require_once('c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php');
$smarty = new Smarty();
2004-03-18 17:16:23 +00:00
?>
]]>
</programlisting>
</example>
<example>
2005-09-14 19:42:23 +00:00
<title>Add library directory to PHP include_path</title>
2004-03-18 17:16:23 +00:00
<programlisting role="php">
<![CDATA[
<?php
2005-09-14 19:42:23 +00:00
// Edit your php.ini file, add the Smarty library
// directory to the include_path and restart web server.
// then the following should work:
require_once('Smarty.class.php');
$smarty = new Smarty();
2004-03-18 17:16:23 +00:00
?>
]]>
</programlisting>
</example>
2005-09-14 19:42:23 +00:00
2004-03-18 17:16:23 +00:00
<para>
Now that the library files are in place, it's time to setup the Smarty
2005-05-27 16:25:02 +00:00
directories for your application.</para>
<para>
Smarty requires four directories which
2005-09-14 19:42:23 +00:00
are by default named <filename class="directory">'templates/'</filename>,
2005-05-27 16:25:02 +00:00
<filename class="directory">'templates_c/'</filename>, <filename
class="directory">'configs/'</filename> and <filename
class="directory">'cache/'</filename>.
</para>
<para>Each of these are definable by the
Smarty class properties
<link linkend="variable.template.dir">
<varname>$template_dir</varname></link>,
<link linkend="variable.compile.dir">
<varname>$compile_dir</varname></link>,
<link linkend="variable.config.dir">
<varname>$config_dir</varname></link>, and
<link linkend="variable.cache.dir">
<varname>$cache_dir</varname></link> respectively.
It is highly recommended
2004-03-18 17:16:23 +00:00
that you setup a separate set of these directories for each application
that will use Smarty.
</para>
<para>
Be sure you know the location of your web server document root. In our
2005-05-27 16:25:02 +00:00
example, the document root is <filename
class="directory">/web/www.example.com/docs/</filename>. The Smarty
2004-03-18 17:16:23 +00:00
directories are only accessed by the Smarty library and never accessed
directly by the web browser. Therefore to avoid any security concerns, it
is recommended to place these directories <emphasis>outside</emphasis> of
the document root.
</para>
<para>
For our installation example, we will be setting up the Smarty environment
for a guest book application. We picked an application only for the purpose
of a directory naming convention. You can use the same environment for any
application, just replace "guestbook" with the name of your app. We'll
place our Smarty directories under
<filename
class="directory">/web/www.example.com/smarty/guestbook/</filename>.
2004-03-18 17:16:23 +00:00
</para>
<para>
You will need as least one file under your document root, and that is the
2005-05-27 16:25:02 +00:00
script accessed by the web browser. We will call our script
<emphasis>'index.php'</emphasis>, and place it in a subdirectory under the
document root called <filename class="directory">/guestbook/</filename>.
2004-03-18 17:16:23 +00:00
</para>
2004-09-12 22:44:08 +00:00
2004-03-18 17:16:23 +00:00
<note>
<title>Technical Note</title>
<para>
2005-05-27 16:25:02 +00:00
It is convenient to setup the web server so that 'index.php' can be
2004-03-18 17:16:23 +00:00
identified as the default directory index, so if you access
2005-05-27 16:25:02 +00:00
http://www.example.com/guestbook/, the 'index.php' script will be executed
without adding 'index.php' to the URL. In Apache you can set this up by adding
"index.php" onto the end of your <emphasis>DirectoryIndex</emphasis> setting (separate
each entry with a space.) as in the httpd.conf example
</para>
<para>
<emphasis>DirectoryIndex
index.htm index.html index.php index.php3 default.html index.cgi
</emphasis>
2004-03-18 17:16:23 +00:00
</para>
</note>
2004-09-12 22:44:08 +00:00
2004-03-18 17:16:23 +00:00
<para>
Lets take a look at the file structure so far:
</para>
<example>
<title>Example file structure</title>
<screen>
<![CDATA[
2005-09-14 19:42:23 +00:00
/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php
/usr/local/lib/php/Smarty-v.e.r/libs/Smarty_Compiler.class.php
/usr/local/lib/php/Smarty-v.e.r/libs/Config_File.class.php
/usr/local/lib/php/Smarty-v.e.r/libs/debug.tpl
/usr/local/lib/php/Smarty-v.e.r/libs/internals/*.php
/usr/local/lib/php/Smarty-v.e.r/libs/plugins/*.php
2004-03-18 17:16:23 +00:00
/web/www.example.com/smarty/guestbook/templates/
/web/www.example.com/smarty/guestbook/templates_c/
/web/www.example.com/smarty/guestbook/configs/
/web/www.example.com/smarty/guestbook/cache/
2004-03-18 17:16:23 +00:00
/web/www.example.com/docs/guestbook/index.php
2004-03-18 17:16:23 +00:00
]]>
</screen>
</example>
<para>
2005-05-27 16:25:02 +00:00
Smarty will need <emphasis role="bold">write access</emphasis>
(windows users please ignore) to the
<link linkend="variable.compile.dir">
<emphasis>$compile_dir</emphasis></link> and
<link linkend="variable.cache.dir">
<emphasis>$cache_dir</emphasis></link>,
so be sure the web server user can write
2004-03-18 17:16:23 +00:00
to them. This is usually user "nobody" and group "nobody". For OS X users,
the default is user "www" and group "www". If you are using Apache, you can
look in your httpd.conf file (usually in "/usr/local/apache/conf/") to see
what user and group are being used.
</para>
<example>
<title>Setting file permissions</title>
<programlisting role="shell">
<![CDATA[
chown nobody:nobody /web/www.example.com/smarty/guestbook/templates_c/
chmod 770 /web/www.example.com/smarty/guestbook/templates_c/
2004-03-18 17:16:23 +00:00
chown nobody:nobody /web/www.example.com/smarty/guestbook/cache/
chmod 770 /web/www.example.com/smarty/guestbook/cache/
2004-03-18 17:16:23 +00:00
]]>
</programlisting>
</example>
<note>
<title>Technical Note</title>
<para>
chmod 770 will be fairly tight security, it only allows user "nobody" and
group "nobody" read/write access to the directories. If you would like to
open up read access to anyone (mostly for your own convenience of viewing
these files), you can use 775 instead.
</para>
</note>
<para>
2005-05-27 16:25:02 +00:00
We need to create the 'index.tpl' file that Smarty will load. This will be
2005-09-14 19:42:23 +00:00
located in the <link linkend="variable.template.dir">$template_dir</link>.
2004-03-18 17:16:23 +00:00
</para>
<example>
<title>Editing /web/www.example.com/smarty/guestbook/templates/index.tpl</title>
2004-03-18 17:16:23 +00:00
<screen>
<![CDATA[
{* Smarty *}
2005-09-14 19:52:50 +00:00
Hello {$name}, welcome to Smarty!
2004-03-18 17:16:23 +00:00
]]>
</screen>
</example>
<note>
<title>Technical Note</title>
<para>
2005-05-27 16:25:02 +00:00
{* Smarty *} is a template
<link linkend="language.syntax.comments">comment</link>.
It is not required, but it is good
2004-03-18 17:16:23 +00:00
practice to start all your template files with this comment. It makes
the file easy to recognize regardless of the file extension. For
example, text editors could recognize the file and turn on special
syntax highlighting.
</para>
</note>
<para>
2005-09-14 19:42:23 +00:00
Now lets edit 'index.php'. We'll create an instance of Smarty,
<link linkend="api.assign">assign</link> a
template variable and <link linkend="api.display">display</link>
the 'index.tpl' file.
2004-03-18 17:16:23 +00:00
</para>
<example>
<title>Editing /web/www.example.com/docs/guestbook/index.php</title>
2004-03-18 17:16:23 +00:00
<programlisting role="php">
<![CDATA[
<?php
// load Smarty library
2005-09-14 19:42:23 +00:00
require_once(SMARTY_DIR . 'Smarty.class.php');
2004-03-18 17:16:23 +00:00
2005-09-14 19:42:23 +00:00
$smarty = new Smarty();
2004-03-18 17:16:23 +00:00
$smarty->template_dir = '/web/www.example.com/smarty/guestbook/templates/';
$smarty->compile_dir = '/web/www.example.com/smarty/guestbook/templates_c/';
$smarty->config_dir = '/web/www.example.com/smarty/guestbook/configs/';
$smarty->cache_dir = '/web/www.example.com/smarty/guestbook/cache/';
2004-03-18 17:16:23 +00:00
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
</example>
<note>
<title>Technical Note</title>
<para>
In our example, we are setting absolute paths to all of the Smarty
2005-05-27 16:25:02 +00:00
directories. If <filename
class="directory">/web/www.example.com/smarty/guestbook/</filename> is
2005-05-27 16:25:02 +00:00
within your PHP include_path, then these settings are not necessary.
However, it is more efficient and (from experience) less error-prone to
2005-05-27 16:25:02 +00:00
set them to absolute paths. This ensures that Smarty is getting files
from the directories you intended.
2004-03-18 17:16:23 +00:00
</para>
</note>
<para>
2005-09-14 19:42:23 +00:00
Now naviagate to the <filename>index.php</filename> file with the web browser.
2005-09-14 19:52:50 +00:00
You should see "Hello Ned, welcome to Smarty!"
2004-03-18 17:16:23 +00:00
</para>
<para>
You have completed the basic setup for Smarty!
</para>
</sect1>
<sect1 id="installing.smarty.extended">
<title>Extended Setup</title>
<para>
This is a continuation of the <link
linkend="installing.smarty.basic">basic installation</link>, please read
that first!
</para>
<para>
A slightly more flexible way to setup Smarty is to extend the class and
initialize your Smarty environment. So instead of repeatedly setting
directory paths, assigning the same vars, etc., we can do that in one place.
Lets create a new directory "/php/includes/guestbook/" and make a new file
2005-05-27 16:25:02 +00:00
called <filename>setup.php</filename>. In our example environment,
"/php/includes" is in our include_path. Be sure you set this up too, or
use absolute file paths.
2004-03-18 17:16:23 +00:00
</para>
<example>
<title>Editing /php/includes/guestbook/setup.php</title>
<programlisting role="php">
<![CDATA[
<?php
// load Smarty library
require('Smarty.class.php');
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');
class Smarty_GuestBook extends Smarty {
2004-09-12 22:44:08 +00:00
function Smarty_GuestBook()
2004-03-29 01:54:07 +00:00
{
2004-09-12 22:44:08 +00:00
2005-05-27 16:25:02 +00:00
// Class Constructor.
// These automatically get set with each new instance.
2004-03-18 17:16:23 +00:00
$this->Smarty();
$this->template_dir = '/web/www.example.com/smarty/guestbook/templates/';
$this->compile_dir = '/web/www.example.com/smarty/guestbook/templates_c/';
$this->config_dir = '/web/www.example.com/smarty/guestbook/configs/';
$this->cache_dir = '/web/www.example.com/smarty/guestbook/cache/';
2004-09-12 22:44:08 +00:00
2004-03-18 17:16:23 +00:00
$this->caching = true;
$this->assign('app_name', 'Guest Book');
2004-03-18 17:16:23 +00:00
}
}
?>
]]>
</programlisting>
</example>
<para>
Now lets alter the index.php file to use setup.php:
</para>
2004-09-12 22:44:08 +00:00
2004-03-18 17:16:23 +00:00
<example>
<title>Editing /web/www.example.com/docs/guestbook/index.php</title>
2004-03-18 17:16:23 +00:00
<programlisting role="php">
<![CDATA[
<?php
require('guestbook/setup.php');
$smarty = new Smarty_GuestBook;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
?>
]]>
</programlisting>
</example>
<para>
Now you see it is quite simple to bring up an instance of Smarty, just use
2005-05-27 16:25:02 +00:00
Smarty_GuestBook which automatically initializes everything for our
application.
2004-03-18 17:16:23 +00:00
</para>
2004-09-12 22:44:08 +00:00
2004-03-18 17:16:23 +00:00
</sect1>
</chapter>
</part>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->