Smarty PHP Template Programming and Applications

By Hasin Hayder , Joao Prado Maia , Lucian Gheorghe
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Introduction to Smarty

About this book

Smarty is a templating engine for PHP. Designers who are used to working with HTML files can work with Smarty templates, which are HTML files with simple tags while programmers work with the underlying PHP code. The Smarty engine brings the code and templates together. The result of all this is that designers can concentrate on designing, programmers can concentrate on programming, and they don't need to get in each others way so much. Even if you are developing a site on your own, Smarty is a powerful way to make your code clearer to you and others, as well as easier to debug and modify later.

Publication date:
April 2006
Publisher
Packt
Pages
256
ISBN
9781904811404

 

Chapter 1. Introduction to Smarty

Today, websites are past the level of presenting summary and contact data for companies and projects. With the evolution of the Internet, most websites are now stand-alone businesses rather than summary presentations of companies. More than that, complex applications’ developers have embraced the idea of using websites as interfaces for their software.

For more than ten years, PHP has been one of the best choices for web developers being the most complete open-source web programming language. With growth of the complexity of PHP applications, a new problem that was raised was, how to separate programmers’ code (PHP) from designers’ code (HTML), or better phrased, how to separate the content from presentation.

Smarty, a PHP templating system, was born to solve this problem.

 

Templating Systems


The basic functionality of a templating system is introducing a way of separating presentation from content with very little interaction between programmers and designers. For example, the design of a website may look like this:

The designers build the face of the website with pictures, text styles, tables, and so on. They build templates for how to arrange the content in each page.

The only information the designers need is how the site should look and whether they need to layout content like news, products, jokes, and so on.

On the other hand, programmers code the application using PHP to do data manipulation (business logic). They are not interested in how the website looks (colors, pictures, text styles) or where the content is laid out on the page. All they need to do is pass the content to the templates using variable names they agree upon with the designers.

This is roughly how template systems work. Starting from this basic functionality, every template system has a set of features that makes it easier for both designers and programmers if they are well documented in the template system they use.

This book is intended to show you how to use all the features offered by Smarty in the optimum way.

Why Use a Templating System?

Building a website is similar to creating software. It can be done by building it from scratch, just starting to write code to see what you can come up with at the end and solving problems on the way; or it can be done by differentiating the project’s components into separate layers and building the website taking these layers into consideration. This is called multi-tier or multi-level software architecture. If the website is a small one containing only a few lines of PHP code, the first option might work. Just create a few tables in a database, start coding with PHP, maybe create a few banners, generate a few tables and debug every step. People like it this way—fast and easy.

When it comes to more complicated web-based projects such as web portals, e-commerce sites, ERP systems, and so on, the first option may work but in most cases it can very difficult to achieve anything by coding away. Besides, with the market being so crowded with web projects, in order for your website to stand a chance, you have to have a great layout which is appropriate for the type of project you are building and of course, stable code tested in all situations. That’s why people involved in building websites specialize either in designing great layouts (web designers), or in programming very well (web programmers). Not often will you see a good web programmer creating great designs, or a good web designer writing good, bug-free code.

A software company that creates websites usually has a design department and a programming department, and most medium to large web projects always have a programmer as well as a designer working on them.

Programmers have different ways of coding applications and if they use only PHP without a template engine, the designer will need to be familiar with every programmer’s way of arranging the code, naming variables, and so on. Also, the programmer will need to understand the designer’s templates and generate the code to print HTML code from PHP where the designer wants it.

Let’s take the following example:

for ( $col = 1; $col < $i; $col++ ) {
	print “<tr> <td> $procesid[$col]</td> <td>$data[$col] </td> <td>$value[$col]</td> </tr>”; }

This is PHP code that prints the values of three arrays in a table. Even a simple example like this one is hard to read and to arrange in a web page exactly as desired.

With Smarty, the PHP syntax doesn’t contain print. Instead, the programmer passes these arrays to the designer by assigning them to Smarty templates. Then it’s the designer’s job to make them look good in the web page without worrying about the PHP code. This is one big benefit about using Smarty, and we will learn in this book how this is done.

