PHP Template Outline l Web Template Classification l
PHP Template
Outline l Web Template Classification l Introduction to Smarty l Smarty Usage l Smarty Example l References
Web Template l Separate content from presentation on the web design. l Suitable for websites often require regular content updates, and standardization of appearance.
Web Template Classification l Static web template (Outside server template system architecture) e. g. Dreamweaver, Front. Page
Web Template Classification l Server-side web template (Server-side template system architecture) e. g. SSI(Server Side Includes), Smarty base: ASP, JSP, PHP, Perl
Web Template Classification l Client-side web template (Distributed template system architecture) e. g. Ajax, RIA (Rich Internet application: Java. Script, Flash Player, Active. X Controls, Java applets, SVG)
Introduction to Smarty is a server-side template system. l Separate application logic and business logic from presentation logic l l l Designers can't break application code, and the code will be tighter, more secure and easier to maintain. Errors in the templates are as simple and intuitive as possible for the designer. Designers can modify or completely redesign without intervention from the programmer. Programmers can maintain the application code without disturbing the presentation layer.
Requirements l Smarty requires a web server running PHP 4. 0. 6 or later. l PHP HTML CSS … l Concept of Template l Organized file groups
Smarty Usage l l l Step 1. Add Smarty to your site include "Smarty. class. php"; Step 2. Create a Smarty object $tpl = new Smarty(); Step 3. Configure Smarty object. $tpl->template_dir = __SITE_ROOT. "\templates\"; $tpl->compile_dir = __SITE_ROOT. "\templates_c\"; $tpl->config_dir = __SITE_ROOT. "\configs\"; $tpl->cache_dir = __SITE_ROOT. "\cache\"; Step 4. Assign site variable $tpl->assign('title', 'Ned'); Step 5. Call display function $tpl->display('test. tpl');
Smarty Usage (2/2) <? php // smarty_start. php define(‘__SITE_ROOT’, ‘d: /appserv/web/demo’); include __SITE_ROOT. “/class/Smarty. class. php"; $tpl = new Smarty(); $tpl->template_dir = __SITE_ROOT. "/templates/"; $tpl->compile_dir = __SITE_ROOT. "/templates_c/"; $tpl->config_dir = __SITE_ROOT. "/configs/"; $tpl->cache_dir = __SITE_ROOT. "/cache/"; ? >
A template file <!-- test. tpl --!> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=big 5"> <title>{$title}</title> </head> <body> {$content} </body> </html>
Smarty Example <? php // test. php require “smarty_start. php"; $tpl->assign("title", ” ; )"ﺳﺎیﺖ $tpl->assign("content", ” ; )"ﺧﻮﺵ آﻤﺪیﺪ $tpl->display('test. tpl'); ? >
Intermediate and temporary file templates_c/%%1798044067/test. tpl. php: <? php /* Smarty version 2. 6. 0, created on 2003 -12 -15 22: 19: 45 compiled from test. tpl */ ? > <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=big 5"> <title><? php echo $this->_tpl_vars['title']; ? ></title> </head> <body> <? php echo $this->_tpl_vars['content']; ? > </body> </html>
Basic Syntax Comments {* this is a comment *} l Variables l l Functions & Attributes l l Template variables start with a $dollar sign. {$foo} {$foo[4]} {$foo. bar} {funcname attr 1="val" attr 2="val"}. Math l Math can be applied directly to variable values.
Custom Functions l {html_image l file="masthead. gif"} <img src="masthead. gif" border="0" height="48" width="256"> l {html_link type="article" id="abc 123" text="Fire takes out Hotel"} l <a href="/display_article. php? id=abc 123"> Fire takes out Hotel</a>
Logic in the Template {if $smarty. session. user and ( $user_type eq "editor" or $user_type eq "admin" )} <input type=checkbox name=edit value="y"> edit {/if} {if $edit_flag} <input type=checkbox name=edit value="y"> edit {/if}
Other Templating Solutions php - based l l a rudimentary way of substituting variables into templates limited form of dynamic block functionality l Examples l Template. Tamer http: //www. templatetamer. com/ l PHPTAL is a XML/XHTML template library for PHP. http: //phptal. motion-twin. com/ l Fast. Template Another template engine for php http: //www. thewebmasters. net/php/Fast. Template. phtml
- Slides: 17