Smarty Hello World Program


 

Smarty Hello World Program

Learn your First program in Smarty - Hello World

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:

0

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

 

1

Ads