如何对我自己的模板使用主题预处理器函数?
我有几个用于节点、CCK 字段和视图主题的 .tpl.php 文件。这些模板文件中有很多逻辑来移动内容、剥离链接、创建新链接等。我知道这是糟糕的开发而不是“Drupal 方式”。
如果我理解正确的话,“Drupal 方式”是使用 template.php
文件中的预处理器函数来操作变量并添加新变量。有关于此的几个问题:
- 是否有为特定主题创建预处理器函数的命名约定?例如,如果我有一个名为
content-field-field_transmission_make_model.tpl
的 CCK 字段模板,我将如何命名预处理器函数? - 我可以将模板预处理器函数用于节点模板、CCK 字段模板和视图模板吗?他们是否有不同的修改模板变量或添加新变量的方法?
I have several .tpl.php files for nodes, CCK fields, and Views theming. These template files have a lot of logic in them to move things around, strip links, create new links, etc. I understand that this is bad development and not "The Drupal Way".
If I understand correctly, "The Drupal Way" is to use preprocessor functions in your template.php
file to manipulate variables and add new variables. A few questions about that:
- Is there a naming convention for creating a preprocessor function for a specific theme? For example, if I have a CCK field template called
content-field-field_transmission_make_model.tpl
, how would I name the preprocessor function? - Can I use template preprocessor functions for node templates, CCK field templates, and Views templates? Do they have different methods of modifying template variables or adding new ones?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获得总体概述,您应该阅读有关在预处理函数中操作变量的内容。
关于命名约定,这通常非常简单,但当前示例有一个问题(见下文):
预处理函数签名需要为
[yourModuleName|yourThemeName]_preprocess_[themeFunctionName](&$variables)
因此,在主题 template.php 文件中实现页面模板将导致
themeName_preprocess_page(&$variables)
大多数情况下,主题函数的名称将是*.tpl.php 文件,没有 .tpl.php 结尾,并且使用下划线而不是连字符。 但是如果根据模板建议 >,因为预处理功能只能针对基本名称实现,而不能针对附加建议实现! (备用模板文件的建议已添加到预处理函数本身中。)
您当前的示例就是其中一种情况,因为
content-field-field_transmission_make_model.tpl.php
就是这样的建议,其基本名称为content-field.tpl.php
,对应的主题函数为content_field
。因此,您必须实现一个名为yourThemeName_preprocess_content_field(&$variables)
的预处理函数,并在其中检查 $variables 数组中的可用条目,以检查是否确实为“field_transmission_make_model”调用,而不是完全不同的 CCK 字段,例如:(注意:未经测试的代码,谨防拼写错误)
在此之后,模板文件中应该有一个新变量
$new_entry
可用,并且内容$label
变量应该已更改($variables 数组中的所有顶级条目都将转换为模板文件的单独变量,以数组索引命名)。至于你的第二个问题,预处理函数的基本用法对于所有模板文件都是相同的,但请注意:
For a general overview, you should read up on manipulating variables within preprocess functions.
Concerning the naming convention, this is normally pretty simple, but there is a catch for your current example (see below):
A preprocess functions signature needs to be
[yourModuleName|yourThemeName]_preprocess_[themeFunctionName](&$variables)
so implementing one for the page template within a themes template.php file would result in
themeName_preprocess_page(&$variables)
Most of the time the name of the theme function will be the name of the *.tpl.php file, without the .tpl.php ending and with underscores instead of the hyphens. But there is a catch if the template file gets selected on the base of template suggestions, as the preprocess function can only be implemented for the base name, not for the additional suggestions! (The suggestions for alternate template files are added in preprocess functions themselves.)
Your current example is one of those cases, as
content-field-field_transmission_make_model.tpl.php
is such a suggestion, with the base name beingcontent-field.tpl.php
, and the corresponding theme function beingcontent_field
. So you would have to implement a preprocess function namedyourThemeName_preprocess_content_field(&$variables)
, and within that inspect the available entries in the $variables array to check if you are actually called for the 'field_transmission_make_model', and not for a completely different CCK field, e.g.:(Note: Untested code, beware of typos)
After this, there should be a new variable
$new_entry
being available in your template file, and the content of the$label
variable should have changed (all top level entries within the $variables array will be turned into separate variables for the template file, named after the array index).As for your second question, the basic usage of preprocess functions is the same for all template files, but be aware:
为了弄清楚我们的预处理函数应该命名什么,我们需要知道某些输出来自哪个模板文件或主题函数,而实现此目的的一个好方法是使用 主题开发人员模块。
这是一个详细解释它的视频 - http://buildamodule.com/video/drupal-theming-essentials-template-files-theme-function-overrides-and-preprocessing-functions-how-to -使用简单预处理函数
In order to figure out what our preprocessing function should be named, we need to know what template file or theme function some output comes from, and one great way to do this is by using the theme developer module.
Here is a video which explains it in detail - http://buildamodule.com/video/drupal-theming-essentials-template-files-theme-function-overrides-and-preprocessing-functions-how-to-use-simple-preprocessing-functions