如何为我自己的模板使用主题预处理器功能? [英] How do I use theme preprocessor functions for my own templates?

查看:19
本文介绍了如何为我自己的模板使用主题预处理器功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个用于节点、CCK 字段和视图主题的 .tpl.php 文件.这些模板文件中有很多逻辑来移动内容、删除链接、创建新链接等.我知道这是糟糕的开发,而不是Drupal 方式".

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".

如果我理解正确,Drupal 方式"是在您的 template.php 文件中使用预处理器函数来操作变量并添加新变量.关于这个的几个问题:

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:

  • 是否有为特定主题创建预处理器函数的命名约定?例如,如果我有一个名为 content-field-field_transmission_make_model.tpl 的 CCK 字段模板,我将如何命名预处理器函数?
  • 我可以将模板预处理器函数用于节点模板、CCK 字段模板和视图模板吗?他们是否有不同的修改模板变量或添加新变量的方法?
  • 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?

推荐答案

对于一般概述,您应该阅读在预处理函数中操作变量.

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

预处理函数签名需要

[yourModuleName|yourThemeName]_preprocess_[themeFunctionName](&$variables)

因此在主题 template.php 文件中为页面模板实现一个将导致

so implementing one for the page template within a themes template.php file would result in

themeName_preprocess_page(&$variables)

大多数情况下,主题函数的名称将是 *.tpl.php 文件的名称,没有 .tpl.php 结尾,并带有下划线而不是连字符.但是如果根据模板建议>,因为预处理功能只能针对基本名称实现,不能针对附加建议!(备用模板文件的建议是在预处理函数本身中添加的.)

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.)

您当前的示例就是其中一种情况,因为 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 字段,例如:

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)

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

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:

  • 预处理函数仅适用于使用 *.tpl.php 文件的主题调用,不适用于主题函数
  • $variables 数组的内容变化很大,这取决于主题
  • 其他模块也可能实现预处理功能,并且它们将被一个接一个地调用,因此如果您想更改由另一个模块添加的某些内容,则只能在您的实现在此之后被调用时才能这样做(即在您的情况下不会有问题,因为在模块中的所有实现之后调用主题中的实现 - 只是想提到一次可以有很多实现)

这篇关于如何为我自己的模板使用主题预处理器功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