The Smartness of Smarty

Smarty allows designers and programmers to interoperate more effectively and not worry about each other’s work. The designer builds the templates for the web page layout and extracts the data from the PHP files that the programmer has created. The programmer passes data to the templates without having to generate HTML code. This way, everyone is happy and more efficient because they all do the job they are good at.

Let’s think about an e-commerce site that sells laptop computers. The manufacturer’s name, the model number, characteristics, and price are content elements that will be stored in a database and displayed to the visitor.

Smarty makes the job of the designer as well as the programmer very easy. The key tasks performed by them can be listed as follows.

The Programmer’s tasks:

  • Extract database elements with a simple query on the database.

  • Validate and manipulate the data by performing business logic on it.

  • If needed, change the data access methods and the business logic without interfering with the designer’s work. For example, the whole system could migrate from MySQL to PostgreSQL without the designer making a single change.

The Designer’s tasks:

  • Create HTML designs without affecting or jeopardizing the programmers PHP code. The designer only needs to be concerned with placing the content elements that the programmer has agreed to provide.

  • Make changes to the design without consulting or interfering with the programmer’s work.

  • Stop worrying about technical changes to the site breaking the way that the site appears to viewers.

In the above example, we see that tasks are assigned to people involved in the project depending on the parts they are working on—presentation, business, and data access.

Later in this chapter we’ll have a look at the process of building software applications, including websites, using multi-tier architecture.

Smarty Alternatives

For most people who need a PHP templating system, Smarty is the natural choice mainly because of its popularity. As you probably know, Smarty is not the only PHP template engine in the market, but it is the most complete and reliable one.

From the variety of other PHP template engines, we would mention the following:

Most of these PHP templating systems have a few advantages like speed and ease of use, mainly because they are not as complex as Smarty. However Smarty has more features than most of its competitors and it can be customized to be pretty fast.

In my opinion, the true competitor of Smarty is PHP Savant, which is a powerful but lightweight object-oriented template system for PHP. Savant includes a lot of Smarty’s features.

However, there is one big difference between Smarty and Savant that can be either an advantage or a big disadvantage for Savant, depending on which point of view you are looking at. The difference is that templates in Savant are written in PHP and not in another language like Smarty introduces. This can be:

  • An advantage for Savant—Designers work with PHP as a template language, they don’t have to learn another templating language.

  • A disadvantage for Savant and a huge advantage for Smarty concerning security. Savant doesn’t compile the templates into PHP as the templates themselves are PHP files. This way, designers have access to the full power of PHP. If the designer is not a trusted one Savant is out of the question and Smarty is the answer.

In most cases, designers are not trusted to have access to the systems where web pages run, therefore Smarty is still the best templating system for PHP. Savant is nice, but it almost totally lacks security, while other template engines have only some parts of the functionality Smarty has.

 

A Rough Guide to the Software Design Process


The process of building a website involves a few steps that may vary from project to project, but generally it is the same for most projects.

The first step is to establish the customer needs, on which you design a database. These needs are later transcribed in a spoken agreement or preferably in a document. This document should contain a description of the desired functionality of the software as well as the definition of the database tables with explanations for every column. Once the customer agrees on this document and maybe makes a few changes, you can start the actual process of building the website.

If you are your own customer this is not the case, otherwise it is my strong opinion that you should do this. For one of my first projects, I skipped the document part and it led to a total fiasco. The customer wanted more and more and did not want to pay more for it, but if the document had existed he would have had no choice.

The next step is to create and show a few layouts to the customer. This is the designer’s job. He or she will work with the customer and come up with a design that the customer likes. After the customer finalizes a design, the actual building of the website begins. When the project is finished, the customer has a testing period in which a few modifications are made or some bugs cleared, and it’s done.

This is beautiful in theory, but in real life, programmers speak languages like PHP and SQL and designers speak in HTML and CSS, and they often tend to have a few differences.

Working in Teams: Layers and Separation of Concerns

When human interactions might fail, it’s preferable to separate the work as much as possible. Multi-tier software architecture should do the job. The most commonly used architecture is one with three tiers or layers. These are:

  • Presentation Logic Layer

  • Business Logic Layer

  • Data Access Layer

