【问题标题】:Drupal 7: Appropriate page template not loadingDrupal 7:未加载适当的页面模板
【发布时间】:2013-07-16 19:20:41
【问题描述】:

页面的路径别名是 /annual-reports/2012。我在主题 template.php 文件中设置了以下内容:

function tcm_preprocess_page(&$variables, $hook) {
    $alias = drupal_get_path_alias($_GET['q']);
    $alias = explode('/', $alias);
    $template_filename = 'page';
    foreach ($alias as $path_part) {
      if(is_numeric($path_part)){
          $variables['theme_hook_suggestions'][] = $template_filename . '__%';
      }
      $template_filename = $template_filename . '__' . $path_part;
      $variables['theme_hook_suggestions'][] = $template_filename;
    }
    print_r("<!--\n");
    print_r($variables['theme_hook_suggestions']);
    print_r("\n-->\n");
}

当我加载页面时,它没有加载正确的模板。如果您注意到我正在将建议数组输出到 cmets 以确保建议了适当的模板。它输出以下内容:

<!--
Array
(
    [0] => page__node
    [1] => page__node__%
    [2] => page__node__207
    [3] => page__annual-reports
    [4] => page__annual-reports__%
    [5] => page__annual-reports__2012
)

-->

我有一个名为 page--annual-reports--%.tpl.php 的模板文件。然而,它正在加载基本页面--node.tpl.php。我错过了什么?

【问题讨论】:

    标签: php drupal-7 drupal-theming drupal-templates


    【解决方案1】:

    好的。问题是模板预处理器中的所有“-”字符都需要替换为“_”,而不仅仅是别名路径部分之间的字符。所以'annual'和'reports'之间的'-'也必须被替换。所以我的预处理器中的 for 循环现在看起来像这样:

    foreach ($alias as $path_part) {
      if(is_numeric($path_part)){
        $variables['theme_hook_suggestions'][] = $template_filename . '__%';
      }
      $template_filename = $template_filename . '__' . preg_replace("/-/", "_", $path_part);
      $variables['theme_hook_suggestions'][] = $template_filename;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多