1

I am trying to use the hook_theme function to utilize some template files. I have the following function defined in my theme:

template.php

function my_name_theme($existing, $type, $theme, $path) {
    return array(
        'testy' => array(
            'variables' => array('title' => NULL),
            'template'  => 'testy',
        ),
    );
}

And I have the following template defined:

testy.tpl.php

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

I have a content type called "Page" with a corresponding template:

node--page.tpl.php

<?php
    print 'before theme call';
    $variables = array();
    $variables['title'] = 'my title';
    print theme('testy', $variables);
    print 'after theme call';
?>

When viewing one of my "Page" content types, the rendered page only shows the following, with no title:

before theme callafter theme call

I have cleared all caches. Why isn't this working?

  • Obvious question first: did you clear Drupal's caches? – Clive Apr 23 '13 at 17:53
  • @Clive Yes, I have. – cowgod Apr 23 '13 at 17:57
  • see if Drupal includes your testy.tpl.php file first (put some garbage in PHP tag and see if you get a fatal error) – AyeshK Apr 23 '13 at 18:03
  • @AyeshK That's a good tip--I tried that and I am not getting any errors. So, why isn't the template being loaded? – cowgod Apr 23 '13 at 18:17
  • where did you put the test.tpl.php file ? – AyeshK Apr 23 '13 at 18:18
0

I had to add a path to my function, then it worked.

template.php

function my_name_theme($existing, $type, $theme, $path) {
    return array(
        'testy' => array(
            'variables' => array('title' => NULL),
            'template'  => 'testy',
            'path'      => drupal_get_path('theme', 'my_name') . '/templates',
        ),
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.