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

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

问题描述

我有几个.tpl.php文件用于节点,CCK字段和Views主题。这些模板文件有很多逻辑来移动事物,删除链接,创建新的链接等。我明白这是糟糕的开发,而不是Drupal方式。



如果我理解正确,Drupal Way是在您的 template.php 文件中使用预处理函数来操纵变量并添加新变量。有几个问题:




  • 是否有一个命名约定为特定主题创建预处理器功能?例如,如果我有一个名为 content-field-field_transmission_make_model.tpl 的CCK字段模板,我将如何命名预处理函数?

  • 我可以为节点模板,CCK字段模板和Views模板使用模板预处理函数吗?他们有不同的修改模板变量的方法或添加新的模板变量吗?


解决方案

概述中,您应该读取处理函数内的操作变量






关于命名约定,这通常很简单,但是当前的示例有一个catch(见下文):



预处理函数签名需要



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



所以在主题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){ 
//我们是否调用了正确的字段?
if('field_transmission_make_model'== $ variables ['field_name']){
//是,添加/操纵变量数组中的条目
$ variables ['new_entry'] ='A无用的新变量;
$ variables ['label'] ='现有标签变量的无用变更';
}
}

(注意:未验证的代码,小心拼写错误) / p>

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






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




  • 预处理功能仅适用于使用* .tpl.php文件的主题调用,用于主题功能


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?

解决方案

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)

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

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