本文目录一览:
smarty模板中include和include_php的用法
我们可以在模板内载入PHP程序或者另一个子模板.分别可以使用include_php及include这两个Smarty模板语法.俺习惯用include 啦.
在使用include 时,我们可以预先载入子模板,或者动态载入子模板.预先载入通常使用在有共同的文件标头或者版权什么的.而动态载入则可以用在同一的框架页,这样就可以换皮肤一样换界面啦.两者也可以同时混用.是情况八.
test.php———
?php
require “main.php”;
$tpl-assign(“title”, “Include Test”);
$tpl-assign(“content”, “这是模板1.htm的参数”);
$tpl-assign(“dyn_page”, “3.htm”);
$tpl-display(‘test.htm’);
?
模板templates下面
test.htm———-
html
head
meta http-equiv=”Content-Type” content=”text/html; charset=big5″
title{$title}/title
/head
body
{include file=”1.htm”}br /
{include file=$dyn_page}
{include file=”2.htm” custom_var=”自己定义变量的内容”}
/body
/html
1.htm—-
{$content}
2.htm—-
{$custom_var}
吃饭后继续
OK,下面我将尽力解释下include_php
include_php这个标签通常用来在你的模板文件中include一个php文件.这个php文件一般在这个本地路径的文件夹下面.include_php这个标签必须有file这个属性.用来包含应用php文件的路径的人.可以是相对路径,也可以是绝对路径.{include_php file=”/path/to/load.php”}
include_php是一种非常好的模板组建句柄.使php代码和模板文件分离开来.举例来说吧.比较直观点.比如你有个模板是用来显示你的网站导航的.当然导航的内容是动态的从数据库中获取.这时你可以将php联接数据库的获取内容放在一个php文件中,然后在模板文件中include_php file=xxxxx.php这样,你可以在任何地方应用这个导航模板.而不需要再次做联结导航内容的工作啦.
通常,php文件默认只能包含一次.如果你一定要多次引用,那么可以增加once属性设置为false就可以拉.
给出实际例子啦(来自手册)
function include_php
load_nav.php
————-
?php
// load in variables from a mysql db and assign them to the template
require_once(“MySQL.class.php”);
$sql = new MySQL;
$sql-query(“select * from site_nav_sections order by name”,SQL_ALL);
$this-assign(‘sections’,$sql-record);
?
index.tpl
———
{* absolute path, or relative to $trusted_dir *}
{include_php file=”/path/to/load_nav.php”}
{foreach item=”curr_section” from=$sections}
a href=”{$curr_section.url}”{$curr_section.name}/abr
{/foreach}
———
回答完毕,谢谢大家得掌声.哇哈哈
include_php函数报错
require ‘./Smarty.class.php’;
路径问题,require ‘smarty/libs/Smarty.class.php’;请参考,具体去看下你的Smarty.class.php放哪里
php SmartyBC.class.php是干什么用的?
Smarty Backward Compatability Wrapper Class
这是 SmartyBC.class.php里面的一行注释,这个累主要是为了做向后兼容,因为Smarty3相对于Smarty2存在一定的变化,其中包括一部分新增的内容,也去掉了Smarty中的一部分不规范的内容,这个类是为了让用户升级Smarty3 的时候可以更容易的兼容原来用Smarty2开发的程序。
比如:
Smarty 3 allows expressions almost anywhere. Expressions can include PHP
functions as long as they are not disabled by the security policy, object
methods and properties, etc. The {math} plugin is no longer necessary but
is still supported for BC.
这里面说的,{math}插件在Smarty 3中已经不是必须的插件,但是在SmartyBC中会依然保持对他的支持。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/252085.html