牛骨文教育服务平台(让学习变的简单)

insert

Attribute Name Type Required Default Description
name string Yes n/a The name of the insert function (insert_name)
assign string No n/a The name of the template variable the output will be assigned to
script string No n/a The name of the php script that is included before the insert function is called
[var ...] [var type] No n/a variable to pass to insert function

属性 类型 是否必须 缺省值 描述
name string Yes n/a 插入函数的名称
assign string No n/a 该属性指定一个变量保存待插入函数输出
script string No n/a

插入函数前需要先包含的php脚本名称

[var ...] [var type] No n/a 传递给待插入函数的本地参数

Insert tags work much like include tags, except that insert tags are not cached when you have template caching enabled. They will be executed on every invocation of the template.

Insert 函数类似欲 inluce 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.

Let"s say you have a template with a banner slot at the top of the page. The banner can contain any mixture of HTML, images, flash, etc. so we can"t just use a static link here, and we don"t want this contents cached with the page. In comes the insert tag: the template knows #banner_location_id# and #site_id# values (gathered from a config file), and needs to call a function to get the banner contents.

例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定:#banner_location_id# 和 #site_id# 值(从配置文件中取),同时需要一个函数取广告条的内容信息.

Example 7-10. function insert
例 7-10. insert 函数演示

{* example of fetching a banner *}
{insert name="getBanner" lid=#banner_location_id# sid=#site_id#}

In this example, we are using the name "getBanner" and passing the parameters #banner_location_id# and #site_id#. Smarty will look for a function named insert_getBanner() in your PHP application, passing the values of #banner_location_id# and #site_id# as the first argument in an associative array. All insert function names in your application must be prepended with "insert_" to remedy possible function name-space conflicts. Your insert_getBanner() function should do something with the passed values and return the results. These results are then displayed in the template in place of the insert tag. In this example, Smarty would call this function: insert_getBanner(array("lid" => "12345","sid" => "67890")); and display the returned results in place of the insert tag.

在此例中,我们使用了 getBanner 作为 name 属性,同时传递了 #banner_location_id# 和 #site_id# 两个参数. 接下来 Smarty 在你的 php 程序中搜索名为 insert_getBanner() 的函数,#banner_location_id# 和 #site_id# 的值被组合成一个数组作为函数的第一个参数传递给该函数. 为了避免函数命名混乱,所有的 insert 函数都必须以 insert_ 开头. 你的 insert_getBanner() 函数根据传递的参数执行并返回执行的结果. 这些结果就显示在模板中调用该函数的位置. 在此例中 Smarty 调用该函数类似insert_getBanner(array("lid"=>"12345","sid"=>67890"));并将返回的结果显示在调用的位置.

If you supply the "assign" attribute, the output of the insert tag will be assigned to this template variable instead of being output to the template. NOTE: assigning the output to a template variable isn"t too useful with caching enabled.

如果设置了 assign 属性,该属性对应的变量名用于保存待包含函数的输出,这样待包含函数的输出就不会直接显示了.注意:赋给模板变量的输出信息在缓存的时候同样无效.

If you supply the "script" attribute, this php script will be included (only once) before the insert function is executed. This is the case where the insert function may not exist yet, and a php script must be included first to make it work. The path can be either absolute, or relative to $trusted_dir. When security is enabled, the script must reside in $trusted_dir.

如果指定了 script 属性,在调用函数并执行前将先包含(只包含一次)script指定的 php 脚本. 这是为了防止被调用的函数不存在,先调用包含该函数的 php 脚本将避免该情况.

The Smarty object is passed as the second argument. This way you can reference and modify information in the Smarty object from within the insert function.

Smart 对象作为函数的第二个参数被传递,在待包含函数中可以通过 $this 访问并修改 smarty 对象信息.

Technical Note: It is possible to have portions of the template not cached. If you have caching turned on, insert tags will not be cached. They will run dynamically every time the page is created, even within cached pages. This works good for things like banners, polls, live weather, search results, user feedback areas, etc.

技术要点: 使模板的一部分不被缓存. 如果打开了缓存, insert 函数却不会被缓存,每次调用页面它们都会被动态加载,即使是在缓存页面中. 该特性可以广泛应用于广告条、投票、实时天气预报、搜索结果、反馈信息等区域.