IT Community - Software Programming, Web Development and Technical Support

Smarty

This is a discussion on Smarty within the PHP Programming forums, part of the Web Development category; Hi all I hereby am giving you some information about smarty in PHP. It will very useful to integrate smarty ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 08-30-2007, 05:23 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Post Smarty

Hi all
I hereby am giving you some information about smarty in PHP. It will very useful to integrate smarty in PHP

Smarty
Smarty is nothing but a template engine for PHP. Here we can separate the application code and design. The application code is the logic we have written in PHP. The design is the template which is the html contents (fonts, stylesheets, image, etc) to be displayed. The concept behind smarty is template compiling. The smarty reads the template files and converts the content to PHP script. Once they created, they are executed from then on.
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017

Last edited by sureshbabu : 08-30-2007 at 05:25 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-30-2007, 05:28 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Question Re: Smarty

Hi suresh,

Thats cool.. Shall i know some special features in Smarty, which makes it different from using the traditional html way of using templates??
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-30-2007, 05:32 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Thumbs up Re: Smarty

Hi Jegan
Ya...sure..i can explain the features of smarty.. Here are some features of smarty

Features of smarty:
1. Fast execution time
2. More efficient
3. Built in cache support
4. Plugin architecture
5. It only recompiles the template files that have changed
6. Unlimited nesting of ifs, loops(sections) are allowed
7. We can use the custom functions and variables to design the template.
As in PHP we can use the variables, functions, attributes, variable modifiers, methods, etc.. to build the template.
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-30-2007, 05:59 AM
P.Sathiya P.Sathiya is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 16
P.Sathiya is on a distinguished road
Default Re: Smarty

Hi guys.

Some more information about smarty

Most other template engines for PHP provide basic variable substitution and dynamic block functionality. Smarty takes a step further to be a "smart" template engine, adding features such as configuration files, template functions, variable modifiers and making all of this functionality as easy as possible to use for both programmers and template designers.

Smarty also compiles the templates into PHP scripts, eliminating the need to parse the templates on every invocation, making Smarty extremely scalable and manageable for large application needs.


Thanks!.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-30-2007, 11:14 PM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Thumbs up Re: Smarty

Hi Suresh....

i understand about smarty and its featues.
now how can we install and configure smarty.
will you explain this...



by
Saravanan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-30-2007, 11:18 PM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Thumbs up Re: Smarty

Hi Saravanan
Ya.Sure...I will.

Installing smarty
The smarty library files can be downloaded from http://smarty.php.net/do_download.ph...-2.6.18.tar.gz.

