Saturday, July 15, 2006 | 1 comment | 
When you discover the Smarty, is almost impracticable to live without it. In this article, I show your functioning and examples of the use of methods. You will notice a profit of general productivity in the development of applications.
Definition of official site:
Although Smarty is known as a "Template Engine", it would be more accurately described as a "Template/Presentation Framework." That is, it provides the programmer and template designer with a wealth of tools to automate tasks commonly dealt with at the presentation layer of an application. I stress the word Framework because Smarty is not a simple tag-replacing template engine. Although it can be used for such a simple purpose, its focus is on quick and painless development and deployment of your application, while maintaining high-performance, scalability, security and future growth.
In other words, Smarty functions of a form that separates interface of the programming logic and has for objective, to facilitate and to improve the development of any application in PHP.
For very being spread out in the entire world, and to be on to the official site of the PHP, the Smarty it has a great community of developers. This aid in the support and quarrel of improvements.
Its last version (until the writing of this article) is the 2.6.14 (changelog), launched in May of 2006.
Beyond the functionalities that I commented in previous article (How to develop better in the Web), I list others:
Download the package of Smarty. Unzip all the folders. Verify a folder "demo", is the demonstration of functioning. Put the folder "libs" inside of the "demo". At final you'll get a folder organization, as shown below. Don't forget to give permission in folder "templates_c".
The default tree of directory of the Smarty is organized in the following way:
This architecture can be modified by the user, remembering that, after modified the names, you it will have to modify some lines of the Smarty.class.php (main class):
Smarty.class.php:
var $template_dir = 'templates'; var $compile_dir = 'templates_c'; var $config_dir = 'configs';
To program using Smarty is still more simple. All archive PHP must point to one template. The access the data base, critical more complex, and other logics are in archive PHP (as before). What dumb here, it is the simplicity of the syntax of templates, facilitating to changes in the future for the Designer or Developer: each one modifies its file.
As the topic suggests, I go direct to the subject. See a very simple example of the use of templates in the Smarty.
index.php:
<?php
/**
* Smarty folder setup in a Constant
*/
define('PATH_SMARTY', '/path/to/smarty');
/**
* Include the main class of Smarty
*/
require PATH_SMARTY.'/Smarty.class.php';
/**
* Instance Smarty and setup properties
*
* compile_check = tell the Smarty to show compile errors
* debugging = open a popup window with all information about the generate page
*/
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
/**
* Assign is the method of Smarty to create a new variable that will be used in the Template file
* syntax: assign->('variable_name', 'value');
*/
$smarty->assign('name', 'Ciro');
$smarty->assign('lastname', 'Feitosa');
/**
* Show the Template
*/
$smarty->display('index.tpl');
?>
index.tpl:
{config_load file=test.conf}
<html>
<body>
{$name} {$lastname}
</body>
</html>
When twirling index.php for the first time, automatically the Smarty will create one cache of the content generated in the /templates_c directory.
In this simple example, you perceive how simple is the syntax used for the Smarty, isn't it?! I'll present now, methods that will have to be used in the archives of templates.
The private variable $smarty, brings options to treat the information comings for methods GET, POST, or for Cookies, Sessions, etc. It sees examples of method calls:
{* display value of page from URL ($_GET) http://www.example.com/index.php?page=foo *}
{$smarty.get.page}
{* display the variable "page" from a form ($_POST['page']) *}
{$smarty.post.page}
{* display the value of the cookie "username" ($_COOKIE['username']) *}
{$smarty.cookies.username}
{* display the server variable "SERVER_NAME" ($_SERVER['SERVER_NAME'])*}
{$smarty.server.SERVER_NAME}
{* display the system environment variable "PATH" *}
{$smarty.env.PATH}
{* display the php session variable "id" ($_SESSION['id']) *}
{$smarty.session.id}
{* display the variable "username" from merged get/post/cookies/server/env *}
{$smarty.request.username}
Thus, in the call of forms you must insert in the field value: $smarty.post.field_name. Example:
form.tpl:
Name: <input type="text" name="name" value="{$smarty.post.name}" /><br />
State: <select name="state">{html_options options=$states selected=$smarty.post.state}</select>
The documentation of the Smarty is excellent. Therefore, I will not try to explain what already it exists, but to quickly show useful functions in day-by-day.
To help in the agreement, I will use forms. I believe that this is the use where more aid, when you uses Smarty. It estimates that you already it has enclosed the class of the Smarty and instance the class (it reviews the example in the sub-topic "Let's go"). They do not observe code validations xHTML, therefore it is not the central idea here.
They are used, mainly, to modify strings. Functions of the type to capitalize, to count characters, to count paragraphs, to change to uppercase letters, to indent lines, etc.
Sees an example of the cat method, that it functions concatenating 2 strings (sees all the variable modifiers methods).
index.tpl:
<html>
<body>
{$nome|cat:" is the owner of this Blog."}
</body>
</html>
Are functions used internally for templates. Loops, conditional (if, else), includes. See the list complete of internal functions.
The conditional functions, as well as in any language, are used when is had a condition. Imagine that you possess a done system with login with session. If the user will have logado, shows the name of it, in case that contrary, shows "Guest". He sees as to make this using templates of the Smarty:
{if $smarty.session.name != ''}
{$smarty.session.name}
{else}
Guest
{/if}
Servants similar of if creating of automatized form had been, some works repetitive. In if treating to forms, she is possible to generate checkboxes, menus drop-downs, radios, and drop-downs menus of date and hour, everything this being able to come of a static or dynamic Array (filled way database, for example).
Imagine that you it has an Array with the country states (in an file .PHP) and desires to generate a menu drop-down in the screen. Uses the html_options method. Beyond this, it exists sets of ten of custom functions.
form.php:
$arrayStates = array(
1 => 'Alabama',
2 => 'Alaska',
3 => 'Arizona',
4 => 'Arkansas',
5 => 'California',
6 => 'Colorado',
7 => 'Connecticut',
8 => 'Delaware',
9 => 'District of Columbia',
10 => 'Florida',
11 => 'Georgia',
12 => 'Hawaii',
13 => 'Idaho',
14 => 'Illinois',
15 => 'Indiana',
16 => 'Iowa',
17 => 'Kansas',
18 => 'Kentucky',
19 => 'Louisiana',
20 => 'Maine',
21 => 'Maryland',
22 => 'Massachusetts',
23 => 'Michigan',
24 => 'Minnesota',
25 => 'Mississippi',
26 => 'Missouri',
27 => 'Montana',
28 => 'Nebraska',
29 => 'Nevada',
30 => 'New Hampshire',
31 => 'New Jersey',
32 => 'New Mexico',
33 => 'New York',
34 => 'North Carolina',
35 => 'North Dakota',
36 => 'Ohio',
37 => 'Oklahoma',
38 => 'Oregon',
39 => 'Pennsylvania',
40 => 'Rhode Island',
41 => 'South Carolina',
42 => 'South Dakota',
43 => 'Tennessee',
44 => 'Texas',
45 => 'Utah',
46 => 'Vermont',
47 => 'Virginia',
48 => 'Washington',
49 => 'West Virginia',
50 => 'Wisconsin',
51 => 'Wyoming'
);
$smarty->assign('states',$arrayStates);
$smarty->display('form.tpl');
form.tpl:
<html>
<body>
<select name="state">{html_options options=$states}</select>
</body>
</html>
Is notable the increase of the productivity using this system of templates. When we work in a team, Designer and Developer with distinct functions, and in this in case that, working in addition together. If the Designer to want to modify one label of a field of the form, for example, will only need to move in template.
For more information, please visit the Smarty official site.
Cirao, goes to conquer the world!!!!!
Good Luck!!!
The comments have been desactived to this Post.
Versão em Português © Copyright 2004-2008 Ciro Feitosa - Todos os Direitos Reservados