【问题标题】:drupal 6 -----why theme() can't outputdrupal 6 -----为什么主题()无法输出
【发布时间】:2010-11-27 06:31:57
【问题描述】:

我放在 mytheme template.php 中的代码

  function mytheme_theme(){
     return array(
       'mytheme_example' => 'example',
         'argument' => array('myvar' => null),
      );
     }

我放在 node.tpl.php 中的代码

 <?php
$html = "";
$myvar = "hello,world";
 $html .= theme('mytheme_example', myvar);

  return $html;
 ?>

我放入 example.tpl.php 的代码

   <div>
   here is the <b><?php print myvar; ?></b>being created.
  </div>

我已经清除了缓存,但是在节点文章的页面上,没有任何关于hello world的输出。

ps:哪些文件我可以使用 hook_theme、template.php、模块文件。有没有我可以使用这个钩子的文件?

【问题讨论】:

    标签: drupal-6 module themes


    【解决方案1】:

    看起来你已经在 template.php 中正确声明了你的 hook_theme,所以我认为这不是问题。

    我确实发现了您的 node.tpl.php 的语法问题,应该不是:

    <?php
        $vars = array('myvar' => 'hello, world');
        $html = theme('mytheme_example', $vars);
        return $html;
    ?>
    

    注意关联数组,带有“myvar”(在 hook_theme 中声明的变量),作为键被传入。

    另外一点,将模板文件命名为与钩子名称相同是标准做法,因此我建议调用模板 mytheme-example.tpl.php。

    更多信息请参见drupal.org

    【讨论】:

    • 我已经更改了您指出的错误。但仍然无法输出 $myvar 的值。
    • 已更新解决方案以反映关联数组,因为我认为这是问题
    【解决方案2】:

    我不知道你是否已经解决了这个问题。

    我会尝试这样声明我的主题:

    function mytheme_theme(){
         return array(
           'mytheme_example' => array(
             'arguments' => array('arguments'=>array()),
             'template' => 'example',
           ),
         }
    

    这就是我通常的做法,而且对我来说效果很好。

    【讨论】:

    • 我已经试过你的代码,但仍然没有任何输出。你能在你的本地环境中测试我的代码吗?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多