6

How can I create a html snippet that I can reuse across multiple template pages and can pass variable s into? Some like this (but obviously a bit more complicated):

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

Thanks

DonutReply
  • 3,184
  • 6
  • 31
  • 34

1 Answers1

8

Use hook_theme() in a custom module, then call the theme() method from within your template.

In your module:

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
     )
  );
}

In your template:

theme('my_theme_name', array('arg1' => 'val1', 'arg2' => 'val2'));
Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
jmlnik
  • 2,867
  • 2
  • 17
  • 20