Separating these layers in project development cycles allows you to achieve rapid application development with project maintainability in mind.

In the three-tier architecture, the interaction between layers is shown in the following diagram:

Starting from the bottom, at the Data Access Layer we find both the data and the ways to extract the data that we want to show to the user. The Data Access Layer may contain:

  • A database (MySQL, PostgreSQL, MSSQL, and so on) and the SQL language used to extract data from the database

  • Files that store data and PHP functions (or other languages) that parse the files

  • Data acquisition software (for example, a thermometer on the parallel port)

Now that we’ve extracted the data, we need to manipulate it in order to get the results we need to display. Data manipulation and validation is done at the Business Logic Layer, which may contain:

  • Data validation based on the business plan (for example, list only in-stock items)

  • Data manipulation according to the business plan (for example, discounts, stock liquidations, and so on)

  • Functions and formulas to calculate things like shipping expenses, and so on.

The Presentation Logic Layer is where the web page layout—how data from the Business Logic Layer is arranged in the web page—is defined. This is done using:

  • Web page templates

  • Text/CSS styles

  • Images, banners, and menu styles

Without a templating engine at the Presentation Logic Layer, we find HTML and PHP creating the layout with pure HTML and generating HTML code from PHP. In this case, we cannot divide the Presentation Logic and Business Logic layers into two separate layers, making the work of designers and programmers very difficult for complex software projects. That’s where Smarty comes in.

 

Smarty—The Ultimate Templating System for PHP


Now that we’ve decided to make our life easier by considering multi-tier architecture for our website, we should take a look at what Smarty can do, as well as what it can’t do for us.

Smarty is not built for separating HTML from PHP; instead, its primary goal is to separate the application logic from presentation logic. If Smarty’s goal was to separate PHP from HTML then the presentation layer would contain no logic; but Smarty templates can contain logic, as long as it is to be used for presentation only.

This may seem like it breaks the rules of rigid separation between layers and tasks, but there are good practical reasons for it. A simple example would be an e-commerce site with products displayed in four columns:

  • Without Smarty: If our presentation layer doesn’t contain logic, we need to modify the business logic to retrieve the products in four arrays.

  • With Smarty: Using logic in the templates, the programmer just passes the products to the templates in a single array and the designer arranges the products in the page as he or she desires.

Smarty offers an interface to pretty much all of PHP, so PHP can be included in the templates, but it’s recommended to leave most of PHP code at the business logic layer. Fortunately, Smarty’s logic is generally much simpler to use than PHP and designers do not need to become programmers in order to incorporate presentation logic into their Smarty designs.

Is Smarty Fast?

Template systems introduce another level of processing in the application, so we might suspect that Smarty applications will run slower than plain PHP equivalents. Basically, we have a new pseudo-scripting language (the template engine’s tags) inside our scripting language (PHP).

Smarty is extremely fast by doing what is called template compiling. That means that it reads the templates, creates PHP scripts from them and includes the PHP files, resulting in a single PHP script that is compiled by the PHP engine, which is pretty fast. The beauty of the process is that the templates are parsed only once by Smarty, and only templates that are modified are compiled again. This is done automatically by Smarty and results in a fast compilation done by the PHP engine with very little overhead from Smarty.

If you are concerned about the performance of your site, Smarty has built-in caching support that can speed things up, especially on websites that have content that is not modified very often. You can cache all the content of a web page or only some of it and you can specify for how long Smarty should keep the content cached. This will be explained in more detail in Chapter 9.

Since Smarty does template compiling resulting in a PHP script, there is no reason why we should not use a PHP compiler cache solution like PHP Accelerator or Zend Cache.

PHP Accelerator and Zend Cache have no problem with Smarty’s output and cache the PHP scripts produced by Smarty very well, so if our main concern is performance we should use one of these caching solutions combined with Smarty’s built-in caching support.

Is Smarty Secure?

We concluded that Smarty is fast, but we need to know about its security, which according to me is most important for any website.

