将变量从template.php传递到page.tpl

Dyl*_*uth 2 variables drupal drupal-7

我是drupal的新手,我需要查询一个表并将该查询的结果作为变量传递给我的page.tpl文件.我已经尝试了几个小时,但没有找到工作.

template.php中的php代码

<?php
 $result = db_select("SELECT COUNT(node.nid) AS num FROM node WHERE node.type = 'service'");
 foreach ($result as $record) {
    $number = $record->num;
 }
 return $number;
Run Code Online (Sandbox Code Playgroud)

我甚至尝试添加一个,echo 'hello world';但也没有出现.

page.tpl中的代码

 <?php if ($number > 0) { ?>
    testing
 <?php } ?>
Run Code Online (Sandbox Code Playgroud)

有两个类型为服务的节点.任何帮助都会很棒.谢谢.

Spa*_*ers 6

您需要在template.php文件中使用一个钩子.

例如(将THEME_NAME替换为您的主题名称):

function THEME_NAME_preprocess_page(&$vars) {   
    $vars['foo'] = "bar";
}
Run Code Online (Sandbox Code Playgroud)

这会将名为"foo"的变量传递给page.tpl.php文件.