Home Tutorial Php Smarty Smarty Hello World Program

 
 

Share on Google+Share on Google+
Smarty Hello World Program
Posted on: October 8, 2009 at 12:00 AM
Advertisement
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:

<?php

//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):

<html>

<head>

<title>My first Smarty program</title>

</head>

<body>

{*This is a comment line of smarty *}

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

</body>

</html>

 

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

 

Advertisement

Related Tags for Smarty Hello World Program:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 8, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com