By default, when using Smarty you are theoretically as secure as you are when using only PHP. This means that by using Smarty you can’t be less secure than using PHP only; but Smarty has some features to improve security in case this is needed.

When a site is built by a programmer working with a designer without using Smarty, the designer has access to the application and can modify all the PHP scripts. This is not good for security because a designer with bad intentions can breach the security of the system very easily when he or she has all the power of PHP in his or her hands.

Smarty comes with some built-in security features for situations where unreliable parties edit templates. When security is set on templates, a designer who modifies templates via an unsecured connection like FTP is unlikely to gain access in the system.

Using Smarty security features, you can:

  • Allow or deny usage of PHP code in the templates

  • Allow only a set of PHP functions as variable modifiers

  • Restrict the folders from which templates can be included

  • Restrict the folders from which local files can be fetched by templates

My strong advice is to always think about security. Think about having a database with credit card details and your designer including unsafe PHP code in one web page. This would result in a disaster for your business.

 

Smarty’s Main Features


Smarty offers both designers and programmers tools to optimize their work. Reading this entire book you will find out about all these wonderful features and will see how great Smarty is. Let’s preview some of these features and look at the reasons why they should or should not be used.

Variable Modifiers

When displaying content on a website, you might want to change some of it depending on the time or visitor’s origin. For example, we might want to show a date in different formats. Using Smarty, there’s no need for the programmer’s intervention for this. He or she will just pass the date in the proper variable to the template, and the designer can format the date however he or she desires.

Also, the designer can upper-case or lower-case a variable, truncate a text block, add spaces between characters, and so on and it’s all done at display-time with Smarty.

Think about displaying a product category as a small title in uppercase and with spaces between the word’s characters (for example, ‘C A R S’). In the database, the corresponding column contains the word ‘cars’. With Smarty you don’t have to ask the programmer to change it to ‘C A R S’. When he or she passes the variable to the template, you can do that with proper variable modifiers at display-time, and again, if you want to modify the way you display the product category later (for example, ‘Cars’), this doesn’t require the programmer’s intervention.

You will learn more about variable modifiers in Chapter 5.

Template Functions

While going through Smarty’s syntax you will discover another great thing about Smarty—Template Functions. Think about designing a large form with many HTML dropdowns. The old-fashioned way to do this is to write tags for every drop-down menu. With Smarty, you can simply write a function for dropdowns and call it every time you need to display one, making things simpler and faster. This was an easy example, but you can save a lot of time writing functions for displaying content in manners that you repeat frequently.

You will learn more about template functions in Chapter 6.

Debugging

At every step of building a software application, programmers and designers need debugging to easily correct their work.

Think how much time you can lose if you misspell a variable and don’t have debugging tools. In my experience as a programmer, that’s the worst scenario, even worse than having mistakes in your algorithm.

Even if you can debug with PHP, Smarty provides a debugging console for correcting Smarty-related errors. This is a very powerful debugging tool, as you are informed of all the included templates, assigned variables and configuration file variables for the current template.

With Smarty you can modify the format of the debugging console, so you can highlight the things you find important in debugging. In addition, you can dump the output of the debugging console to the page using a Smarty function in the template, but since Smarty’s functions are executed at run time, you can only see the variables involved not the templates included.

You will learn more about debugging with Smarty in Chapter 7.

Plug-ins

One very important thing for a business that develops software is the possibility to reuse code. This saves time and money when new projects are built that contain functionality related to other projects created before, and this is what really makes a software business profitable. Using code from one application in another with copy and paste requires modification of variables, function names, retesting and careful integration with the new application, but is faster rather than rewriting the piece of code.

However, there is a better method for reusing code—using Plug-ins. When building a site, identify functionalities that can be reused even in the same project and create them as plug-ins. This way, after building a few sites, you will have a portfolio of plug-ins that run well and can be included without modifications in any new project saving a lot of time and work. For doing this, we need to have plug-in support in the software that we use to build our project.

Smarty has a plug-in architecture that is used for most of its customizable functionality. With Smarty, you can write plug-ins for functions, compiler functions, block functions, modifiers, resources, inserts, prefilters, postfilters, and output filters.

