<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mukesh Chapagain&#039;s Blog &#187; Smarty</title>
	<atom:link href="http://blog.chapagain.com.np/category/smarty/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.chapagain.com.np</link>
	<description>PHP Magento jQuery Wordpress Javascript programming n tutorial</description>
	<lastBuildDate>Tue, 07 Sep 2010 06:18:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>File Upload in PEAR and Smarty</title>
		<link>http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/</link>
		<comments>http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 10:07:18 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[PEAR]]></category>
		<category><![CDATA[Smarty]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[http]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=73</guid>
		<description><![CDATA[For uploading files, you need to install a package of PEAR called ‘HTTP_Upload’ along with the installation of PEAR and Smarty.


HTTP_Upload is used for easy and secure managment of files submitted via HTML forms. This package provides an advanced system for managing uploads of files via HTML
 fields. Features include:

1) Handling of multiple file uploads at a time
2) Safe file copying/moving from temporary directories
3) Upload validaton mechanisms
4) Extensive information about uploaded files
5) Renaming uploaded files
6) Internationalized error messages

Two class files are included in index.php. One is ConnectSmarty.class.php which is used ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/using-database-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Using database in PEAR and Smarty'>Using database in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/file-upload-in-php-simplified/' rel='bookmark' title='Permanent Link: File Upload in PHP :: Simplified'>File Upload in PHP :: Simplified</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For uploading files, you need to install a package of PEAR called ‘HTTP_Upload’ along with the installation of PEAR and <a href="http://blog.chapagain.com.np/index.php/2008/07/11/installing-the-smarty-template-engine/"title="Installing Smarty"  target="_blank">Smarty</a>.</p>
<p class="MsoNormal" style="text-align: justify;">
<p><span id="more-73"></span></p>
<p class="MsoNormal" style="text-align: justify;">HTTP_Upload is used for easy and secure managment of files submitted via <acronym>HTML</acronym> forms. This package provides an advanced system for managing uploads of files via HTML<br />
<input type="file" /> fields. Features include:</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span>1)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "> </span></span><!--[endif]-->Handling of multiple file uploads at a time</p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span>2)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "> </span></span><!--[endif]-->Safe file copying/moving from temporary directories</p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span>3)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "> </span></span><!--[endif]-->Upload validaton mechanisms</p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span>4)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "> </span></span><!--[endif]-->Extensive information about uploaded files</p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span>5)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "> </span></span><!--[endif]-->Renaming uploaded files</p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span>6)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "> </span></span><!--[endif]-->Internationalized error messages</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">Two class files are included in index.php. One is ConnectSmarty.class.php which is used for connecting with Smarty. I have explained about it in my <a href="http://blog.chapagain.com.np/index.php/2008/07/11/installing-the-smarty-template-engine/"title="Installing Smarty"  target="_blank">previous article</a>. The other is files.class.php.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">files.class.php</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">&lt;?php
require_once &quot;HTTP/Upload.php&quot;;&lt;/p&gt;
class Files
{
function upload($field_name,$dest)
{
$upload =&amp; new HTTP_Upload(&quot;en&quot;);
$file = $upload-&gt;getFiles($field_name);&lt;/p&gt;
if ($file-&gt;isValid())
{
$moved = $file-&gt;moveTo(&quot;$dest/&quot;);
if (PEAR::isError($moved))
{
require_once('ConnectSmarty.class.php');
// create an object of the class included above
$smarty = new ConnectSmarty;&lt;/p&gt;
$error =&amp; $moved-&gt;getMessage();&lt;/p&gt;
// assign error message
$smarty-&gt;assign('errmessage',$error);

// display error message and exit
$smarty-&gt;display('error.tpl');
exit();
}
}
}
}
?&gt;</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">This class file includes the upload class which becomes available after installing the HTTP_Upload package. I have created a function named ‘upload’ inside the class ‘Files’. The <em>upload()</em> function requires two parameters: <em>$field_name</em> and <em>$dest</em>.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>$field_name</em> is the name of input file.</p>
<p class="MsoNormal" style="text-align: justify;"><em>$dest</em> is the name of the destination directory where the file is to be uploaded.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">The function <em>getFiles() </em>checks if a valid file was uploaded through the form. If the file is valid then it is moved to the destination directory.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">If there is error while moving the file to the destination directory then the error message is fetched through the <em>getMessage()</em> function and displayed in a separate template file called error.tpl.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">index.php</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">&lt;?php
require_once('classes/ConnectSmarty.class.php');
require_once ('classes/files.class.php');&lt;/p&gt;
// create an object of the class included above
$smarty = new ConnectSmarty;&lt;/p&gt;
if(isset($_POST['submit']))
{
$post = $_POST;
$keys = &quot;&quot;;&lt;/p&gt;
$type = $_FILES['logo']['type'];&lt;/p&gt;
// Only allowing jpeg and gif image to be uploaded
if($type == &quot;image/jpeg&quot; || $type == &quot;image/gif&quot;)
{
/**
* file upload part
* creating an object of the class Files
* calling the upload function of the class Files
* the destination directory is to be created at first
* in this case, the destination directory is created and named 'uploads'
*/
$file =&amp; new Files;
$fieldName = &quot;logo&quot;;
$dest = &quot;uploads&quot;;
$file-&gt;upload($fieldName,$dest);

// assign success message
$smarty-&gt;assign('success','File Uploaded Successfully');
}
else
{
// assign error message
$smarty-&gt;assign('error','Only jpge and gif image are supported.');

// display error message and exit
$smarty-&gt;display('index.tpl');
exit();
}
}

// display the content
$smarty-&gt;display('index.tpl');

?&gt;</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">I have only allowed jpeg and gif image to be uploaded. If the file is successfully uploaded then a success message is assigned and displayed.</p>
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">// assign success message

$smarty-&gt;assign('success','File Uploaded Successfully');</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">If the file is not of jpeg or gif type then an error message is assigned and displayed.</p>
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">// assign error message

