Overview
KB
Learn PHP
PHP techniques
PHP Smarty Functions
PHP Web Email
Secure development
PHP Security Scenario
SSH and SFTP
PHP Smarty
PHP Smarty Modifiers
PHP Smarty Caching
PHP Smarty Misc
CakePHP
Zend Framework
PHP basics

PHP Smarty

Smarty is a template engine: a utility for separating a site's presentation (the HTML et al.) from its functionality (the PHP code). This separation has two main benefits:
  • Cleaner organization. The PHP code and files are not muddled among HTML code and files.
  • Division of labor. Dedicated Web designers and WYSIWYG editors can create a site's look without knowledge of the PHP backend. PHP coders can program without mucking up the HTML.
On top of the benefits of any template engine, Smarty adds its own benefits:
  • Multiple caching levels
  • Support for plug-ins and add-ons
  • Built-in debugger
  • Improved performance

Simple Exercise

creating a new Smarty template
To see how easy it is to work with Smarty, follow these steps:
  1. Begin a new HTML page in your text editor or IDE. NuSphere's PhpED supports a Smarty template file (.tpl) as a common type (see the image at right).
  2. Use HTML, CSS, images, etc., to create the look the page should have.
  3. Insert placeholders for content that should be dynamically generated by PHP. Placeholders should be a variable name surrounded by curly braces (see the image at right).
  4. creating a new Smarty template
  5. Save this file as demotpl.tpl.
  6. Begin a new PHP page in your text editor or IDE.
  7. Include the Smarty class (for this to work, you must have Smarty installed on your server; NuSphere's PhpED installs it automatically):
    include('Smarty.class.php');
  8. Create a new object of type Smarty:
    $page = new Smarty();
  9. Assign the dynamic content to the placeholders:
    $page->assign('placeholder_name', 'content');
    This content could come from a database, the result of a conditional (e.g., reporting upon a form's submission), and so on.
  10. Generate the page:
    $page->display('demotpl.tpl');
  11. Save this file as page1.php, in the same folder as demotpl.tpl (you could also put the template file in a separate folder, then change the $page->display() line so that it contains an absolute or relative path to the template file).
  12. Run the PHP script on your server to see the results (see the image at right).
creating a new Smarty template


Article continues:  next page>>

 Download NuSphere PHP IDE

Download a free trial of the fast PHP EDitor and robust Integrated Development Environment for PHP.

 Buy NuSphere PhpED® now

Best PHP Editor and complete PHP IDE.
NuSphere PhpED 21.0 is available from our online store front.

 Special Team4 Offer

Get 4 copies of PhpED for the price of 3!

Optimum solution for development teams.
PhpED 21.0 Team4
Need more than 4 licenses? Contact Us for more quantity discounts, please use "Ordering/Payment issue" subject on the form.
"To be honest its bloody awesome, I have looked at loads of PHP editors and this is THE only one that actual works straight out of the box!!! Brilliant, well done."
Andrew Breward,
Director of Technology
caboodal.com