A lot of software out there with plug-in architecture loads all plug-ins before compiling. This is not necessarily wrong, but Smarty’s designers kept performance in mind and made the plug-ins load only when they are invoked in a template. More than that, a plug-in invoked once in a template is only loaded once, even if there are more instances of Smarty requesting the same plug-in.

For example, when creating an e-commerce site, you can create plug-ins for the shopping basket functionality or filters for currency conversion that you can use later in other e-commerce projects you build, without having to write the same code again but being paid similar amounts of money.

You will learn more about plug-ins in Chapter 10.

Filters

Disciplined designers may write a lot of comments in their templates, resulting in a big file after compilation. Smarty’s developers thought about this and included a set of filters to solve this problem. Smarty has prefilters, which are PHP functions that the templates are run through before they are compiled. With prefilters you can modify anything in the templates before template compilation. For example, if you want to remove all the comments you can do that very easily using a prefilter.

However, if you want to add some comments to the compiled templates you can do that with postfilters. Smarty’s postfilters are PHP functions that the templates are run through after they are compiled.

You can also filter out the content of a template by using output filters, which are PHP functions used for filtering the output of a template when it is executed, for example replacing banned words with *, hiding email addresses, and so on. With these powerful filters, the programmer has complete control over the templates.

You will learn more about prefilters, postfilters, and output filters in Chapter 11.

 

Smarty Internals


From the designer’s point of view, Smarty is a new simple and powerful scripting language that can be used together with HTML to simplify the work with the programmer. Also, the new scripting language that Smarty introduces to designers offers them ways to simplify their work.

A very eloquent example to my previous statement would be the following syntax:

<select name=”Employee”>
  {HTML_options options=$names}
</select>

The first and last lines are HTML but the line in the middle is Smarty. This line creates select options with values in the $names array that is received from the programmer. In this way, the designer only has to ask the programmer to pass employee names in an array named $names (minimum interaction). Instead of writing <option></option> a lot of times, the designer uses a Smarty function that simplifies his or her work.

Instead of placing the above example in an HTML file, the designer places it in a file with the .tpl extension and tells the programmer the full name of the template (.tpl) file.

This book has a section called Smarty for Designers in which designers will learn this new scripting language with useful examples, tips, and tricks from the authors’ experience.

From the programmer’s point of view, Smarty is a large PHP class with variables and methods. The name of the class is Smarty, so to assign a Smarty class variable, the programmer must do:

$smarty = new Smarty;

The Smarty class offers programmers more efficient ways of interacting with designers. For example, say the designer requested all employee names in a variable named $names. After extracting the names from the database in an array, the programmer passes these to the designer. This can be done by simply adding:

$smarty->assign(‘names’,$names_array);

For the programmer, one of the best things about Smarty is that there’s no need to have HTML code in the PHP files. HTML code is in the .tpl files which are displayed using one of the Smarty class methods:

$smarty->display(“index.tpl”);

The second section of this book is called Smarty for Programmers. Reading this section, programmers will learn about the Smarty class and how to use it in the best way.

 

Installing and Configuring Smarty


Smarty is distributed under the GNU Lesser General Public License, which is the successor for the GNU Library General Public License. The difference between the Lesser GPL and the old Library GPL is that the software is less protected in the Lesser GPL, meaning that the libraries distributed under this license are free software and can be used to create commercial software. The old Library GPL allows you to use the software distributed under its license only to create free software.

The point is that Smarty is a free tool, and we can create commercial software using Smarty without paying for it.

If you want to make sure you are 100% legal, you can read the entire Lesser General Public License at http://www.gnu.org/copyleft/lesser.html.

Step 1: Obtaining Smarty

You can download the latest stable Smarty source code from Smarty’s website at http://smarty.php.net/download.php. Always make sure to download the latest release as it contains bug fixes for all the previous versions.

Smarty is, in fact, a library for PHP and its requirements are very simple: a web server with PHP 4.0.6 or later. Apache can be used as a web server since it’s the most popular choice, but it’s not the only one. Instructions on installing Apache can be found at http://www.apache.org.

