Book Image

Smarty PHP Template Programming and Applications

By : Hasin Hayder, Joao Prado Maia, Lucian Gheorghe
Book Image

Smarty PHP Template Programming and Applications

By: Hasin Hayder, Joao Prado Maia, Lucian Gheorghe

Overview of this book

<p>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.</p>
Table of Contents (18 chapters)
Smarty PHP Template Programming and Applications
Credits
About the Authors
About the Reviewer
Preface
Index

Dynamically Caching Template Sections


Smarty’s caching features are very interesting, but the options outlined above are still pretty inflexible. Another interesting feature is the ability to check whether a given template is already cached or not with the is_cached function. This is important because it allows the programmer to only execute code when strictly necessary. For instance, you could decide whether to run an expensive database query or not based on whether the given template is still available on Smarty’s cache. The following example illustrates this feature:

<?php
include_once(‘libs/smarty.class.php’);
$smarty = new Smarty;
$smarty->caching = 1;

// only run this SQL query if necessary
if (!$smarty->is_cached(‘templates/example4.tpl’)) {
    $stmt = “SELECT
                user_id,
                user_full_name
             FROM
                user
             WHERE
                user_name=’” . addslashes($username) . “’”;
    $result = mysql_query($stmt, $conn)...