目录搜索
Smarty模板编译引擎I.开始第一章. 什么是Smarty?第二章. 安装要求基本安装扩展设置II.模板设计者篇第三章.基本语法注释函数属性第四章.变量从PHP分配的变量Associative arraysArray indexesObjects从配置文件读取的变量{$smarty}保留变量Request variables{$smarty.now}{$smarty.const}{$smarty.capture}{$smarty.config}{$smarty.section}{$smarty.template}第五章.变量调节器capitalizecount_characterscatcount_paragraphscount_sentencescount_wordsdate_formatdefaultescapeindentlowernl2brregex_replacereplacespacifystring_formatstripstrip_tagstruncateupperwordwrap第六章.组合修改器第七章.内建函数captureconfig_loadforeachincludeinclude_phpinsertifldelimliteralphpsectionindexindex_previndex_nextiterationfirstlastrownumloopshowtotalstrip第八章.自定义函数assigncountercycledebugevalfetchhtml_checkboxeshtml_imagehtml_optionshtml_radioshtml_select_datehtml_select_timehtml_tablemathmailtopopup_initpopuptextformat第九章.配置文件第十章.调试控制台III.模板程序员篇第十一章 常量SMARTY_DIR第十二章 变量$template_dir$compile_dir$config_dir$plugins_dir$debugging$debug_tpl$debugging_ctrl$global_assign$undefined$autoload_filters$compile_check$force_compile$caching$cache_dir$cache_lifetime$cache_handler_func$cache_modified_check$config_overwrite$config_booleanize$config_read_hidden$config_fix_newlines$default_template_handler_func$php_handling$security$secure_dir$security_settings$trusted_dir$left_delimiter$right_delimiter$compiler_class$request_vars_order$compile_id$use_sub_dirs$default_modifiers$default_resource_type第十三章.方法appendappend_by_refassignassign_by_refclear_all_assignclear_all_cacheclear_assignclear_cacheclear_compiled_tplclear_configconfig_loaddisplayfetchget_config_varsget_registered_objectget_template_varsis_cachedload_filterregister_blockregister_compiler_functionregister_functionregister_modifierregister_objectregister_outputfilterregister_postfilterregister_prefilterregister_resourcetrigger_errortemplate_existsunregister_blockunregister_compiler_functionunregister_functionunregister_modifierunregister_objectunregister_outputfilterunregister_postfilterunregister_prefilterunregister_resource第十四章.缓存Setting Up CachingMultiple Caches Per PageCache GroupsControlling Cacheability of Plugins' Output第十五章.高级特点ObjectsPrefiltersPostfiltersOutput FiltersCache Handler FunctionResourcesTemplates from $template_dirTemplates from any directoryTemplates from other sourcesDefault template handler function第十六章.以插件扩展SmartyHow Plugins WorkNaming ConventionsWriting PluginsTemplate FunctionsModifiersBlock FunctionsCompiler FunctionsPrefilters/PostfiltersOutput FiltersResourcesInsertsⅣ.高级特点第十七章.疑难解答Smarty/PHP errors第18章.使用技巧和经验Blank Variable HandlingDefault Variable HandlingPassing variable title to header templateDatesWAP/WMLComponentized TemplatesObfuscating E-mail Addresses第十九章. 相关资源第二十章. 漏洞
文字

Componentized Templates

组合的模板

This tip is a bit of a hack, but still a neat idea. Use at your own risk. ;-)

这项技巧有点普通,但是仍然是个不错的思想,如果要使用的话,自担风险。;-)

Traditionally, programming templates into your applications goes as follows: First, you accumulate your variables within your PHP application, (maybe with database queries.) Then, you instantiate your Smarty object, assign the variables and display the template. So lets say for example we have a stock ticker on our template. We would collect the stock data in our application, then assign these variables in the template and display it. Now wouldn't it be nice if you could add this stock ticker to any application by merely including the template, and not worry about fetching the data up front?

传统上,把模板编入你的应用程序中步骤如下:首先,你累计一下你的PHP程序中的变量,(可能用到数据库查询。)然后,你实例化一个你的Smarty对象,给变量赋值并且输出这个模板。因此,例如我们说我们在我们的模板里有一个资源标记。我们可以在我们的应用程序中收集这些资源数据,然后给模板中的这些变量赋值并输出。现在如果你仅仅是通过包含这些模板就可以把资源标记加入到任何一个应用程序中不是很好吗?

You can embed PHP into your templates with the {php}{/php} tags. With this, you can setup self contained templates with their own data structures for assigning their own variables. With the logic embedded like this, you can keep the template & logic together. This way no matter where the template source is coming from, it is always together as one component.

你可以通过使用{php}{/php}这两个标签来在你的模板里嵌入PHP。这样,你就能够建立包含自身的使用自己的数据结构的模板来给他们自身的变量赋值。通过像这样逻辑的嵌入,你能够把模板和逻辑性结合起来。这种方法使得模板不管源于何处总是像一个构件一样组合在一起。

Example 18-7. componentized template

例 18-7. 组合的模板

{* Smarty *}

{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);

{/php}

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

As of Smarty 1.5.0, there is even a cleaner way. You can include php in your templates with the {include_php ...} tag. This way you can keep your PHP logic separated from the template logic. See the 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}
上一篇:下一篇: