该页面的路径别名是/年度报告/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");
}当我加载页面时,它没有加载正确的模板。如果您注意到,我正在将suggestions数组输出到注释,以确保建议适当的模板。它输出以下内容:
<!--
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--年度报告--%.tpl.php的模板文件。但是,它加载的是基页--node.tpl.php。我遗漏了什么?
发布于 2013-07-17 03:46:38
好的。问题是模板预处理器中所有的'-‘字符都需要替换为'_’,而不仅仅是别名路径部分之间的字符。因此,“年度”和“报告”之间的“-”也必须被替换。所以我的预处理器中的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;
}https://stackoverflow.com/questions/17685225
复制相似问题