Verzeichnis suchen
Smarty模板编译引擎 I.开始 第一章. 什么是Smarty? 第二章. 安装 要求 基本安装 扩展设置 II.模板设计者篇 第三章.基本语法 注释 函数 属性 第四章.变量 从PHP分配的变量 Associative arrays Array indexes Objects 从配置文件读取的变量 {$smarty}保留变量 Request variables {$smarty.now} {$smarty.const} {$smarty.capture} {$smarty.config} {$smarty.section} {$smarty.template} 第五章.变量调节器 capitalize count_characters cat count_paragraphs count_sentences count_words date_format default escape indent lower nl2br regex_replace replace spacify string_format strip strip_tags truncate upper wordwrap 第六章.组合修改器 第七章.内建函数 capture config_load foreach include include_php insert if ldelim literal php section index index_prev index_next iteration first last rownum loop show total strip 第八章.自定义函数 assign counter cycle debug eval fetch html_checkboxes html_image html_options html_radios html_select_date html_select_time html_table math mailto popup_init popup textformat 第九章.配置文件 第十章.调试控制台 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 第十三章.方法 append append_by_ref assign assign_by_ref clear_all_assign clear_all_cache clear_assign clear_cache clear_compiled_tpl clear_config config_load display fetch get_config_vars get_registered_object get_template_vars is_cached load_filter register_block register_compiler_function register_function register_modifier register_object register_outputfilter register_postfilter register_prefilter register_resource trigger_error template_exists unregister_block unregister_compiler_function unregister_function unregister_modifier unregister_object unregister_outputfilter unregister_postfilter unregister_prefilter unregister_resource 第十四章.缓存 Setting Up Caching Multiple Caches Per Page Cache Groups Controlling Cacheability of Plugins' Output 第十五章.高级特点 Objects Prefilters Postfilters Output Filters Cache Handler Function Resources Templates from $template_dir Templates from any directory Templates from other sources Default template handler function 第十六章.以插件扩展Smarty How Plugins Work Naming Conventions Writing Plugins Template Functions Modifiers Block Functions Compiler Functions Prefilters/Postfilters Output Filters Resources Inserts Ⅳ.高级特点 第十七章.疑难解答 Smarty/PHP errors 第18章.使用技巧和经验 Blank Variable Handling Default Variable Handling Passing variable title to header template Dates WAP/WML Componentized Templates Obfuscating E-mail Addresses 第十九章. 相关资源 第二十章. 漏洞
Figuren

include_php

Attribute Name Type Required Default Description
file string Yes n/a The name of the php file to include
once boolean No true whether or not to include the php file more than once if included multiple times
assign string No n/a The name of the variable that the output of include_php will be assigned to

属性 类型 是否必须 缺省值 描述
file string Yes n/a 待包含php文件的名称
once boolean No true 如果待包含php文件已被包含是否仍然包含(类似php中的include_once函数)
assign string No n/a 该属性指定一个变量保存待包含php文件的输出

include_php tags are used to include a php script in your template. If security is enabled, then the php script must be located in the $trusted_dir path. The include_php tag must have the attribute "file", which contains the path to the included php file, either relative to $trusted_dir, or an absolute path.

inluce_php 函数用于在模板中包含 php 脚本. 如果设置了安全模式,被包含的脚本必须位于 $trusted_dir 路径下. include_php 函数必须设置 file 属性,该属性指明被包含 php 文件的路径,可以是 $trusted_dir 的相对路径,也可以是绝对路径.

include_php is a nice way to handle componentized templates, and keep PHP code separate from the template files. Lets say you have a template that shows your site navigation, which is pulled dynamically from a database. You can keep your PHP logic that grabs database content in a separate directory, and include it at the top of the template. Now you can include this template anywhere without worrying if the database information was assigned by the application before hand.

include_php 是解决模板部件化的好方法,它使得 php 代码从模板文件中被分离出来. 举个例子:假设有一个从数据库中动态取出数据用于显示站点导航的模板,你可以将得数据内容的 php 逻辑部分分离出来保存在一个单独的文件夹下,并在模板开始的位置包含该 php 脚本. 那么就可以在任何地方包含此模板而不用担心之前数据库信息是否已被程序取出.

By default, php files are only included once even if called multiple times in the template. You can specify that it should be included every time with the once attribute. Setting once to false will include the php script each time it is included in the template.

即使是在模板中多次地调用 php 文件,默认情况下它们只被包含一次. 你可以设置 once 属性从而指明每次调用都重新包含该文件. 如果将 once 属性设置为 false,每次调用该文件都将被重新包含.

You can optionally pass the assign attribute, which will specify a template variable name that the output of include_php will be assigned to instead of displayed.

如果设置了 assign 属性,该属性对应的变量名用于保存待包含 php 的输出,这样待包含 php 文件的输出就不会直接显示了。

The smarty object is available as $this within the PHP script that you include.

在待包含 php 文件中可以通过 $this 访问 smarty 对象.

Example 7-9. function include_php
例 7-9. include_php 函数演示

load_nav.php
-------------

<?php

	// load in variables from a mysql db and assign them to the template
	// 从mysql数据库中取得数据,将数据赋给模板变量
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 *} {* 绝对路径或 $trusted_dir 的相对路径 *} {include_php file="/path/to/load_nav.php"} {foreach item="curr_section" from=$sections} <a href="{$curr_section.url}">{$curr_section.name}</a><br> {/foreach}
 
Vorheriger Artikel: Nächster Artikel: