'Smart' way to include the same HTML in multiple TPL files?

时间:2015-06-15 14:17:40

标签: drupal-theming

Im using the same HTML (below) in multiple views TPL files. Its used to make my own custom pager work with JavaScript.

At the moment im creating a TPL file for each view and then adding the full markup every time. This isnt great from a maintenance point of view as if I need to alter the HTML I will need to do so for every instance.

Is there a 'smarter' way to do this? For instance can I create a new TPL file to house this HTML and call the file in each views TPL file when I need it?

  <div class="picThumb">
    <div class="picThumb-inner">
      <div class="picThumb-trig picThumb-trigLeft">Previous</div>
      <div class="picThumb-trig picThumb-trigP1">1</div>
      <div class="picThumb-trig picThumb-trigP2">2</div>
      <div class="picThumb-trig picThumb-trigRight">Next</div>
    </div>
  </div>

2 个答案:

答案 0 :(得分:3)

这里最好的选择 - 通过hook_theme()注册自定义模板:

/**
 * Implements hook_theme().
 */
function THEME_OR_MODULE_theme() {
  return array(
    'custom_template'  => array(
      'template' => 'custom-template',
      'variables' => array(),
    ),
  );
}

然后将自定义HTML放在custom-template.tpl.php文件中。

然后你可以&#34;包括&#34;它使用theme()函数在任何地方:

<?php print theme('custom_template') ?>

答案 1 :(得分:0)