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

326 views Asked by At

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

There are 2 answers

3
tonystar On

The best option here — is to register a custom template via hook_theme():

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

Then put your custom HTML in the custom-template.tpl.php file.

Then you can "include" it anywhere using theme() function:

<?php print theme('custom_template') ?>
0
Mikael On

Is it what you are looking for?

Drupal - Render a sub view/partial within template