1 / 17

PHP Template

Learn about Smarty, a server-side template system that separates content from presentation, making web design easier and more efficient. This guide provides an introduction to Smarty, its usage, examples, and references.

marvinl
Download Presentation

PHP Template

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Template

  2. Outline • Web Template • Web Template Classification • Introduction to Smarty • Smarty Usage • Smarty Example • References

  3. Web Template • Separate content from presentation on the web design. • Suitable for websites often require regular content updates, and standardization of appearance.

  4. Web Template Classification • Static web template (Outside server template system architecture)e.g. Dreamweaver, FrontPage

  5. Web Template Classification • Server-side web template (Server-side template system architecture)e.g. SSI(Server Side Includes),Smartybase: ASP,JSP,PHP,Perl

  6. 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)

  7. 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.

  8. Requirements • Smarty requires a web server running PHP 4.0.6 or later. • PHP HTML CSS … • Concept of Template • Organized file groups

  9. 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');

  10. 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/"; ?>

  11. 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>

  12. Smarty Example <?php // test.php require “smarty_start.php"; $tpl->assign("title", ”سایت"); $tpl->assign("content", ”خوش آمدید"); $tpl->display('test.tpl'); ?>

  13. 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>

  14. 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.

  15. 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>

  16. 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}

  17. 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

More Related