2011-10-07 120 views
2

我意识到这个问题已被问到,但我要么根本不明白或以前的答案不适用(或者我不明白如何应用它们)对我的情况。这里所说:Drupal:从自定义模块传递自定义变量到我的模板

我有一个名为自定义模块:

中/网站/所有/模块/自定义/ my_module “my_module”

我有一个模块文件:

/网站/所有/modules/custom/my_module/my_module.module

我有一个页面模板名称“页,我的空间”,这是不是我的模块中:

/网站/所有/主题/ 的MyTheme /页 /page-mypath-mypage.tpl.php

我给这个钩子菜单:

$items['mypath/mypage'] = array(
    'title'   => 'My Page!', 
    'page callback'   => 'my_module_mypage', 
    'page arguments' => array(1,2), 
    'access callback' => true, 
    'type'   => MENU_CALLBACK, 
); 

在功能方面,我建立了一些内容,像这样:

function my_module_mypage($x, $y) { 
    $output = "foo AND bar!"; 
    return $output; 
} 

在模板(再次,而不是在我的模块文件夹,但在主题子文件夹“页”,我有:

<?php print $content ?> 

当我去http://mysite/mypath/mypage我得到“富和酒吧!”

现在的问题。我想要一个名为'$ moar_content'的my_module_mypage()中定义的新变量。我想在我的页面输出$ moar_content-mypath-mypage.tpl.php。我只需要为此模块和此模板执行此操作。我不需要它在主题范围内,所以我不认为使用mytheme的'template.php'是适当的。

我想我需要使用某种预处理,但我尝试的所有事情都失败了,而且我读的所有东西似乎都缺少某种神奇的成分。

我的想法是:

function my_module_preprocess_page_mypath_mypage(&$variables) { 
    $variables['moar_content'] = 'OATMEAL'; 
} 

function my_module_preprocess_my_module_mypage(&$variables) { 
    $variables['moar_content'] = 'OATMEAL'; 
} 

什么的。我很确定自己走在正确的轨道上,但是我正在打砖墙。

+0

巨大取决于你使用的Drupal的版本 - 6或7? – Clive

+0

Drupal 6.对不起,我应该提前说过。 – Spanky

回答

1

做的工作,你必须遵循Drupal的最佳实践,假定你正在使用D6,让您可以插入一些变量到您的模板是这样的:

// You menu path is good 
$items['mypath/mypage'] = array(
    'title' => 'My Page!', 
    'page callback' => 'my_module_mypage', 
    'page arguments' => array(1,2), 
    'access callback' => true, 
    'type' => MENU_CALLBACK, 
); 

第二件事,我们定义为主题挂钩我们的页面

// We define here a new theme file for your your page 
// Your theme file must be located in your module's folder 
// you can use a subfolder to group all your module's theme files 
// E.g : themes/my-module-theme.tpl.php 
// Note that in theme files, we change _ by - 
function my_module_theme() { 
    return array(
     'my_module_theme' => array(// Keep that name in your mind 
      'template' => 'my_module_theme', 
       'arguments' => array(
       'my_var' => NULL, 
       'my_var2' => NULL, 
      ), 
     ) 
    ); 
} 

现在我们可以创建我们的模块的根文件夹中的文件“我的模块,theme.tpl.php”,并贴上类似“foo和吧!” 回到我们的my_module。模块,回调应该是这样的:

function my_module_mypage($x, $y) { 
    // $x and $y are optionnal, so this is the first manner 
    // to inject variables into your theme's file 
    $output = theme("my_module_theme", $x, $y); 
    return $output; 
} 

您也可以使用预处理挂钩插入变量

// The hook must be named like this : template_preprocess_NAME_OF_THEME_FILE 
// where NAME_OF_THEME_FILE is the name that you kept in your mind ;) 
function template_preprocess_my_module_theme(&$variables) { 
    // Do some job 
    $var1 = 'Foobar'; 

    // So in "my-module-theme.tpl.php", $my_var1 will print Foobar 
    $variables['my_var1'] = $var1; 
} 
+0

我的页面模板不在我的模块目录中。他们在主题/ mytheme /页面。其中很多。不幸的是,这就是交给我的东西,所以我必须与之合作。 – Spanky

+0

对不起,但这个答案不回答我的问题。这是我能在其他地方找到的答案。如果我按照这些说明操作,那么我的模块文件夹中会有一个模板,但我仍然无法在/ sites/all/themes/mytheme/pages/page-mypage中的页面模板上下文中呈现该模板。 tpl.php - 所以我仍然陷入困境。谢谢,虽然:) – Spanky