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
|
To see how easy it is to work with Smarty, follow these steps:
- 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).
- Use HTML, CSS, images, etc., to create the look the page should have.
- 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).
- Save this file as demotpl.tpl.
- Begin a new PHP page in your text editor or IDE.
- 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');
- Create a new object of type Smarty:
$page = new Smarty();
- 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.
- Generate the page:
$page->display('demotpl.tpl');
- 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).
- Run the PHP script on your server to see the results (see the image at right).
|
|
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
|
Special Team4 Offer
Get 4 copies of PhpED for the price of 3!
Optimum solution for development teams.
|
|
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
|
|