230 likes | 607 Views
PHP Template. Outline. Web Template Web Template Classification Introduction to Smarty Smarty Usage Smarty Example References. Web Template. Separate content from presentation on the web design.
E N D
Outline • Web Template • Web Template Classification • Introduction to Smarty • Smarty Usage • Smarty Example • References
Web Template • Separate content from presentation on the web design. • Suitable for websites often require regular content updates, and standardization of appearance.
Web Template Classification • Static web template (Outside server template system architecture)e.g. Dreamweaver, FrontPage
Web Template Classification • Server-side web template (Server-side template system architecture)e.g. SSI(Server Side Includes),Smartybase: ASP,JSP,PHP,Perl
Web Template Classification • Client-side web template (Distributed template system architecture)e.g. Ajax,RIA (Rich Internet application: JavaScript,Flash Player,ActiveX Controls,Java applets,SVG)
Introduction to Smarty • Smarty is a server-side template system. • Separate application logic and business logic from presentation logic • 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 • Smarty requires a web server running PHP 4.0.6 or later. • PHP HTML CSS … • Concept of Template • Organized file groups
Smarty Usage • 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=big5"> <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/%%179/%%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=big5"> <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 *} • Variables • Template variables start with a $dollar sign. • {$foo} {$foo[4]} {$foo.bar} • Functions & Attributes • {funcname attr1="val" attr2="val"}. • Math • Math can be applied directly to variable values.
Custom Functions • {html_image file="masthead.gif"} • <img src="masthead.gif" border="0" height="48" width="256"> • {html_link type="article" id="abc123" text="Fire takes out Hotel"} • <a href="/display_article.php?id=abc123"> 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 <br> {/if} {if $edit_flag} <input type=checkbox name=edit value="y"> edit <br> {/if}
Other Templating Solutionsphp - based • a rudimentary way of substituting variables into templates • limited form of dynamic block functionality • Examples • TemplateTamer http://www.templatetamer.com/ • PHPTAL is a XML/XHTML template library for PHP. http://phptal.motion-twin.com/ • FastTemplate Another template engine for php http://www.thewebmasters.net/php/FastTemplate.phtml