$smarty-&gt;assign('error','Only jpge and gif image are supported.');

// display error message and exit

$smarty-&gt;display('index.tpl');

exit();</pre>
<p>index.tpl</p>
<pre class="brush: xml;">&lt;html&gt;

&lt;head&gt;
&lt;title&gt; Homepage : File Upload&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;font color=&quot;green&quot;&gt;{$success}&lt;/font&gt;
&lt;font color=&quot;red&quot;&gt;{$error}&lt;/font&gt;

&lt;h2&gt;Upload Logo&lt;/h2&gt;
&lt;table&gt;
&lt;form action=&quot;index.php&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;tr&gt;&lt;td&gt;Logo&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;file&quot; name=&quot;logo&quot; &gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/form&gt;
&lt;/table&gt;

&lt;/body&gt;

&lt;/html&gt;</pre>
<p>error.tpl</p>
<pre class="brush: xml;">&lt;div align=&quot;center&quot;&gt;
&lt;font color=&quot;red&quot;&gt;
{$errmessage}
&lt;/font&gt;
&lt;/div&gt;</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong><span><a href="http://chapagain.googlecode.com/files/fileupload_pear_smarty.zip" rel="nofollow" title="File upload in PEAR and Smarty" >Download source code</a></span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip" rel="nofollow" >http://chapagain.googlecode.com/files/smarty.zip</a></p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/">File Upload in PEAR and Smarty</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=73&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/using-database-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Using database in PEAR and Smarty'>Using database in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/file-upload-in-php-simplified/' rel='bookmark' title='Permanent Link: File Upload in PHP :: Simplified'>File Upload in PHP :: Simplified</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pagination in PEAR and Smarty</title>
		<link>http://blog.chapagain.com.np/pagination-in-pear-and-smarty/</link>
		<comments>http://blog.chapagain.com.np/pagination-in-pear-and-smarty/#comments</comments>
		<pubDate>Sun, 20 Jul 2008 10:20:53 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[PEAR]]></category>
		<category><![CDATA[Smarty]]></category>
		<category><![CDATA[pager]]></category>
		<category><![CDATA[pagination]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=72</guid>
		<description><![CDATA[First of all, you need to install PEAR in your web server. Then you need to install Smarty. After that, you need to install a package in PEAR. It’s called ‘Pager’.


Pager is a class to page an array of data. It is taken as input and it is paged according to various parameters. Pager also builds links within a specified range, and allows complete customization of the output.

In the index.php file, we make a function named getPager(). This function contains four parameters &#8211; $item, $perPage, $mode, and $delta.
 
$item is ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/' rel='bookmark' title='Permanent Link: Creating selection list, using foreach and section loop in Smarty'>Creating selection list, using foreach and section loop in Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/using-database-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Using database in PEAR and Smarty'>Using database in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>First of all, you need to install PEAR in your web server. Then you need to <a href="http://blog.chapagain.com.np/index.php/2008/07/11/installing-the-smarty-template-engine/"title="Installing Smarty"  target="_blank"><span style="text-decoration: underline;">install Smarty</span></a>. After that, you need to install a package in PEAR. It’s called ‘Pager’.</p>
<p class="MsoNormal" style="text-align: justify;">
<p><span id="more-72"></span></p>
<p class="MsoNormal" style="text-align: justify;">Pager is a class to page an array of data. It is taken as input and it is paged according to various parameters. Pager also builds links within a specified range, and allows complete customization of the output.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">In the index.php file, we make a function named getPager(). This function contains four parameters &#8211; $item, $perPage, $mode, and $delta.</p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><em>$item</em> is the array of data.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>$perPage</em> is the number of data to be displayed per page.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>$mode</em> defines about the mode of pagination links. There are two types of modes – Sliding and Jumping. The Jumping mode displays ‘Back’ and ‘Next’ link.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>$delta</em> defines the width of pagination links. High value of $delta increases the width of pagination links.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">The getPager() function returns an array of data and links. The first element of the array contains array of data and the second element contains pagination links.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">index.php</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">&lt;?php
require_once('classes/ConnectSmarty.class.php');
require_once ('Pager/Pager.php');&lt;/p&gt;
// create an object of the class included above
$smarty = new ConnectSmarty;&lt;/p&gt;
function getPager($item,$perPage,$mode,$delta)
{
// setting Sliding as default mode
if($mode == &quot;&quot;)
{
$mode = 'Sliding';
}

// setting the default delta value as 2
// delta means the width of pagination
// more delta value results more width
if($delta == &quot;&quot;)
{
$delta = '2';
}

$params = array(
'mode'=&gt;$mode,
'perPage'=&gt;$perPage,
'delta'=&gt;$delta,
'itemData'=&gt;$item);
$pager =&amp; Pager::factory($params);
$data = $pager-&gt;getPageData();
$links = $pager-&gt;getLinks();
$links = $pager-&gt;links;
return array($data,$links);
}

$mode = 'Sliding'; // $mode = 'Jumping';
$perPage = '3';
$delta = '2';
$item = array(
array('country_id'=&gt;'1','country_name'=&gt;'India'),
array('country_id'=&gt;'2','country_name'=&gt;'Pakistan'),
array('country_id'=&gt;'3','country_name'=&gt;'Nepal'),
array('country_id'=&gt;'4','country_name'=&gt;'Srilanka'),
array('country_id'=&gt;'5','country_name'=&gt;'Bangladesh'),
array('country_id'=&gt;'6','country_name'=&gt;'Maldives'),
array('country_id'=&gt;'7','country_name'=&gt;'Bhutan'),
array('country_id'=&gt;'8','country_name'=&gt;'Afganistan'),
array('country_id'=&gt;'9','country_name'=&gt;'China')
);
$pagination = getPager($item,$perPage,$mode,$delta);
// print_r($pagination);

$smarty-&gt;assign('pager',$pagination[1]);
$smarty-&gt;assign('country',$pagination['0']);

// display the content
$smarty-&gt;display('index.tpl');

?&gt;</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>foreach</em> loop is used to display data. Before trying <em>foreach</em> loop, I had tried <em>section</em> loop but it didn’t work with pagination/pager. After that I tried the <em>foreach</em> loop and it worked fine and smooth. <span style="font-family: Wingdings;"><span>J</span></span></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">A custom Smarty function ‘cycle’ is used to alternate row color of the table on which data is being displayed. Cycle is used to cycle through a set of values. This makes it easy to alternate between two or more colors in a table, or cycle through an array of values.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">index.tpl</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: xml;">&lt;html&gt;&lt;/p&gt;
&lt;head&gt;
&lt;title&gt; Homepage : Pagination&lt;/title&gt;
&lt;/head&gt;&lt;/p&gt;
&lt;body&gt;

&lt;table&gt;
&lt;tr style=&quot;background-color:#336699;color:#eeeeee&quot;&gt;
&lt;td&gt;Country ID&lt;/td&gt;&lt;td width=&quot;65%&quot;&gt;Country Name&lt;/td&gt;

{foreach from=$country item=view}
&lt;tr bgcolor=&quot;{cycle values=&quot;#ffffff,#eeeeee&quot;}&quot;&gt;
&lt;td align=&quot;center&quot;&gt;{$view.country_id}&lt;/td&gt;
&lt;td&gt;{$view.country_name}&lt;/td&gt;
&lt;/tr&gt;
{/foreach}

&lt;tr&gt;&lt;td colspan=&quot;5&quot; align=&quot;center&quot;&gt;{$pager}&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

&lt;/body&gt;

&lt;/html&gt;</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong><span><a href="http://chapagain.googlecode.com/files/pagination_pear_smarty.zip" rel="nofollow" title="Pagination in PEAR and Smarty" >Download source code</a></span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip" rel="nofollow" >http://chapagain.googlecode.com/files/smarty.zip</a></p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/pagination-in-pear-and-smarty/">Pagination in PEAR and Smarty</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=72&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/' rel='bookmark' title='Permanent Link: Creating selection list, using foreach and section loop in Smarty'>Creating selection list, using foreach and section loop in Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/using-database-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Using database in PEAR and Smarty'>Using database in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/pagination-in-pear-and-smarty/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using database in PEAR and Smarty</title>
		<link>http://blog.chapagain.com.np/using-database-in-pear-and-smarty/</link>
		<comments>http://blog.chapagain.com.np/using-database-in-pear-and-smarty/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 06:00:57 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[PEAR]]></category>
		<category><![CDATA[Smarty]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[mdb2]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=71</guid>
		<description><![CDATA[For using database, you need to install a package of PEAR called ‘MDB2’ along with the installation of PEAR and Smarty.
MDB2 provides a common API for all support RDBMS.

Connecting to database
To connect to a database through PEAR::MDB2, you have to create a valid DSN &#8211; data source name. This DSN consists in the following parts:
phptype: Database backend used in PHP (i.e. mysql , pgsql etc.)
dbsyntax: Database used with regards to SQL syntax etc.
protocol: Communication protocol to use ( i.e. tcp, unix etc.)
hostspec: Host specification (hostname[:port])
database: Database to use on the ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For using database, you need to install a package of PEAR called ‘MDB2’ along with the installation of <a href="http://pear.php.net/manual/en/installation.php" rel="nofollow" title="Installing PEAR"  target="_blank">PEAR</a> and <a href="http://blog.chapagain.com.np/index.php/2008/07/11/installing-the-smarty-template-engine/"title="Installing Smarty"  target="_blank">Smarty</a>.</p>
<p>MDB2 provides a common API for all support RDBMS.</p>
<p><span id="more-71"></span></p>
<p><strong>Connecting to database</strong></p>
<p>To connect to a database through PEAR::MDB2, you have to create a valid <acronym>DSN &#8211; data source name</acronym>. This DSN consists in the following parts:</p>
<p class="MsoNormal" style="text-align: justify;">phptype: Database backend used in PHP (i.e. mysql , pgsql etc.)</p>
<p class="MsoNormal" style="text-align: justify;">dbsyntax: Database used with regards to SQL syntax etc.</p>
<p class="MsoNormal" style="text-align: justify;">protocol: Communication protocol to use ( i.e. tcp, unix etc.)</p>
<p class="MsoNormal" style="text-align: justify;">hostspec: Host specification (hostname[:port])</p>
<p class="MsoNormal" style="text-align: justify;">database: Database to use on the DBMS server</p>
<p class="MsoNormal" style="text-align: justify;">username: User name for login</p>
<p class="MsoNormal" style="text-align: justify;">password: Password for login</p>
<p class="MsoNormal" style="text-align: justify;">proto_opts: Maybe used with protocol</p>
<p class="MsoNormal" style="text-align: justify;">option: Additional connection options in URI query string format. options get separated by &amp;.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">The DSN can either be provided as an associative array or as a string. The string format of the supplied DSN is in its fullest form:</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value</em></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">To instantiate a database object you have several methods available using MDB2.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>factory()</em> : Will instantiate a new <span>MDB2_Driver_Common</span> instance, but will not connect to the database until required. This will delay making the actual connection.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>connect()</em> : Will instantiate a new <span>MDB2_Driver_Common</span> instance, and will establish a database connection immediately. This way any connection issues will immediately raise an error.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>singleton()</em> : Returns a MDB2_Driver_Common instance.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">A new MDB2_Driver_Common object is only created once using <em>factory()</em>, subsequent calls to singleton will return a reference to the existing object. This method is preferred over declaring your database object as a global.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">To connect to a database you have to use the function <em>factory()</em>, <em>connect()</em> or <em>singleton()</em>, which require a valid DSN as the first parameter. This parameter can either be a string or an array. The second parameter is the optional $options array that can contain runtime configuration settings for this package.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">I have used the <em>connect()</em> function with the DSN as</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><em>phptype://username:password@hostspec/database</em></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">$mdb2 =&amp; MDB2::connect('mysql://root:@localhost/test');</pre>
</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">In my case, the database backend is mysql, database username is root, database password is null, the hostname is localhost and the database name is test.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong>Querying the database</strong></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">PEAR MDB2 provides several methods for querying databases. The most direct method is query(). It takes a SQL query string as an argument. There are two possible returns: A new MDB2_Result object for queries that return results (such as SELECT queries), or a MDB2_Error object on failure. It should not be used with statements that manipulate data (such as INSERT queries).</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">exec() should be used for manipulation queries. There are two possible returns: An integer denoting the number of affected rows for statements that manipulate data (such as INSERT queries), or a MDB2_Error object on failure. It should not be used with statements that return results (such as SELECT queries).</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">Here, I will only be dealing with the SELECT queries.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">$mdb2 = new ConnectMDB2;&lt;/p&gt;
// calling the function conMDB2 of ConnectMDB2 class
$connect = $mdb2-&gt;conMDB2();&lt;/p&gt;
$res = $connect-&gt;query(&quot;SELECT * FROM basic&quot;);
</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong> </strong></p>
<p class="MsoNormal" style="text-align: justify;"><strong>Fetching result</strong></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">The MDB2_Result_Common object provides four methods for fetching data from rows of a result set: fetchOne(), fetchRow(), fetchCol() and fetchAll().</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">fetchRow() and fetchOne() read an entire row or a single field from a column respectively. The result pointer gets moved to the next row each time these methods are called. NULL is returned when the end of the result set is reached.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">fetchAll() and fetchCol() read all rows in the result set and therefore move the result pointer to the end. While fetchAll() reads the entire row data, fetchCol() only reads a single column.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">MDB2_Error is returned if an error is encountered.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">I have used the fetchRow() method. The fetchmode MDB2_FETCHMODE_ASSOC is used for fetching data. The default fetchmode is MDB2_FETCHMODE_ORDERED.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">while($row[] = $res-&gt;fetchRow(MDB2_FETCHMODE_ASSOC))
{
$result = $row;
}
</pre>
</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">If there is any error then the error message is displayed in a separate template called error.tpl. And if there is no error while fetching data then the fetched result is displayed in the template file index.tpl.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: php;">if(PEAR::isError($res))
{
// get error message
$error =&amp; $res-&gt;getMessage();&lt;/p&gt;
// assign error message
$smarty-&gt;assign('errmessage',$error);&lt;/p&gt;
// display the message and exit
$smarty-&gt;display('error.tpl');
exit();
}&lt;/p&gt;
// asign the content
$smarty-&gt;assign('result',$result);

// display the content
$smarty-&gt;display('index.tpl');
</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">In the template file, <em>foreach</em> loop is used to display the result.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<pre class="brush: xml;">{foreach from=$result item=view}
&lt;tr bgcolor=&quot;{cycle values=&quot;#ffffff,#eeeeee&quot;}&quot;&gt;
&lt;td&gt;{$view.firstname}&lt;/td&gt;
&lt;td&gt;{$view.lastname}&lt;/td&gt;
&lt;/tr&gt;
{/foreach}
</pre>
</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><strong><span><a href="http://chapagain.googlecode.com/files/database_pear_smarty.zip" rel="nofollow" title="Download Source Code" >Download source code</a></span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip" rel="nofollow" >http://chapagain.googlecode.com/files/smarty.zip</a></p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/using-database-in-pear-and-smarty/">Using database in PEAR and Smarty</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=71&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/backup-and-recovery-of-mysql-database/' rel='bookmark' title='Permanent Link: Backup and Recovery of MySQL database'>Backup and Recovery of MySQL database</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/using-database-in-pear-and-smarty/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating selection list, using foreach and section loop in Smarty</title>
		<link>http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/</link>
		<comments>http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 06:56:29 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Smarty]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[section]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=70</guid>
		<description><![CDATA[In this article, I will be illustrating and explaining about foreach and section loop used in Smarty. I will be using these loops for creating a selection list. 
 

We can create selection list from a custom Smarty function called ‘html_options’. html_options is a custom function that creates html option group with provided data. It takes care of which item(s) are selected by default as well. Required attributes are values and output, unless you use options instead.



{html_options values=$id output=$names selected=&#8221;4&#8243;}



Instead of html_options, we can also use loops to create the ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/' rel='bookmark' title='Permanent Link: An Introduction to Smarty Template Engine'>An Introduction to Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/templating-in-smarty/' rel='bookmark' title='Permanent Link: Templating in Smarty'>Templating in Smarty</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span>In this article, I will be illustrating and explaining about foreach and section loop used in Smarty. I will be using these loops for creating a selection list. </span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span id="more-70"></span></p>
<p class="MsoNormal" style="text-align: justify;"><span>We can create selection list from a custom Smarty function called ‘html_options’. </span>html_options is a custom function that creates html option group with provided data. It takes care of which item(s) are selected by default as well. Required attributes are values and output, unless you use options instead.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><br />
<select name="user"></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;">{html_options values=$id output=$names selected=&#8221;4&#8243;}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"></select>
<p></span></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><span>Instead of html_options, we can also use loops to create the selection list. There are namely two loops in Smarty: 1) Section, and 2) Foreach</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">1) <span> </span>Template sections are used for looping over arrays of data. All <span class="emphasis"><em>section</em></span> tags must be paired with <span class="emphasis"><em>/section</em></span> tags. Required parameters are <span class="emphasis"><em>name</em></span> and <span class="emphasis"><em>loop</em></span>. The name of the section can be anything you like, made up of letters, numbers and underscores. Sections can be nested, and the nested section names must be unique from each other. The loop variable (usually an array of values) determines the number of times the section will loop. <span class="emphasis"><em>sectionelse</em></span> is executed when there are no values in the loop variable.</p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span><br />
<select name="country"></span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span>{section name=&#8221;getCountry&#8221; loop=&#8221;$country&#8221;}</span></span></p>
<p class="MsoNormal"><span style="color: #800080;"><span><option value="{$country[getCountry].country_id}" {if $country[getCountry].country_id eq '3'} selected {/if}>{$country[getCountry].country_name} </option></span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span>{/section}</span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span></select>
<p></span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>2)<span> </span></span><span class="emphasis"><em>foreach</em></span> loops are an alternative to <span class="emphasis"><em>section</em></span> loops. <span class="emphasis"><em>foreach</em></span> is used to loop over a single associative array. The syntax for <span class="emphasis"><em>foreach</em></span> is much easier than <span class="emphasis"><em>section</em></span>, but as a tradeoff it can only be used for a single array. <span class="emphasis"><em>foreach</em></span> tags must be paired with <span class="emphasis"><em>/foreach</em></span> tags. Required parameters are <span class="emphasis"><em>from</em></span> and <span class="emphasis"><em>item</em></span>. <span class="emphasis"><em>foreach</em></span> loops can be nested, and the nested foreach names must be unique from each other. The <span class="emphasis"><em>from</em></span> variable (usually an array of values) determines the number of times <span class="emphasis"><em>foreach</em></span> will loop. <span class="emphasis"><em>foreachelse</em></span> is executed when there are no values in the <span class="emphasis"><em>from</em></span> variable.</p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span><br />
<select name="country2"></span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span>{foreach item=country2 from=$country}</span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span><span> </span><option value="{$country2.country_id}" {if $country2.country_id eq '3'}selected{/if}> {$country2.country_name} </option></span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span>{/foreach}</span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #800080;"><span></select>
<p></span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><a href="http://chapagain.googlecode.com/files/selectionlist_smarty.zip" rel="nofollow" title="Section Loop in Smarty" ><span>Download source code</span></a></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip">http://chapagain.googlecode.com/files/smarty.zip<br />
</a></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><span>Thank You.</span></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;">
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/">Creating selection list, using foreach and section loop in Smarty</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=70&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/' rel='bookmark' title='Permanent Link: An Introduction to Smarty Template Engine'>An Introduction to Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/templating-in-smarty/' rel='bookmark' title='Permanent Link: Templating in Smarty'>Templating in Smarty</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Templating in Smarty</title>
		<link>http://blog.chapagain.com.np/templating-in-smarty/</link>
		<comments>http://blog.chapagain.com.np/templating-in-smarty/#comments</comments>
		<pubDate>Mon, 14 Jul 2008 08:35:19 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Smarty]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=69</guid>
		<description><![CDATA[You can also include other smarty template files in one smarty template. This brings the use of template function available in Smarty.
You may want to make a constant header and footer for your entire website. For this purpose, you can use the ‘include’ tag. Include tags are used for including other templates in the current template. Any variables available in the current template are also available within the included template. The include tag must have the attribute &#8220;file&#8221;, which contains the template resource path.

You can also pass variables to included ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/' rel='bookmark' title='Permanent Link: Creating selection list, using foreach and section loop in Smarty'>Creating selection list, using foreach and section loop in Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/installing-the-smarty-template-engine/' rel='bookmark' title='Permanent Link: Installing the Smarty Template Engine'>Installing the Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/' rel='bookmark' title='Permanent Link: An Introduction to Smarty Template Engine'>An Introduction to Smarty Template Engine</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">You can also include other smarty template files in one smarty template. This brings the use of template function available in Smarty.</p>
<p style="text-align: justify;">You may want to make a constant header and footer for your entire website. For this purpose, you can use the ‘include’ tag. Include tags are used for including other templates in the current template. Any variables available in the current template are also available within the included template. The include tag must have the attribute &#8220;file&#8221;, which contains the template resource path.</p>
<p><span id="more-69"></span></p>
<p>You can also pass variables to included templates as attributes. Attribute variables override current template variables. In our case, the ‘title’ variable is passed to the included template ‘header.tpl’. It the ‘title’ variable is not passed in the included template ‘header.tpl’ then the default value of the title assigned in ‘header.tpl’ will be printed.</p>
<p>We have included header.tpl and footer.tpl in the index.tpl file. The header.tpl file contains title and banner text. The footer.tpl file contains copyright information. This information is constant over the entire website. So, it would be easy to modify the header and footer content if we include these files in every individual template.</p>
<p>index.tpl</p>
<pre class="brush: xml;">{include file=&quot;header.tpl&quot; title=&quot;Homepage&quot;}&lt;/p&gt;
The author of this tutorial:&lt;br/&gt;&lt;br/&gt;
Name: {$name} &lt;br/&gt;
Address: {$address} &lt;br/&gt;
Date: {$smarty.now|date_format:&quot;%Y-%m-%d&quot;} &lt;br/&gt;&lt;/p&gt;
{include file=&quot;footer.tpl&quot;}</pre>
<p>header.tpl</p>
<pre class="brush: xml;">&lt;html&gt;&lt;head&gt;
&lt;title&gt; {$title|default:&quot;no title&quot;} &lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;h2&gt;Welcome to my site&lt;/h2&gt;</pre>
<p>footer.tpl</p>
<pre class="brush: xml;">&lt;br/&gt;
Copyright © Mukesh Chapagain
&lt;/body&gt;&lt;/p&gt;
&lt;/html&gt;</pre>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><a href="http://chapagain.googlecode.com/files/templating_smarty.zip" rel="nofollow" title="Templating Smarty" ><span>Download source code</span></a></p>
<p class="MsoNormal" style="text-align: justify;">
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip">http://chapagain.googlecode.com/files/smarty.zip<br />
</a></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/templating-in-smarty/">Templating in Smarty</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=69&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/' rel='bookmark' title='Permanent Link: Creating selection list, using foreach and section loop in Smarty'>Creating selection list, using foreach and section loop in Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/installing-the-smarty-template-engine/' rel='bookmark' title='Permanent Link: Installing the Smarty Template Engine'>Installing the Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/' rel='bookmark' title='Permanent Link: An Introduction to Smarty Template Engine'>An Introduction to Smarty Template Engine</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/templating-in-smarty/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing the Smarty Template Engine</title>
		<link>http://blog.chapagain.com.np/installing-the-smarty-template-engine/</link>
		<comments>http://blog.chapagain.com.np/installing-the-smarty-template-engine/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 12:19:45 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Smarty]]></category>
		<category><![CDATA[install]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=68</guid>
		<description><![CDATA[At first you need to download Smarty from http://smarty.php.net/download.php or you can download it directly from http://chapagain.googlecode.com/files/smarty.zip. Extract the zip file. Rename the extracted folder as ‘smarty’. Then, copy the folder ‘smarty’ to your working directory present inside the root directory of your web server.
 

There are different ways to install Smarty. 
 
1) You can simply supply the absolute path to the Smarty library file:- 
require(&#8216;/usr/local/lib/php/Smarty/Smarty.class.php&#8217;);
 
2) You can add library directory to PHP include_path
Copy the libs folder from the Smarty distribution to the mycode folder (for example, C:\apache\htdocs\mycode\libs\).
After ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/' rel='bookmark' title='Permanent Link: An Introduction to Smarty Template Engine'>An Introduction to Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span>At first you need to download Smarty from <a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a> or you can download it directly from <a href="http://chapagain.googlecode.com/files/smarty.zip" rel="nofollow" >http://chapagain.googlecode.com/files/smarty.zip</a>. Extract the zip file. Rename the extracted folder as ‘smarty’. Then, copy the folder ‘smarty’ to your working directory present inside the root directory of your web server.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span id="more-68"></span></p>
<p class="MsoNormal" style="text-align: justify;"><span>There are different ways to install Smarty. </span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>1)<span style="font-family: "> </span></span></span><!--[endif]--><span>You can simply supply the absolute path to the Smarty library file:- </span></p>
<p class="MsoNormal" style="text-align: justify; text-indent: 0.5in;"><em><span>require(&#8216;/usr/local/lib/php/Smarty/Smarty.class.php&#8217;);</span></em></p>
<p class="MsoNormal" style="text-align: justify; text-indent: 0.5in;"><em><span> </span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>2)<span style="font-family: "> </span></span></span><!--[endif]--><span>You can add library directory to PHP include_path</span></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify;"><em><span>Copy the libs folder from the Smarty distribution to the mycode folder (for example, C:\apache\htdocs\mycode\libs\).</span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify;"><em><span>After this, you need to edit the php.ini file and add include_path = &#8220;.;C:\apache\htdocs\ mycode\libs\&#8221; where C:\apache\htdocs\mycode\libs\ represents the location of your libs folder.</span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify;"><em><span> </span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>3)<span style="font-family: "> </span></span></span><!--[endif]--><span>You can set SMARTY_DIR constant manually</span></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify;"><em><span>define(&#8216;SMARTY_DIR&#8217;, &#8216;/usr/local/lib/php/Smarty/&#8217;); </span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify;"><em><span>require(SMARTY_DIR . &#8216;Smarty.class.php&#8217;);</span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify;"><em><span> </span></em></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>4)<span style="font-family: "> </span></span></span><!--[endif]--><span>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.</span></p>
<p class="MsoNormal" style="text-align: justify; text-indent: 0.5in;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="text-decoration: underline;"><span>I will be explaining the fourth way, i.e. extending the class and initializing Smarty environment.</span></span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>At first, let us have a look on the directory structure that we must create. Suppose, our work folder is named ‘example’, then our directory structure should be as under:-</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>example</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|_ _ _ _ _ _ _ smarty (this directory contains Smarty library files)</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|_ _ _ _ _ _ _ classes (this directory contains class files that we create)</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|_ _ _ _ _ _ _ smarty_temp (contains manually created Smarty directories)</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>|_ _ _ _ _ _ _ index.php</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>As stated at the beginning of this article, the Smarty library files are kept in the ‘smarty’ directory.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Smarty requires four directories which are (by default) named templates, templates_c, configs and cache. Each of these are definable by the Smarty class properties $template_dir, $compile_dir, $config_dir, and $cache_dir respectively. It is highly recommended that you setup a separate set of these directories for each application that will use Smarty. We create these directories inside ‘smarty_temp’ directory.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Now, let’s move to the class file which is kept inside the classes directory.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span>
<pre class="brush: php;">&lt;?php
/**
* Loading Smarty library
*/
require_once 'smarty/libs/Smarty.class.php';&lt;/span&gt;&lt;/span&gt;

/**
* Connection to Smarty package
*
*/

class ConnectSmarty extends Smarty
{
/**
* This function connects with smarty
* manage paths to smarty directories
* @access public
*/
function ConnectSmarty()
{
$this-&gt;Smarty();

$this-&gt;template_dir = 'smarty_temp/templates';
$this-&gt;config_dir = 'smarty_temp/configs';
$this-&gt;cache_dir = 'smarty_temp/cache';
$this-&gt;compile_dir = 'smarty_temp/templates_c';
}
}

?&gt;</pre>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>The above class is called in the index page. An object of the class is created. Then, the Smarty method called ‘assign’ is used to assign values to the template. Through assign method, y</span>ou can explicitly pass name/value pairs, or associative arrays containing the name/value pairs.<span> After that ‘display’ method is used to display the template.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span>
<pre class="brush: php;">&lt;/span&gt;&lt;?php
require_once('classes/ConnectSmarty.class.php');

// create an object of the class included above
$smarty = new ConnectSmarty;

// assign content
$smarty-&gt;assign('name','Mukesh Chapagain');
$smarty-&gt;assign('address','Kathmandu, Nepal');

// display the content
$smarty-&gt;display('index.tpl');

?&gt;</pre>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>index.tpl is the template file which contains html and smarty code.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span>
<pre class="brush: xml;">&lt;/span&gt;&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Homepage &lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

The author of this tutorial:&lt;br/&gt;&lt;br/&gt;
Name: {$name} &lt;br/&gt;
Address: {$address} &lt;br/&gt;

&lt;/body&gt;

&lt;/html&gt;</pre>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span><a href="http://chapagain.googlecode.com/files/install_smarty.zip" rel="nofollow" title="Install Smarty Template Engine"  target="_blank">Download source code</a></span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Note: <em>I have not included Smarty library files in this zip file. You can download the Smarty library files from </em></span><a href="http://smarty.php.net/download.php" rel="nofollow" ></a><a href="http://smarty.php.net/download.php" rel="nofollow" title="Download Smarty"  target="_blank">http://smarty.php.net/download.php</a><span><em> or directly from </em></span><a href="http://chapagain.googlecode.com/files/smarty.zip" rel="nofollow" ></a><a href="http://chapagain.googlecode.com/files/smarty.zip">http://chapagain.googlecode.com/files/smarty.zip<br />
</a></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Thank You.</span></p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/installing-the-smarty-template-engine/">Installing the Smarty Template Engine</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=68&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/file-upload-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: File Upload in PEAR and Smarty'>File Upload in PEAR and Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/' rel='bookmark' title='Permanent Link: An Introduction to Smarty Template Engine'>An Introduction to Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/pagination-in-pear-and-smarty/' rel='bookmark' title='Permanent Link: Pagination in PEAR and Smarty'>Pagination in PEAR and Smarty</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/installing-the-smarty-template-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>An Introduction to Smarty Template Engine</title>
		<link>http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/</link>
		<comments>http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 05:32:30 +0000</pubDate>
		<dc:creator>Mukesh</dc:creator>
				<category><![CDATA[Smarty]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template engine]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://blog.chapagain.com.np/?p=63</guid>
		<description><![CDATA[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 programmer and the template designer play different roles, or in most cases is not the same person. 
 

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, ...


<strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/installing-the-smarty-template-engine/' rel='bookmark' title='Permanent Link: Installing the Smarty Template Engine'>Installing the Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/templating-in-smarty/' rel='bookmark' title='Permanent Link: Templating in Smarty'>Templating in Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/' rel='bookmark' title='Permanent Link: Creating selection list, using foreach and section loop in Smarty'>Creating selection list, using foreach and section loop in Smarty</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><span>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 programmer and the template designer play different roles, or in most cases is not the same person. </span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span id="more-63"></span></p>
<p class="MsoNormal" style="text-align: justify;"><span>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. </span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>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 desire no logic in your templates you certainly can do so by boiling the content down to text and variables only. </span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>With Smarty, the PHP syntax doesn&#8217;t contain print. Instead, the programmer passes these arrays to the designer by assigning them to Smarty templates. Then it&#8217;s the designer&#8217;s job to make them look good in the web page without worrying about the PHP code. The use of Smarty comes at its best when you have different programmers and designers and you don’t want the designer to access the system core.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Smarty makes the job of the designer as well as the programmer very easy. The key tasks performed by them can be listed as follows.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>The Programmer&#8217;s tasks:</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>• Extract database elements with a simple query on the database.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span>• Validate and manipulate the data by performing business logic on it.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span>• If needed, change the data access methods and the business logic without interfering with the designer&#8217;s work. For example, the whole system could migrate from MySQL to PostgreSQL without the designer making a single change.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>The Designer&#8217;s tasks:</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>• Create HTML designs without affecting or jeopardizing the programmers PHP code. The designer only needs to be concerned with placing the content elements that the programmer has agreed to provide.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span>• Make changes to the design without consulting or interfering with the programmer&#8217;s work.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span>• Stop worrying about technical changes to the site breaking the way that the site appears to viewers.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>Some of the Smarty Features are:-</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span>No template parsing overhead, only compiles once. It is smart about recompiling only the template files that have changed.</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span>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. </span>When a file is called from the web, the php file is parsed and smarty compiles the results from the template files used. When that same page is called and the template files have not been altered smarty will use the compiled version which causes the server to not need to recompile the pages.</p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><strong>Built-in caching support</strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">Smarty has the ability to use cache. Hence, it is extremely fast. Smarty caches the output of the template contents, saving the overhead expense involved in retrieving data from a data source. Smarty caches the output of the template with this data from the data source, and saves you from having to connect to the database every time your Web page is accessed.</p>
<p class="MsoNormal" style="text-align: justify;">Smarty is again smart enough to allow you to specify what should or should not be cached. In fact, you can have cached and un-cached portions on the same template page, as Smarty allows you to specify exactly what you don&#8217;t want cached (like that stock ticker at the bottom of the page, which is frequently changed) and what you do want cached (such as your navigation bar, which is seldom changed). You can also set the cache expiry time so that your template output is cached only for a specific length of time. You can thus achieve the middle-ground between having up-to-date dynamic content and quick-to-load Web pages.</p>
<p class="MsoNormal" style="text-align: justify;"><strong><span>You can make custom functions and custom variable modifiers, so the template language is extremely extensible.</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">Smarty provides built-in and custom functions for use in your templates. These functions are like the API of Smarty templates, except that custom functions can be modified but not built-in functions. In the custom functions, you can assign template variables during the execution of the templates.</p>
<p class="MsoNormal" style="text-align: justify;">Variable modifiers give template designers the ability to modify template variables. Variable modifiers can be applied to variables, custom functions or strings. To apply a modifier, specify the value followed by the <tt><span style="font-size: 10pt;">|</span></tt> (pipe) and the modifier name. A modifier may accept additional parameters that affect its behavior. These parameters follow the modifer name and are separated by <tt><span style="font-size: 10pt;">:</span></tt> (colon).</p>
<p class="MsoNormal" style="text-align: justify;"><em><span>{$title|upper} =</span></em><span> Applying modifier to a variable. This will change the variable text into uppercase.</span></p>
<p class="MsoNormal" style="text-align: justify;"><em><span> </span></em></p>
<p class="MsoNormal" style="text-align: justify;"><em><span>{$title|truncate:40:&#8221;&#8230;&#8221;}</span></em><span> = Applying modifier with parameters. This will truncate the variable text into 40 characters followed by three dots (…).</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><strong>It is possible to embed PHP code right in your template files, although this may not be needed (nor recommended) since the engine is so customizable.</strong><strong></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p>php tags allow php to be embedded directly into the template. This is for advanced users only, not normally needed.</p>
<p class="MsoNormal" style="text-align: justify;"><span>{php}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>// including a php script directly</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>// from the template.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>include(&#8220;/path/to/display_weather.php&#8221;);</span></p>
<p><span>{/php}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span>Presence of conditional statements (<em>if/elseif/else</em>) and loops (<em>foreach/section</em>)</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p><span class="emphasis"><em>{if}</em></span> statements in Smarty have much the same flexibility as PHP if statements, with a few added features for the template engine. Every <span class="emphasis"><em>{if}</em></span> must be paired with an <span class="emphasis"><em>{/if}</em></span>. <span class="emphasis"><em>{else}</em></span> and <span class="emphasis"><em>{elseif}</em></span> are also permitted. All PHP conditionals are recognized, such as <span class="emphasis"><em>||</em></span>, <span class="emphasis"><em>or</em></span>, <span class="emphasis"><em>&amp;&amp;</em></span>, <span class="emphasis"><em>and</em></span>, etc.</p>
<p class="MsoNormal" style="text-align: justify;"><span>{if $name eq &#8220;Fred&#8221;}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>Welcome Sir.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span>{elseif $name eq &#8220;Wilma&#8221;}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>Welcome Ma&#8217;am.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span>{else}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span><span> </span>Welcome, whatever you are.</span></p>
<p><span>{/if}</span></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">Template sections are used for looping over arrays of data. All <span class="emphasis"><em>section</em></span> tags must be paired with <span class="emphasis"><em>/section</em></span> tags. <span class="emphasis"><em>foreach</em></span> loops are an alternative to <span class="emphasis"><em>section</em></span> loops. <span class="emphasis"><em>foreach</em></span> is used to loop over a single associative array. <span class="emphasis"><em>foreach</em></span> tags must be paired with <span class="emphasis"><em>/foreach</em></span> tags. Both <em>section</em> and <em>foreach</em> loops can be nested, and the nested loops names must be unique from each other.</p>
<p class="MsoNormal" style="text-align: justify;"><strong><span>Config Files</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">Config files are configuration files where you can store global template variables. This allows you to store variables that should affect every template (i.e. global variables) in a central location. A good example of such a variable would be the color scheme for your templates. Normally if you wanted to change the color scheme of an application, you would have to go through each and every template file and change the colors. With a config file, the colors can be kept in one place, and only one file needs to be updated.</p>
<p class="MsoNormal" style="text-align: justify;">The other variables that you can store in a config files are like page title, page information, database information, etc.</p>
<p class="MsoNormal" style="text-align: justify;"><strong><span>Plugin architecture</span></strong></p>
<p class="MsoNormal" style="text-align: justify;"><span> </span></p>
<p class="MsoNormal" style="text-align: justify;">The Smarty plug-in architecture was introduced in version 2.0 and allows you to customize Smarty to suit your purposes. With plug-ins, you can create your own template functions, variable modifiers and filters. It is used for almost all the customizable functionality of Smarty. This includes: functions, modifiers, block functions, compiler functions, prefilters, postfilters, outputfilters, resources, and inserts.<span> </span></p>
<p class="MsoNormal" style="text-align: justify;"><em><span> </span></em></p>
<p class="MsoNormal" style="text-align: justify;"><em><span>Source:</span></em></p>
<p class="MsoNormal" style="text-align: justify;"><strong><span> </span></strong></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>1)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "Times New Roman";"> </span></span></span><!--[endif]--><em><span>Smarty Manual</span></em><span>, Monte Ohrt, Andrei Zmievski, 2001-2005 New Digital Group, Inc.</span></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>2)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "Times New Roman";"> </span></span></span><!--[endif]--><em><span>Smarty – PHP Template Programming and Applications</span></em><span>, Lucian Gheorghe, Hasin Hayder, Joao Prado Maia. </span></p>
<p class="MsoNormal" style="margin-left: 0.5in; text-align: justify; text-indent: -0.25in;"><!--[if !supportLists]--><span><span>3)<span style="font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; font-family: "Times New Roman";"> </span></span></span><!--[endif]--><span>http://www.sitepoint.com/article/smarty-php-template-engine </span></p>
<div style="margin-top: 15px; font-style: italic">
<p><strong>From</strong> <a href="http://blog.chapagain.com.np/">Mukesh Chapagain&#039;s Blog</a> | <strong>Post</strong> <a href="http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/">An Introduction to Smarty Template Engine</a></p>
</div>
<hr /><small>Copyright &copy; 2010<br /> This feed is for personal, non-commercial use only. <br /> The use of this feed on other websites breaches copyright. If this content is not in your news reader, it makes the page you are viewing an infringement of the copyright. (Digital Fingerprint:<br /> )</small><img src="http://blog.chapagain.com.np/?ak_action=api_record_view&id=63&type=feed" alt="" />

<p><strong>Related posts:</strong><ol><li><a href='http://blog.chapagain.com.np/installing-the-smarty-template-engine/' rel='bookmark' title='Permanent Link: Installing the Smarty Template Engine'>Installing the Smarty Template Engine</a></li>
<li><a href='http://blog.chapagain.com.np/templating-in-smarty/' rel='bookmark' title='Permanent Link: Templating in Smarty'>Templating in Smarty</a></li>
<li><a href='http://blog.chapagain.com.np/creating-selection-list-using-foreach-and-section-loop-in-smarty/' rel='bookmark' title='Permanent Link: Creating selection list, using foreach and section loop in Smarty'>Creating selection list, using foreach and section loop in Smarty</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.chapagain.com.np/an-introduction-to-smarty-template-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