Here are the library files for smarty
1. Smarty.class.php
2. Smarty_Compiler.class.php
3. Config_File.class.php
4. debug.tpl
5. /internals/*.php (all of them)
6. /plugins/*.php (all of them)

The smarty class can be included simply in your script as follows
<?php
require('Smarty.class.php');
$oSmarty = new Smarty;
?>
If your experiencing the file not found error, you have to enter the full path of the location of the smarty class file.
The path can also be set manually as follows

<?php
define('SMARTY_DIR', '/usr/local/lib/php/Smarty/');
require(SMARTY_DIR . 'Smarty.class.php');
$oSmarty = new Smarty;
?>

Then we have to setup the smarty directories for our applications. The smarty directory path variables and their respective folder name are as follows

1. $ template_dir - templates
2. $compile_dir - templates_c
3. $config_dir - configs
4. $cache_dir - cache

Example

1. $ template_dir = “../templates/”
2. $compile_dir = “../templates_c”
3. $config_dir = “../configs/”
4. $cache_dir = “../cache/”

Creating an application in smarty

The PHP code:

<?php
require('Smarty.class.php');
$oSmarty = new Smarty;
$oSmarty->assign(“Hello discuss web”,$vStr);
$oSmarty ->assign('Contacts',
array('fax' => '555-222-9876',
'email' => 'zaphod@slartibartfast.com',
'phone' => array('home' => '555-444-3333',
'cell' => '555-111-1234')));
$oSmarty ->display('index.tpl');
?>

Where Index.tpl is

<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>{$vStr}</td>
</tr>
<tr>
<td>{$Contacts.fax}</td>
</tr>
<tr>
<td>{$Contacts.email}</td>
</tr>
<tr>
<td>{$Contacts.phone.home}</td>
</tr>
<tr>
<td>{$Contacts.phone.cell}</td>
</tr>
</table>

Output will be:

Hello discuss web
555-222-9876
zaphod@slartibartfast.com
555-444-3333
555-111-1234
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-31-2007, 01:07 AM
P.Sathiya P.Sathiya is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 16
P.Sathiya is on a distinguished road
Default Re: Smarty

hi,
Can any one explain briefly smarty cache concept?

txs.

Last edited by P.Sathiya : 08-31-2007 at 01:12 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 08-31-2007, 05:53 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Thumbs up Re: Smarty

Hi sathya
Ya sure...I can explain about cache in smarty.

Caching concept in smarty is used to speed up process of calling the template. The first thing is we have to enable the cache. This is done simply by setting the member variable $caching. Here is the sample code for enabling the cache.

<?
require('Smarty.class.php');
$oSmarty = new Smarty;
$oSmarty->caching = true;
$oSmarty->display('test.tpl');
?>

The function display() will display the template as usual. But if the cache is enabled, a copy of the out put will be saved in cache. If we supposed to display the same template again, the cached copy will be used instead of rendering the template.

We can set lifetime for the files inside the cache. The default value is 3600 seconds. i.e. 1 hour. After that it will expire. We can also set the cache time individually i.e for each file. This can be done by setting the value for the variable $cache_lifetime

Here is a sample for using cache individually
<?
require('Smarty.class.php');
$oSmarty = new Smarty;
$oSmarty->caching = 2;
$oSmarty->cache_lifetime = 3600;
$oSmarty->display('test.tpl');
?>
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017

Last edited by sureshbabu : 08-31-2007 at 06:16 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 08-31-2007, 06:23 AM
P.Sathiya P.Sathiya is offline
D-Web Trainee
 
Join Date: Mar 2007
Posts: 16
P.Sathiya is on a distinguished road
Smile Re: Smarty

First of all thanks Mr.suresh for your responce

i have another one qurey. What is PHPA & explain its part of the work in smarty cache?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 09-10-2007, 08:59 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: Smarty

Hi suresh ,

Can u please explain me little bit elaborately about, $cache_lifetime, coz, wat u said in the previous post, i have some confusions in cache lifetime setting
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 09-10-2007, 09:00 AM
jegan jegan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 161
jegan is on a distinguished road
Default Re: Smarty

Also suresh,

can u pls say me abt condition checking, validating processes in Smarty..
__________________
Thanks & Regards,
Jegan CBK
"We will either find a way, or make one!”
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 09-27-2007, 03:25 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Thumbs up Re: Smarty

Hi jagan,
$cache_lifetime:

This is the length of time in seconds that a template cache is valid. Once this time has expired, the cache will be regenerated. $caching must be set to "true" for $cache_lifetime to have any purpose. A value of -1 will force the cache to never expire. A value of 0 will cause the cache to always regenerate (good for testing only, to disable caching a more efficient method is to set $caching = false.)

If $force_compile is enabled, the cache files will be regenerated every time, effectively disabling caching. You can clear all the cache files with the clear_all_cache() function, or individual cache files (or groups) with the clear_cache() function.

Technical Note: If you want to give certain templates their own cache lifetime, you could do this by setting $caching = 2, then set $cache_lifetime to a unique value just before calling display() or fetch().

See the following code....

<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = 2; // lifetime is per cache

// set the cache_lifetime for index.tpl to 5 minutes
$smarty->cache_lifetime = 300;
$smarty->display('index.tpl');

// set the cache_lifetime for home.tpl to 1 hour
$smarty->cache_lifetime = 3600;
$smarty->display('home.tpl');

// NOTE: the following $cache_lifetime setting will not work when $caching = 2.
// The cache lifetime for home.tpl has already been set
// to 1 hour, and will no longer respect the value of $cache_lifetime.
// The home.tpl cache will still expire after 1 hour.
$smarty->cache_lifetime = 30; // 30 seconds
$smarty->display('home.tpl');
?>

Hope it helps u.....
Thanks,
venkatbi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 09-27-2007, 04:06 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: Smarty

$compile_check

Example for enabling $compile_check
<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;
$smarty->compile_check = true;

$smarty->display('index.tpl');
?>


If $compile_check is enabled, every template file and config file that is involved with the cache file is checked for modification. If any of the files have been modified since the cache was generated, the cache is immediately regenerated.

If $force_compile is enabled, the cache files will always be regenerated. This effectively turns off caching. $force_compile is usually for debugging purposes only, a more efficient way of disabling caching is to set $caching = false (or 0.)

Thanks,
Venkatbi
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 09-27-2007, 04:37 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: Smarty

We can clear all the cache files with the clear_all_cache() function, or individual cache files (or groups) with the clear_cache() function.

clearing the cache

<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;

// clear out all cache files
$smarty->clear_all_cache();

// clear only cache for index.tpl
$smarty->clear_cache('index.tpl');

$smarty->display('index.tpl');
?>
__________________
.......................
Thanks,
Venkatbi...

Last edited by venkatbi : 09-27-2007 at 04:39 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 09-27-2007, 04:51 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: Smarty

Multiple Caches Per Page

You can have multiple cache files for a single call to display() or fetch(). Let's say that a call to display('index.tpl') may have several different output contents depending on some condition, and you want separate caches for each one. You can do this by passing a cache_id as the second parameter to the function call.

Example for passing a cache_id to display() function

<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->caching = true;

$my_cache_id = $_GET['article_id'];

$smarty->display('index.tpl',$my_cache_id);
?>

Above, we are passing the variable $my_cache_id to display() as the cache_id. For each unique value of $my_cache_id, a separate cache will be generated for index.tpl. In this example, "article_id" was passed in the URL and is used as the cache_id.
__________________
.......................
Thanks,
Venkatbi...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 09-28-2007, 03:53 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: Smarty

Filters:

Smarty allows you to specify ('register' or 'load' actually) filters through which you can run your templates before or after they are compiled. Prefilters are functions that your templates are run through before they're compiled; postfilters after; and output filters, upon the template output as it is requested.

'Why filters?' you say. Prefilters allow you to do things like removing unwanted comments (such as those created by Dreamweaver) and ensuring content in the templates you don't want does not go through to the compiler. Postfilters let you add additional information to your templates, such as the template creation date (as a comment) after they're compiled. Output filters give you the ability to modify your template output, allowing you to do things like obfuscating email addresses on your Web page to protect against spambots (using a preg_replace()).
__________________
.......................
Thanks,
Venkatbi...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 09-28-2007, 07:16 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: Smarty

Config Files

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. Your template designers only have to change the values in the config file should a color scheme revamp be required. This saves them suffering through the painful alternative of going through every individual template to change the colors.

Config files also allow for sections, which are not unlike those in .ini files. The section names are enclosed in brackets (e.g. [welcome_page]) and are only loaded upon request. Anything that's not in a section is globally available (upon a call to the config_load function).
__________________
.......................
Thanks,
Venkatbi...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 09-28-2007, 07:19 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Default Re: Smarty

Plug-ins:

The Smarty plug-in architecture was introduced in version 2.0 and allows you to customize Smarty to suit your purposes (however grand or nefarious). The prefilters, postfilters and output filters I discussed earlier are just some of the plug-in types available to the customizer. Other plug-in types are the modifier, block, compiler, resource and insert types.

With plug-ins, you can create your own template functions, variable modifiers and filters. You can even change the data source you want Smarty to read from (the default is from flat files), using a resource plug-in. With a resource plug-in, you can save your templates in a database, and retrieve them using sockets (or any other method you use to access templates with PHP. This means you can access just about any source).
__________________
.......................
Thanks,
Venkatbi...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 09-28-2007, 08:10 AM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: Smarty

Hi All,

I need add some additional functionality in the existing plug-in file. Is it possible to modify the existing plug-in or i can create a new one?.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 09-29-2007, 01:36 AM
venkatbi venkatbi is offline
D-Web Programmer
 
Join Date: Jul 2007
Posts: 80
venkatbi is on a distinguished road
Thumbs up Re: Smarty

Hi Senthilkumar,
instead of modifying the existing plug-in file u can create ur own plug-in file and add it ur plug-in directory.
__________________
.......................
Thanks,
Venkatbi...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use smarty variables in javascript? sureshbabu PHP Programming 1 03-31-2008 02:48 AM
Drawback in smarty sureshbabu PHP Programming 0 03-28-2008 06:03 AM
validate using smarty venkatbi PHP Programming 2 09-05-2007 08:57 AM
Smarty Jeyaseelansarc PHP Programming 3 05-16-2007 06:35 AM


All times are GMT -7. The time now is 03:01 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0