www.ilam.ac.irstaffsmozafarphps11Lec14-PHP_Te by yurtgc548


									PHP Template

 Web Template
 Web Template Classification
 Introduction to Smarty
 Smarty Usage
 Smarty Example
 References
    Web Template

 Separate content from
  presentation on the web
 Suitable for websites
  often require regular
  content updates, and
  standardization of
     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),Smarty
    base: 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
       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.

   Smarty requires a web server running
    PHP 4.0.6 or later.

 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
   Step 5. Call display function
             Smarty Usage (2/2)
// 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 --!>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
                   Smarty Example
// test.php
    require “smarty_start.php";
    $tpl->assign("title", ”‫;)"سایت‬
    $tpl->assign("content", ”‫;)"خوش آمدید‬
     Intermediate and temporary file


<?php /* Smarty version 2.6.0, created on 2003-12-15 22:19:45 compiled
   from test.tpl */ ?>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title><?php echo $this->_tpl_vars['title']; ?></title>
<?php echo $this->_tpl_vars['content']; ?>
                    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

{if $edit_flag}
   <input type=checkbox name=edit value="y"> edit
               Other Templating Solutions
                               php - based

   a rudimentary way of substituting variables into templates
   limited form of dynamic block functionality

   Examples
      TemplateTamer

       PHPTAL is a XML/XHTML template library for PHP.

       FastTemplate        Another template engine for php

To top