2

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>
Evanss
  • 23,390
  • 94
  • 282
  • 505
  • No idea, whether this is going in the right direction but if i got you right, you want to use something like snippets: https://www.drupal.org/node/1288700 – Anticom Jun 18 '15 at 08:56
  • Its more an HTML snippet than a PHP one. Is there a way to create new TPL files? – Evanss Jun 18 '15 at 08:58

2 Answers2

3

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') ?>
tonystar
  • 1,274
  • 9
  • 19
  • This isnt working for me. Ive changed THEME_OR_MODULE to my theme's name and put it in my template.php. My new file is in the templates directory and called custom-template.tpl. Ive put in various TPL files but nothing happens. – Evanss Jun 25 '15 at 11:34
  • It should be called "custom-template.tpl.php", not "custom-template.tpl"! Also make sure you clear the caches. – tonystar Jun 25 '15 at 11:47
  • Thats was a typo, the file is called custom-template.tpl.php. All that ive put in it is

    Test

    . Ive cleared the cache multiple times.
    – Evanss Jun 25 '15 at 12:04
0

Is it what you are looking for?

Drupal - Render a sub view/partial within template

Community
  • 1
  • 1
Mikael
  • 2,355
  • 1
  • 21
  • 45