<HTML ><HEAD ><TITLE >Extended Setup</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="Basic Installation" HREF="installing.smarty.basic.html"><LINK REL="NEXT" TITLE="Smarty For Template Designers" HREF="smarty.for.designers.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="installing.smarty.basic.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="smarty.for.designers.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="installing.smarty.extended" ></A >Extended Setup</H1 ><P > This is a continuation of the <A HREF="installing.smarty.basic.html" >basic installation</A >, please read that first! </P ><P > 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 <TT CLASS="filename" >/php/includes/guestbook/</TT > and make a new file called <TT CLASS="filename" >setup.php</TT >. In our example environment, <TT CLASS="filename" >/php/includes</TT > is in our include_path. Be sure you set this up too, or use absolute file paths. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN203" ></A ><P ><B >Example 2-10. Editing /php/includes/guestbook/setup.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(</span><span style="color: #DD0000">'Smarty.class.php'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The setup.php file is a good place to load<br />// required application library files, and you<br />// can do that right here. An example:<br />// require('guestbook/guestbook.lib.php');<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Smarty_GuestBook </span><span style="color: #007700">extends </span><span style="color: #0000BB">Smarty </span><span style="color: #007700">{<br /><br /> function </span><span style="color: #0000BB">Smarty_GuestBook</span><span style="color: #007700">()<br /> {<br /><br /> </span><span style="color: #FF8000">// Class Constructor.<br /> // These automatically get set with each new instance.<br /><br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">Smarty</span><span style="color: #007700">();<br /><br /> </span><span style="color: #0000BB">$this</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">$this</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">$this</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">$this</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">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">caching </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'app_name'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Guest Book'</span><span style="color: #007700">);<br /> }<br /><br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > Now lets alter the <TT CLASS="filename" >index.php</TT > file to use <TT CLASS="filename" >setup.php</TT >: </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN209" ></A ><P ><B >Example 2-11. 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: #007700">require(</span><span style="color: #DD0000">'guestbook/setup.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_GuestBook</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 ><P > Now you see it is quite simple to bring up an instance of Smarty, just use <TT CLASS="literal" >Smarty_GuestBook()</TT > which automatically initializes everything for our application. </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="installing.smarty.basic.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="smarty.for.designers.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Basic 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" >Smarty For Template Designers</TD ></TR ></TABLE ></DIV ></BODY ></HTML >