PHP Template - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

PHP Template

Description:

Title: PowerPoint Presentation Last modified by: mozafar Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show (4:3) Other titles – PowerPoint PPT presentation

Number of Views:103
Avg rating:3.0/5.0
Slides: 18
Provided by: ilamAcIr
Category:

less

Transcript and Presenter's Notes

Title: PHP Template


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 objecttpl new
    Smarty()
  • Step 3. Configure Smarty object.
    tpl-gttemplate_dir __SITE_ROOT .
    "\\templates\\"
  • tpl-gtcompile_dir __SITE_ROOT .
    "\\templates_c\\"
  • tpl-gtconfig_dir __SITE_ROOT . "\\configs\\"
  • tpl-gtcache_dir __SITE_ROOT . "\\cache\\"
  • Step 4. Assign site variabletpl-gtassign('title',
    'Ned')
  • Step 5. Call display functiontpl-gtdisplay('test.
    tpl')

10
Smarty Usage (2/2)
  • lt?php
  • // smarty_start.php
  • define(__SITE_ROOT, d/appserv/web/demo)
  • include __SITE_ROOT . /class/Smarty.class.php"
  • tpl new Smarty()
  • tpl-gttemplate_dir __SITE_ROOT .
    "/templates/"
  • tpl-gtcompile_dir __SITE_ROOT .
    "/templates_c/"
  • tpl-gtconfig_dir __SITE_ROOT . "/configs/"
  • tpl-gtcache_dir __SITE_ROOT . "/cache/"
  • ?gt

11
A template file
  • lt!-- test.tpl --!gt
  • lthtmlgt
  • ltheadgt
  • ltmeta http-equiv"Content-Type"
    content"text/html charsetbig5"gt
  • lttitlegttitlelt/titlegt
  • lt/headgt
  • ltbodygt
  • content
  • lt/bodygt
  • lt/htmlgt

12
Smarty Example
  • lt?php
  • // test.php
  • require smarty_start.php"
  • tpl-gtassign("title", ????")
  • tpl-gtassign("content", ??? ?????")
  • tpl-gtdisplay('test.tpl')
  • ?gt

13
Intermediate and temporary file
  • templates_c/179/1798044067/test.tpl.php
  • lt?php / Smarty version 2.6.0, created on
    2003-12-15 221945 compiled from test.tpl / ?gt
  • lthtmlgt
  • ltheadgt
  • ltmeta http-equiv"Content-Type"
    content"text/html charsetbig5"gt
  • lttitlegtlt?php echo this-gt_tpl_vars'title'
    ?gtlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt?php echo this-gt_tpl_vars'content' ?gt
  • lt/bodygt
  • lt/htmlgt

14
Smarty Example 2 (1/2)
  • lt?php
  • // test2.php
  • require smarty_start.php"
  • array1 array(1 gt "??", 2 gt "??", 3 gt
    "??", 4 gt "??")
  • tpl-gtassign("array1", array1)
  • array2 array(
  • array("index1" gt "data1-1", "index2" gt
    "data1-2", "index3" gt "data1-3"),
  • array("index1" gt "data2-1", "index2" gt
    "data2-2", "index3" gt "data2-3"),
  • array("index1" gt "data3-1", "index2" gt
    "data3-2", "index3" gt "data3-3"),
  • array("index1" gt "data4-1", "index2" gt
    "data4-2", "index3" gt "data4-3"),
  • array("index1" gt "data5-1", "index2" gt
    "data5-2", "index3" gt "data5-3")
  • )
  • tpl-gtassign("array2", array2)
  • tpl-gtdisplay("test2.tpl")
  • ?gt

15
Smarty Example 2 (2/2)
  • lt! test2.tpl --!gt
  • lthtmlgtltheadgtltmeta http-equiv"Content-Type"
    content"text/html charsetbig5"gt
  • lttitlegt??????lt/titlegtlt/headgtltbodygt
  • ltpregt
  • ?? foreach ??? array1
  • foreach itemitem1 fromarray1
  • item1
  • /foreach
  • ?? section ??? array1
  • section namesec1 looparray1
  • array1sec1
  • /section
  • ?? foreach ??? array2
  • foreach itemindex2 fromarray2
  • foreach keykey2 itemitem2 fromindex2
  • key2 item2
  • /foreach
  • /foreach
  • ?? section ??? array2

16
Basic Syntax
  • Comments this is a comment
  • Variables
  • Template variables start with a dollar sign.
  • foo foo4 foo.bar
  • Functions Attributes
  • funcname attr1"val" attr2"val".
  • Math
  • Math can be applied directly to variable values.

17
Custom Functions
  • html_image file"masthead.gif"
  • ltimg src"masthead.gif" border"0" height"48"
    width"256"gt
  • html_link type"article" id"abc123" text"Fire
    takes out Hotel"
  • lta href"/display_article.php?idabc123"gt Fire
    takes out Hotellt/agt

18
Logic in the Template
  • if smarty.session.user and ( user_type eq
    "editor" or user_type eq "admin" )
  • ltinput typecheckbox nameedit value"y"gt edit
    ltbrgt
  • /if
  • if edit_flag
  • ltinput typecheckbox nameedit value"y"gt edit
    ltbrgt
  • /if

19
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
Write a Comment
User Comments (0)
About PowerShow.com