zoukankan      html  css  js  c++  java
  • PHP模板引擎Smarty入门使用

    PHP模板引擎Smarty入门使用 1、Smarty介绍及应用的优缺点 什么是smarty? Smarty是一个使用PHP写出来的PHP模板引擎,目的是要使用PHP程序同美工分离,使的程序员改变程序的逻辑内容时不会影响到美工的页面设计,美工重新修改页面时不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。(也易于程序的多样式开发) Smarty优点 1. 速度快:相对其他模板引擎。        2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PHP文件 3 缓存技术:它可以将用户最终看到的HTML文件缓存成一个静态的HTML页 4. 插件技术:smarty可以自定义插件。 不适合使用smarty的地方 1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新 2. 小项目。小项目因为项目简单而美工与程序员兼于一人的项目 2、Smarty的配置 include_once("Smarty/Smarty.class.php"); //包含smarty类文件  $smarty new Smarty(); //建立smarty实例对象$smarty  $smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量  $smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存  $smarty->template_dir = "./templates"; //设置模板目录  $smarty->compile_dir = "./templates_c"; //设置编译目录  $smarty->cache_dir = "./smarty_cache"; //缓存文件夹  //----------------------------------------------------  //左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突  //----------------------------------------------------  $smarty->left_delimiter = "{";  $smarty->right_delimiter = "}";  网站中必须首先要配置好smarty模版,并且建立好相应的templates、templates_c、smarty_cache目录,在其他的网页中都要使用include("smarty_inc.php");来调用该模版引擎 smarty_inc.php <?php include_once("Smarty/Smarty.class.php"); //包含smarty类文件 $smarty = new Smarty(); //建立smarty实例对象$smarty $smarty->config_dir="Smarty/Config_File.class.php";  // 目录变量 $smarty->caching=false; //是否使用缓存,项目在调试期间,不建议启用缓存 $smarty->template_dir = "./templates"; //设置模板目录 $smarty->compile_dir = "./templates_c"; //设置编译目录 $smarty->cache_dir = "./smarty_cache"; //缓存文件夹 //---------------------------------------------------- //左右边界符,默认为{},但实际应用当中容易与JavaScript相冲突 //---------------------------------------------------- $smarty->left_delimiter = "{"; $smarty->right_delimiter = "}"; ?> index.php <?php include("smarty_inc.php"); $site="php学习网站"; $name[]=array("name"=>"新闻第一条","date"=>"2011.6.9"); $name[]=array("name"=>"php100中文","date"=>"2011.6.9"); $name[]=array("name"=>"视频教程","date"=>"2011.6.9"); $name[]=array("name"=>"中国门户","date"=>"2011.6.9"); $row=array("标题","作者","当前页面"); $smarty->assign("site",$site); $smarty->assign("title",$name); $smarty->assign("row",$row); $smarty->display("index.html"); ?> 模版引擎里的网页 templates/index.html <html> {$site}<br> {$row[0]} | {$row[1]} | {$row[2]} <hr> {section name=list loop=$title} <b><font color=red> {$title[list].name} - {$title[list].date} </font></b><br> {/section} </html> 输出结果: php学习网站 标题 | 作者 | 当前页面 新闻第一条 - 2011.6.9  php100中文 - 2011.6.9  视频教程 - 2011.6.9  中国门户 - 2011.6.9 
  • 相关阅读:
    jQuery.extend
    Topshelf便捷创建Windows服务
    cron表达式
    定时调度框架:Quartz.net
    sqlserver自定义函数
    HTML dom document 对象
    正则表达式之 数据验证 与 文本替换
    JavaScript 之 DOM 与 BOM
    CSS 之pseudo-classes 与pseudo-element的异同
    CSS中的 position与Grid Layout
  • 原文地址:https://www.cnblogs.com/gxldan/p/4066662.html
Copyright © 2011-2022 走看看