Drupal - display sub-view / partial in a template

How to create an html fragment that I can reuse on multiple pages of a template and can pass the variable s? Some people like it (but obviously a little more complicated):

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

thanks

+6
source share
1 answer

Use hook_theme() in your custom module, then call theme() from 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')); 
+8
source

Source: https://habr.com/ru/post/911282/


All Articles