After downloading Smarty from the web server and uncompressing the archive, we have a folder with the current Smarty distribution. In the distribution root you will find a folder named libs, which contains the following files and folders:

  • smarty.class.php

  • smarty_Compiler.class.php

  • config_File.class.php

  • debug.tpl

  • \internals

  • \plug-ins

All these files are needed to run Smarty applications and the PHP files are not to be edited.

Step 2: Configure PHP to Find the Smarty Libraries

The simple method of installing Smarty is to add the libs folder path to the include path in the php.ini file.

On Windows

A very simple but effective way to configure Smarty would be to first create a folder, say mycode in the document root of your Apache installation. Next, copy the libs folder from the Smarty distribution to the mycode folder (for example, C:\apache\htdocs\mycode\libs\).

After this, you need to edit the php.ini file and add include_path = “.;C:\apache\htdocs\mycode\libs\” where C:\apache\htdocs\mycode\libs\ represents the location of your libs folder.

On Linux

For example, let’s say you’ve extracted the distribution of Smarty you downloaded in /usr/local/Smarty.

Just edit the php.ini file and add include_path = “.:/usr/local/Smarty/libs” and it’s all set. You can also copy the libs folder to any location of the include_path in php.ini if you don’t want to edit the file.

This is the easiest way to install Smarty. The point is that you need to load the Smarty.class.php file in every PHP file that uses Smarty templates, so if you have it in one folder of the PHP include path, you can create a Smarty instance for it by:

<?php
require(‘Smarty.class.php’);
$smarty = new Smarty;
?>

After setting everything up, you have to change the permission of your templates_c folder, which resides in your script path. That means your code and the templates_c folder must remain in the same directory. Now using FTP just change the permissions of your templates_c folder to 777 (rwxrwxrwx).

You can perform this action using the following shell command:

chmod –R 0777 templates_c

An Alternative to Step 2: Using Smarty without Having Full Access to the System

If you don’t have access to modify the php.ini file or to copy Smarty libs in one of the folders of the php include_path, you can still use Smarty by setting the Smarty libs directory in the PHP files that use Smarty.

Smarty uses a PHP constant called SMARTY_DIR, which doesn’t need to be set in the first case when you edit the php.ini file to include the Smarty libs. However, you can manually set the SMARTY_DIR to point to the same path as the Smarty libs.

  • For Windows:

    <?php
    define(‘SMARTY_DIR’, ‘C:\apache\htdocs\mycode\libs\);
    require(SMARTY_DIR . ‘Smarty.class.php’);
    $smarty = new Smarty;
    ?>
  • For Linux:

    <?php
    define(‘SMARTY_DIR’, ‘/usr/local/Smarty/libs’);
    require(SMARTY_DIR . ‘Smarty.class.php’);
    $smarty = new Smarty;
    ?>

Note

Note that since there are so many ways of using Smarty. In this book, we will assume that you copied the libs folder from Smarty’s distribution root to the mycode folder within your Apache document root and added its location to the php.ini file. If you are using Linux, we also assume that you have changed the permissions of the templates_c folder to 777 or rwxrwxrwx.

Step 3: Set Up Smarty for Your Application

After we are sure that we can find the smarty.class.php file, we need to set up the folders for our application. Every Smarty application needs four folders, which are named by default templates, templates_c, configs, and cache.

Each of these folders can be defined by the Smarty class proprieties $template_dir, $compile_dir, $config_dir, and $cache_dir. Every Smarty application should have its own folders.

For Linux, Smarty needs write access to $compile_dir and $cache_dir, so we have to allow the web server user write access to these folders. Look at the httpd.conf (Apache web server configuration) file and see the user and group used to run Apache. Usually the user and group is nobody, so I will use it in my example. Let’s say we have the document root of our web server in /www/htdocs. We need to set the file permissions of /www/htdocs/templates_c and /www/htdocs/cache to allow write access for user nobody.

chown nobody:nobody /www/htdocs/templates_c
chown nobody:nobody /www/htdocs/cache
chmod 770 /www/hdocs/templates_c
chmod 770 /www/htdocs/cache

Step 4: Verifying the Installation

