我该如何创建一个可在多个模板页面中重用且可以传递变量的 HTML 片段呢?类似这样(但显然还要复杂些):
<ul>
<? foreach ($items as $item): ?>
<li><?=$item?></li>
<? endfor; ?>
</ul>
谢谢
使用自定义模块中的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'));