本文目錄一覽:
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/zh-hant/n/252085.html