如何对我自己的模板使用主题预处理器函数?

发布于 2024-08-23 11:47:09 字数 404 浏览 8 评论 0原文

我有几个用于节点、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌面超妹 2024-08-30 11:47:09

要获得总体概述,您应该阅读有关在预处理函数中操作变量的内容


关于命名约定,这通常非常简单,但当前示例有一个问题(见下文):

预处理函数签名需要为

[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 字段,例如:(

function yourThemeName_preprocess_content_field(&$variables) {
  // Are we called for the right field?
  if ('field_transmission_make_model' == $variables['field_name']) {
    // Yes, add/manipulate entries within the variables array
    $variables['new_entry'] = 'A useless new variable';
    $variables['label'] = 'A useless change of the existing label variable';
  }
}

注意:未经测试的代码,谨防拼写错误)

在此之后,模板文件中应该有一个新变量 $new_entry 可用,并且内容$label 变量应该已更改($variables 数组中的所有顶级条目都将转换为模板文件的单独变量,以数组索引命名)。


至于你的第二个问题,预处理函数的基本用法对于所有模板文件都是相同的,但请注意:

  • 预处理函数仅适用于使用 *.tpl.php 文件的主题调用,不适用于主题函数
  • $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 being content-field.tpl.php, and the corresponding theme function being content_field. So you would have to implement a preprocess function named yourThemeName_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.:

function yourThemeName_preprocess_content_field(&$variables) {
  // Are we called for the right field?
  if ('field_transmission_make_model' == $variables['field_name']) {
    // Yes, add/manipulate entries within the variables array
    $variables['new_entry'] = 'A useless new variable';
    $variables['label'] = 'A useless change of the existing label variable';
  }
}

(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:

  • Preprocess functions are only available for theme calls that use *.tpl.php files, not for theme functions
  • The content of the $variables array varies heavily, depending on what gets themed
  • Other modules might implement the preprocess functions as well, and they will be called one after another, so if you want to change something that gets added by another module, you can only do so if your implementation gets called after that (which will be no problem in your case, as implementations within a theme are called after all implementations within modules - just wanted to mention that there can be many implementations at once)

为了弄清楚我们的预处理函数应该命名什么,我们需要知道某些输出来自哪个模板文件或主题函数,而实现此目的的一个好方法是使用 主题开发人员模块。

这是一个详细解释它的视频 - 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文