To make sure the installation works, copy the demo folder from the Smarty distribution root to the document root of your web server (for example, c:\Apache2\htdocs), edit the index.php file to make sure you have require(‘smarty.class.php’); and type http://localhost/demo/index.php in your web browser. You should see a demo page with some of Smarty’s capabilities and the debugging console. Make sure your browser allows pop-ups.

Smarty Development Versions on CVS

By the way, if you want to play with features in development for future versions of Smarty, then the PHP CVS server hosts the latest version of Smarty (the unstable one) and most of the old releases. CVS can be downloaded from http://ccvs.cvshome.org/servlets/ProjectDocumentList.

To download Smarty from the PHP CVS server, first you have to login into the server with the command:

cvs -d :pserver:[email protected]:/repository login

using phpfi as the password.

To download the current Smarty tree just type:

cvs -d :pserver:[email protected]:/repository co smarty

or any stable version of Smarty by using the following command:

cvs -d :pserver:[email protected]:/repository co -r smarty_X_Y_Z smarty

where X_Y_Z is the corresponding version number, for instance using Smarty_2_6_10 in the previous command line will download Smarty release 2.6.10 in the smarty folder.

Note

Note that using the current Smarty distribution from the PHP CVS server in production is not recommended at all. It is an unstable version used for development and if you want to use Smarty for building websites you should use the latest stable version.

 

Upgrading a Smarty Site


Smarty is backward compatible, so when you need to upgrade a Smarty site all you need to do is:

  • Get the latest Smarty distribution as described in Step 1 earlier.

  • Replace the old content of the libs folder where you installed it with the content of the libs from the new distribution’s root.

  • Make sure you don’t have the old file Smarty.class.php in any other PHP include path.

 

Summary


In this chapter, you learned that using multi-tier software architecture makes our life easier. Smarty’s objective is to separate the Business Logic from the Application Logic. Smarty is fast and secure because of the way templates are parsed, which is template compiling. It has prefilters, postfilters, and output filters which give the programmer complete control of template output and content. By creating resource handles, you can use templates from any source that you can access with PHP. Smarty has a plug-in architecture so you can use plug-ins for most of its functionality. It also includes a very powerful debugging tool. You also learned how to install Smarty on Windows and Linux.

About the Authors

  • Hasin Hayder

    Hasin Hayder graduated in Civil Engineering from the Rajshahi University of Engineering and Technology (RUET) in Bangladesh. He is a Zend-certified Engineer and expert in developing localized applications. He is currently working as a Technical Director in Trippert Labs and managing the local branch in Bangladesh. Beside his full time job, Hasin writes his blog at https://hasin.wordpress.com, writes article in different websites and maintains his open source framework, Orchid. Hasin lives in Bangladesh with his wife Ayesha and his son, Afif.

    Browse publications by this author
  • Joao Prado Maia

    João Prado Maia is Lead Software Developer with Alert Logic, Inc. and was previously with MySQL AB as the lead developer behind Eventum, an issue tracking system, and MySQL Network, a subscription product for everything related to MySQL services. He has been working with PHP, Smarty, and PEAR for several years, and maintains phpbrasil.com, one of the most popular PHP-related community sites in Brazil. He is also interested in fostering a community of PHP developers in Houston by organizing the Houston PHP Users Group at http://www.houstonphp.org.

    Browse publications by this author
  • Lucian Gheorghe

    Lucian Gheorghe has just joined the Global NOC of Interoute, Europe's largest voice and data network provider. Before Interoute, he was working as a senior network engineer for Globtel Internet, a significant Internet and Telephony Services Provider to the Romanian market He has been working with Linux for more than 8 years putting a strong accent on security for protecting vital data from hackers and ensuring good quality services for internet customers. Moving to VoIP services he had to focus even more on security as sensitive billing data is most often stored on servers with public IP addresses. He has been studying QoS implementations on Linux to build different types of services for IP customers and also to deliver good quality for them and for VoIP over the public internet. Lucian has also been programming with Perl, PHP and Smarty for over 5 years mostly developing in-house management interfaces for IP and VoIP services.

    Browse publications by this author
Book Title
Access this book, plus 7,500 other titles for FREE
Access now