Get Smarty


Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Buy cheap glasses from and save up to 80%.

Where to buy prescription glasses? is your best choice!

Where to buy discount wedding dresses and cheap smart dresses free shipping -

Shop for 2018 World Cup Football Boots Online sale at

Browse best 2018 world cup soccer cleats and shoes from



传统来说,应用程序使用模板编程都按这样的方式: 先在PHP程序中,将需要的变量都获取到 (可能还会通过数据库获取), 然后实例化Smarty对象,调用assign()来赋值变量,并且display()。 好的,我们来看看模板上显示股票的例子吧。 我们会在程序中收集到股票的数据,然后将数据变量赋值到模板,并且显示模板。 现在,如果你可以在模板简单地获取到任意来源的股票数据,不需要担心在前端获取这些数据,不是更好吗?


Example 21.7. 组件化模板

function.load_ticker.php - 文件在 $plugins 目录


// setup our function for fetching stock data
function fetch_ticker($symbol)
   // put logic here that fetches $ticker_info
   // from some ticker resource
   return $ticker_info;

function smarty_function_load_ticker($params, $smarty)
   // call the function
   $ticker_info = fetch_ticker($params['symbol']);

   // assign template variable
   $smarty->assign($params['assign'], $ticker_info);



{load_ticker symbol='SMARTY' assign='ticker'}

Stock Name: {$} Stock Price: {$ticker.price}


参见 {include_php}, {include}{php}.

No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Email: (not shown)
What is 1 plus 0? (Are you human?)