Smarty Hello World Program

Learn your First program in Smarty - Hello World


Share on Google+Share on Google+

Smarty Hello World Program


Learn your First program in Smarty - Hello World

How to write "Hello World" program?

In any smarty program we need two files: a) .php file and b).tpl file.

    i) .php file: Which will have the basic php coding and in this file we instantiate the smarty object, after instantiating we use the fields and methods of smarty like caching, assign, display etc.

    ii) .tpl file: In the .tpl file we write basic html coding and within braces we write our smarty coding.

Put the smarty folder inside the C:\wamp\www folder .

Store the php files inside the smarty folder and store the tpl files inside the templates folder.

 First example:

i) Business logic (HelloWorld.php) which contains smarty variables:


//This is a simple comment use in php file 

require './libs/Smarty.class.php';//require function  includes Smarty class  

$smarty=new Smarty;// instantiates an object $smarty of class Smarty

$smarty->caching =true;//enable the caching  

$smarty->assign ('name', 'hello world');//used to assign values to the templates

$smarty->display ('HelloWorld.tpl');//this is used to display the template



ii) Presentation code (HelloWorld.tpl):



<title>My first Smarty program</title>



{*This is a comment line of smarty *}

{$name}{*Smarty will replace the assigned content *}




To run your first program:

i) Start WAMP server

ii) Type http://localhost/Smarty/HelloWorld.php in internet browser's address bar, you'll get the output on the screen




Posted on: October 8, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles