Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal - Render a sub view/partial within template

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

like image 297
DonutReply Avatar asked Mar 21 '12 10:03

DonutReply


1 Answers

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'));
like image 106
jmlnik Avatar answered Nov 07 '22 00:11

jmlnik