2012-03-21 56 views
6

如何创建一个可以在多个模板页面间重复使用的html片段,并且可以将变量s传入?一些像这样的(但显然有点复杂):Drupal - 呈现模板中的子视图/部分

<ul> 
    <? foreach ($items as $item): ?> 
    <li><?=$item?></li> 
    <? endfor; ?> 
</ul> 

感谢

回答

8

使用hook_theme()自定义模块中,然后从你的模板里调用theme()方法。

在你的模块:

mymodule_theme($existing, $type, $theme, $path) { 
    return array(
    'my_theme_name' => array(
     'template' => 'my_template_file_name', // without the .tpl.php extension 
     'variables' => array(), // to define default values for passed variables 
    ) 
); 
} 

在模板:

theme('my_theme_name', array('arg1' => 'val1', 'arg2' => 'val2'));