Smarty (template engine): Difference between revisions
Ucanlookitup (talk | contribs) revert to Revision as of 16:01, 11 September 2006 by 217.83.79.214 |
tagged to check POV |
||
Line 1: | Line 1: | ||
{{dablink|For the candies of the same name, please see [[Smarties]]}} |
{{dablink|For the candies of the same name, please see [[Smarties]]}} |
||
{{POV-check}} |
|||
{{Infobox Software |
{{Infobox Software |
Revision as of 18:35, 14 September 2006
The neutrality of this article is disputed. |
File:Smarty-logo.gif | |
Developer(s) | Monte Ohrt, Messju Mohr |
---|---|
Stable release | 2.6.14
/ 28th May 2006 |
Repository | |
Type | Template Engine |
License | LGPL |
Website | smarty.php.net |
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
- regular expressions
- foreach
- if
- nl2br - For example {$variable|nl2br}
and many more features. Other template engines also support these commands in templates.
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 *} <p>{$body_text}</p> </body><!-- this is a little comment that will be seen in source --> </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
- Smarty Home Page - official site.
- Download
- Smarty Resources list
- Forum - other languages
- Smarty users knowledge base Wiki
- PHP Templating with Smarty by Cezar Floroiu - Smarty tutorial
- Smarty vs. XML/XSLT - from DevPapers.com by Sergey Makogon
- Article "Templates, Gift from Heaven or Syntactic Junk Food?" by Nathan Oostendorp
- Chapter from Smarty PHP Template Programming and Applications book.
- Smarty Cheat Sheet Smarty Cheat Sheet for Templates Designers and Programmers
- Timestretch: PHP, MySQL, and Smarty Programming - Also see the PHP2 page for more.
- CakePHP