1

一般说明

我正在向$variables['theme_hook_suggestions']主题预处理函数内部添加值,但我的主题没有接受建议。我正在做一个健康的完全缓存清除,所以缓存不是问题。

我希望我的主题选择我的建议,但无论我尝试什么,它都不起作用。

以上是我的问题的抽象版本,它的解决方案可能对大多数人有用。下面是我的具体设置的详细描述,以及我的问题的更具体的陈述,以及我尝试的“解决方案”(它不起作用)。

您能给我的任何帮助都将是巨大的,非常感谢。谢谢!

设置

在我的template.php文件中,我有以下代码:

function MYTHEME_preprocess_user_profile(&$variables) {  
  $variables['profile'] = array(
    'top_section' => array(
      '#theme'  => 'profile_section',
      '#type' => 'top_section',
      'profile_section' => /* [several renderable elements] */,
    ),
    'bottom_section' => array(
      '#theme'  => 'profile_section',
      '#type' => 'bottom_section',
      'profile_section' => /* [several renderable elements] */,
    ),
  );
}

因此,从本质上讲,该MYTHEME_preprocess_user_profile()函数创建了几个配置文件部分,分配给$variables['profile'].

然后我在我的主题中设置了一个user-profile.tpl.php模板文件。由于我正在使用 Zen 子主题,因此该文件位于我的主题目录中的“模板”目录中。它看起来像这样:

<h1 class="title">user-profile.tpl.php</h1>
<?php foreach(element_children($profile) as $section) {
  print render($profile[$section]);  
}
?>

然后我设置了一个 MYTHEME_theme() 函数,如下所示:

function MYTHEME_theme($existing, $type, $theme, $path) {
  return array(
    'profile_section' => array(
      'render element' => 'profile_section',
      'template' => 'templates/profile-section',
    ),
  );
}

最后,我profile-section.tpl.php在我的模板目录中设置了一个文件,如下所示:

<h2>profile-section.tpl.php</h2>
<p>Theming the profile section <strong><php print $profile_section['#type'] ?></strong> with the <strong>standard</strong> profile section template.</strong></p>

有了这个,在大多数情况下,我得到了我希望主题如何对我的用户配置文件部分做出反应的所有所需行为。也就是说,对于我的每个个人资料部分,我可以将其主题化为“profile_section”。

问题

我还希望能够设置模板文件来处理特定配置文件部分的主题,基于我放入模板目录的新模板文件。所以,具体到这个例子,虽然我想profile-section.tpl.php处理大多数配置文件部分,但我还希望能够放入profile-section--top-section.tpl.php文件,清除缓存,并让主题将该文件用于“top_section”配置文件部分。

我的“解决方案”不起作用

我已经读到我的问题可以通过在适当的主题预处理函数中使用 theme_hook_suggestions 来解决。因此,就我而言,我编写了一个MYTHEME_preprocess_profile_section()函数,如下所示:

function MYTHEME_preprocess_profile_section(&$variables) {
  $variables['theme_hook_suggestions'][] = 'profile_section__' . $variables['profile_section']['#type'];
}

然后我写了一个profile-section--top-section.tpl.php文件如下:

<h2>profile-section--top-section.tpl.php</h2>
<p>Theming the profile section <strong><php print $profile_section['#type'] ?></strong> with the <strong>top-sectoin</strong> profile section template.</strong></p>

这完成了我的设置。我刷新缓存,但是...

主题仍然profile-section.tpl.php为我的 top_section profile_section 选择模板,而不是profile_section__top_section.tpl.php我写的更具体的模板。

我需要从这里做什么,或者我应该改变什么,以使这个设置从我的主题建议中获取MYTHEME_preprocess_profile_section()

4

0 回答 0