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?

11
  • Obvious question first: did you clear Drupal's caches?
    – Clive
    Commented Apr 23, 2013 at 17:53
  • @Clive Yes, I have.
    – cowgod
    Commented Apr 23, 2013 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)
    – AKS
    Commented Apr 23, 2013 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
    Commented Apr 23, 2013 at 18:17
  • where did you put the test.tpl.php file ?
    – AKS
    Commented Apr 23, 2013 at 18:18

1 Answer 1

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 and acknowledge you have read our privacy policy.

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