Español   Català  
Guifi.net - English Wiki

Smarty

From Guifi.net - English Wiki

Smarty is a template engine written in PHP. Smarty separates PHP, as a business logic, from HTML, a presentation logic, and generates web content by the placement of special Smarty tags within a document (i.e. variable substitution).

Tags are variables such as {$variable}, and a range of logical and loop operators.

This makes for much cleaner coding and more flexible modification. For instance, one could typically modify a Smarty-based application's appearance (i.e. skin) easily, without ever looking at any business logic or PHP code. This compartmentalization also allows for the the back-end to change separate from the layout.

Smarty allows template programming with

and many more features. Other template engines also support these commands in templates.

Contents

Installation

For a system wide installation, follow the instructions in the manual:

Debian Installation

For 'lutka' test installation, the steps in a nutshell (assuming blogmail app installed at webroot/blogmail): Debian package installs to /usr/share/php/smarty/libs

Update /etc/php/apache/php.ini to read:

; path to Smarty Template library 
; require('Smarty.class.php');
; $smarty = new Smarty;
include_path=".:/usr/share/php/smarty/libs

In /webroot create the folders:

  • /smarty/blogmail/templates
  • /smarty/blogmail/templates_c
  • /smarty/blogmail/configs
  • /smarty/blogmail/cache

Set permissions for apache user (nobody or www-data as per your httpd.conf file):

chown nobody:nobody /smarty/templates_c/
chmod 770 /smarty/templates_c/
chown nobody:nobody /smarty/cache/
chmod 770 /smarty/cache/

Create the file /public_html/smarty/blogmail/templates/index.tpl with the contents:

{* Smarty *} Hello, {$name}!

Update your php.ini include path to read:

include_path=".:/usr/share/php/smarty/libs:/path/to/webroot/php/includes"

Create a folder in webroot: php/includes/blogmail/ and a file there named setup.php:

<?php
require('Smarty.class.php');
class Smarty_Blogmail extends Smarty
{
       function Smarty_Blogmail()      // Class Constructor. These automatically get set with each new instance.
      {
               $this->Smarty();
               $this->template_dir='/home/mdl/public_html/smarty/blogmail/templates/';
               $this->compile_dir='/home/mdl/public_html/smarty/blogmail/templates_c/';
               $this->config_dir='/home/mdl/public_html/smarty/blogmail/configs/';
               $this->cache_dir='/home/mdl/public_html/smarty/blogmail/cache/';
               $this->caching = true;
               $this->assign('app_name','Blogmail Develpoment on Lutka');
      }

} ?>

Create a file webroot/blogmail/index.php with the contents:

<?php require('blogmail/setup.php');
$smarty = new Smarty_Blogmail;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
?>

Test in your browser for output "Hello, Ned!"

Smarty Example

index.php

include('Smarty.class.php');
// Crea Objeto
$smarty = new Smarty;
// Asigna algo de contenido, este vendria comunmente de
// una base de datos u otra fuente,pero usaremos valores
// estáticos para este ejemplo.
$smarty->assign('nombre', 'José Manuel Pardo Pérez');
$smarty->assign('direccion', 'Alpes #992');
// Lo muestra
$smarty->display('index.tpl');

index.tpl

<html>
<head>
<title>Información del Usuario</title>
</head>
<body>

Información del Usuario:

Nombre: {$nombre}
Dirección: {$direccion}

</body>
</html>


Output HTML generated

<html>
<head>
<title>Información del Usuario</title>
</head>
<body>

Información del Usuario

Nombre: José Manuel Pardo Pérez
Dirección: Alpes #992

</body>
</html>


Code example

Since Smarty separates PHP from HTML, you have two files:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
   <title>{$title_text}</title>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head> 

<body> {* This is a little comment that won't be visible in HTML source *}

{$body_text}

</body>
</html>

In the business logic code you can configure Smarty to use this template:

define('SMARTY_DIR', 'smarty-2.6.9/' );
require_once(SMARTY_DIR . 'Smarty.class.php');

$smarty = new Smarty;
$smarty->config_dir = SMARTY_DIR;
$smarty->template_dir = './tmpl';
$smarty->compile_dir = './tmpl/compile';
$smarty->compile_check = TRUE;
$smarty->debugging = FALSE; 

$smarty->assign('title_text', 'TITLE: This is the Smarty basic example ...');
$smarty->assign('body_text', 'BODY: This is the message set using assign()');

$smarty->display('basic.htm');

See also

  • PRADO, a component-based web development framework for PHP5
  • CakePHP, a PHP web application framework inspired by Ruby on Rails
  • PHROOT, PHP Rapid Object Oriented Technology Framework Project

External links