> Smarty模板引擎中文在线手册 > Componentized Templates [组合的模板]

include_PHP function for more information.

就像smarty 1.5.0,这里有一种更清晰的方法。你可以在你的模板里使用{include_php ...}这些标签来包含php。使用这种方法,你能够保持PHP的逻辑性和模板的逻辑性分离。参照include_php 这个函数来获得更多信息。

Example 18-8. componentized template with include_php

例 18-8. 使用include_php组合的模板

load_ticker.php
---------------

<?php
	// setup our function for fetching stock data
	function fetch_ticker($symbol,&$ticker_name,&$ticker_price) {
		// put logic here that fetches $ticker_name
		// and $ticker_price from some resource
	}

	// call the function
	fetch_ticker("YHOO",$ticker_name,$ticker_price);
	
	// assign template variables
 $this->assign("ticker_name",$ticker_name);
 $this->assign("ticker_price",$ticker_price);
?>


index.tpl
---------

{* Smarty *}

{include_php file="load_ticker.php"}

Stock Name: {$ticker_name} Stock Price: {$ticker_price}

上一篇:
下一篇: