How to implement hook_theme in drupal 7?

前端 未结 1 1128
后悔当初
后悔当初 2020-12-16 17:49

I created a new drupal 7 theme and trying to implement hook_theme at template.php like this:

function mytheme_theme($existing, $type, $theme, $path){
    ret         


        
相关标签:
1条回答
  • 2020-12-16 17:52

    Try to add the variables array in hook_theme

    function mytheme_theme($existing, $type, $theme, $path){
        return array(
            'mytheme_header' => array(
                'template' => 'header',
                'path' => $path . '/templates',
                'type' => 'theme',
                'variables' => array(
                    'title' => NULL,
                    'some_text' => NULL,
                ),
            ),
        );
    }
    

    In your header.tpl.php file:

    <h1><?php print $title; ?></h1>
    <p><?php print $some_text; ?></p>
    

    Then, print it out like this:

    $vars = array();
    $vars['title'] = "This is a title";
    $vars['some_text'] = "Some text...";
    print theme('mytheme_header', $vars);
    
    0 讨论(0)
提交回复
热议问题