<HTML ><HEAD ><TITLE >Basic Installation</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Installation" HREF="installation.html"><LINK REL="PREVIOUS" TITLE="Installation" HREF="installation.html"><LINK REL="NEXT" TITLE="Extended Setup" HREF="installing.smarty.extended.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="installation.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 2. Installation</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="installing.smarty.extended.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="installing.smarty.basic" ></A >Basic Installation</H1 ><P > Install the Smarty library files which are in the <TT CLASS="filename" >/libs/</TT > sub directory of the distribution. These are PHP files that you SHOULD NOT edit. They are shared among all applications and they only get updated when you upgrade to a new version of Smarty. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN82" ></A ><P ><B >Example 2-1. Required Smarty library files</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >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)</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Smarty uses a PHP <A HREF="http://php.net/define" TARGET="_top" >constant</A > named <A HREF="smarty.constants.html#constant.smarty.dir" ><TT CLASS="constant" >SMARTY_DIR</TT ></A > which is the <SPAN CLASS="bold" ><B CLASS="emphasis" >full system file path</B ></SPAN > to the Smarty <TT CLASS="filename" >libs/</TT > directory. Basically, if your application can find the <TT CLASS="filename" >Smarty.class.php</TT > file, you do not need to set <A HREF="smarty.constants.html#constant.smarty.dir" ><TT CLASS="constant" >SMARTY_DIR</TT ></A > as Smarty will figure it out on its own. Therefore, if <TT CLASS="filename" >Smarty.class.php</TT > is not in your include_path, or you do not supply an absolute path to it in your application, then you must define <TT CLASS="constant" >SMARTY_DIR</TT > manually. <TT CLASS="constant" >SMARTY_DIR</TT > <SPAN CLASS="bold" ><B CLASS="emphasis" >must include a trailing slash/</B ></SPAN >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN98" ></A ><P ><B >Example 2-2. Create an instance of Smarty</B ></P ><P > Here's how you create an instance of Smarty in your PHP scripts: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// NOTE: Smarty has a capital 'S'<br /></span><span style="color: #007700">require_once(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Try running the above script. If you get an error saying the <TT CLASS="filename" >Smarty.class.php</TT > file could not be found, you have to do one of the following: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN104" ></A ><P ><B >Example 2-3. Set SMARTY_DIR constant manually</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// *nix style (note capital 'S')<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'SMARTY_DIR'</span><span style="color: #007700">, </span><span style="color: #DD0000">'/usr/local/lib/php/Smarty-v.e.r/libs/'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// windows style<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'SMARTY_DIR'</span><span style="color: #007700">, </span><span style="color: #DD0000">'c:/webroot/libs/Smarty-v.e.r/libs/'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// hack version example that works on both *nix and windows<br />// Smarty is assumend to be in 'includes/' dir under current script<br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(</span><span style="color: #DD0000">'SMARTY_DIR'</span><span style="color: #007700">,</span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"\\"</span><span style="color: #007700">,</span><span style="color: #DD0000">"/"</span><span style="color: #007700">,</span><span style="color: #0000BB">getcwd</span><span style="color: #007700">()).</span><span style="color: #DD0000">'/includes/Smarty-v.e.r/libs/'</span><span style="color: #007700">);<br /><br />require_once(</span><span style="color: #0000BB">SMARTY_DIR </span><span style="color: #007700">. </span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN107" ></A ><P ><B >Example 2-4. Supply absolute path to library file</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// *nix style (note capital 'S')<br /></span><span style="color: #007700">require_once(</span><span style="color: #DD0000">'/usr/local/lib/php/Smarty-v.e.r/libs/Smarty.class.php'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// windows style<br /></span><span style="color: #007700">require_once(</span><span style="color: #DD0000">'c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN110" ></A ><P ><B >Example 2-5. Add library directory to PHP include_path</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Edit your php.ini file, add the Smarty library<br />// directory to the include_path and restart web server.<br />// then the following should work:<br /></span><span style="color: #007700">require_once(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Now that the library files are in place, it's time to setup the Smarty directories for your application.</P ><P > Smarty requires four directories which are by default named <TT CLASS="filename" >'templates/'</TT >, <TT CLASS="filename" >'templates_c/'</TT >, <TT CLASS="filename" >'configs/'</TT > and <TT CLASS="filename" >'cache/'</TT >. </P ><P >Each of these are definable by the Smarty class properties <A HREF="api.variables.html#variable.template.dir" > <TT CLASS="varname" >$template_dir</TT ></A >, <A HREF="variable.compile.dir.html" > <TT CLASS="varname" >$compile_dir</TT ></A >, <A HREF="variable.config.dir.html" > <TT CLASS="varname" >$config_dir</TT ></A >, and <A HREF="variable.cache.dir.html" > <TT CLASS="varname" >$cache_dir</TT ></A > respectively. It is highly recommended that you setup a separate set of these directories for each application that will use Smarty. </P ><P > Be sure you know the location of your web server document root. In our examples, the document root is <TT CLASS="filename" >/web/www.example.com/docs/</TT >. The Smarty 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 <SPAN CLASS="emphasis" ><I CLASS="emphasis" >outside</I ></SPAN > of the document root. </P ><P > 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 <SPAN CLASS="QUOTE" >"guestbook"</SPAN > with the name of your app. We'll place our Smarty directories under <TT CLASS="filename" >/web/www.example.com/smarty/guestbook/</TT >. </P ><P > You will need as least one file under your document root, and that is the script accessed by the web browser. We will call our script <TT CLASS="filename" >index.php</TT >, and place it in a subdirectory under the document root called <TT CLASS="filename" >/guestbook/</TT >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Technical Note: </B > It is convenient to setup the web server so that <TT CLASS="filename" >index.php</TT > can be identified as the default directory index, so if you access <TT CLASS="literal" >http://www.example.com/guestbook/</TT >, the <TT CLASS="filename" >index.php</TT > script will be executed without adding <TT CLASS="filename" >index.php</TT > to the URL. In Apache you can set this up by adding <TT CLASS="literal" >index.php</TT > onto the end of your <TT CLASS="literal" >DirectoryIndex</TT > setting (separate each entry with a space) as in the following <TT CLASS="filename" >httpd.conf</TT > example. <DIV CLASS="informalexample" ><A NAME="AEN147" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >DirectoryIndex index.htm index.html index.cgi index.php</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></BLOCKQUOTE ></DIV ><P > Lets take a look at the file structure so far: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN150" ></A ><P ><B >Example 2-6. File structure so far</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >/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 /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/ /web/www.example.com/docs/guestbook/index.php</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Smarty will need <SPAN CLASS="bold" ><B CLASS="emphasis" >write access</B ></SPAN > (windows users please ignore) to the <A HREF="variable.compile.dir.html" > <TT CLASS="parameter" ><I >$compile_dir</I ></TT ></A > and <A HREF="variable.cache.dir.html" > <TT CLASS="parameter" ><I >$cache_dir</I ></TT ></A >, so be sure the web server user can write 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 <TT CLASS="filename" >httpd.conf</TT > file to see what user and group are being used. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN160" ></A ><P ><B >Example 2-7. Setting file permissions</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="shell" >chown nobody:nobody /web/www.example.com/smarty/guestbook/templates_c/ chmod 770 /web/www.example.com/smarty/guestbook/templates_c/ chown nobody:nobody /web/www.example.com/smarty/guestbook/cache/ chmod 770 /web/www.example.com/smarty/guestbook/cache/</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > 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. </P ></BLOCKQUOTE ></DIV ><P > We need to create the <TT CLASS="filename" >index.tpl</TT > file that Smarty will load. This needs to be located in the <A HREF="api.variables.html#variable.template.dir" > <TT CLASS="parameter" ><I >$template_dir</I ></TT ></A >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN170" ></A ><P ><B >Example 2-8. Our /web/www.example.com/smarty/guestbook/templates/index.tpl</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >{* Smarty *} Hello {$name}, welcome to Smarty!</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Technical Note: </B > <TT CLASS="literal" >{* Smarty *}</TT > is a template <A HREF="language.basic.syntax.html#language.syntax.comments" >comment</A >. It is not required, but it is good 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. </P ></BLOCKQUOTE ></DIV ><P > Now lets edit <TT CLASS="filename" >index.php</TT >. We'll create an instance of Smarty, <A HREF="api.assign.html" ><TT CLASS="varname" >assign()</TT ></A > a template variable and <A HREF="api.display.html" ><TT CLASS="varname" >display()</TT ></A > the <TT CLASS="filename" >index.tpl</TT > file. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN185" ></A ><P ><B >Example 2-9. Editing /web/www.example.com/docs/guestbook/index.php</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">// load Smarty library<br /></span><span style="color: #007700">require_once(</span><span style="color: #0000BB">SMARTY_DIR </span><span style="color: #007700">. </span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$smarty </span><span style="color: #007700">= new </span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">template_dir </span><span style="color: #007700">= </span><span style="color: #DD0000">'/web/www.example.com/smarty/guestbook/templates/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">compile_dir </span><span style="color: #007700">= </span><span style="color: #DD0000">'/web/www.example.com/smarty/guestbook/templates_c/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">config_dir </span><span style="color: #007700">= </span><span style="color: #DD0000">'/web/www.example.com/smarty/guestbook/configs/'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">cache_dir </span><span style="color: #007700">= </span><span style="color: #DD0000">'/web/www.example.com/smarty/guestbook/cache/'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'name'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Ned'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">display</span><span style="color: #007700">(</span><span style="color: #DD0000">'index.tpl'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Technical Note: </B > In our example, we are setting absolute paths to all of the Smarty directories. If <TT CLASS="filename" >/web/www.example.com/smarty/guestbook/</TT > is within your PHP include_path, then these settings are not necessary. However, it is more efficient and (from experience) less error-prone to set them to absolute paths. This ensures that Smarty is getting files from the directories you intended. </P ></BLOCKQUOTE ></DIV ><P > Now naviagate to the <TT CLASS="filename" >index.php</TT > file with the web browser. You should see "Hello Ned, welcome to Smarty!" </P ><P > You have completed the basic setup for Smarty! </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="installation.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="installing.smarty.extended.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Installation</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="installation.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Extended Setup</TD ></TR ></TABLE ></DIV ></BODY ></HTML >