Drupal:将自定义变量从自定义模块传递到我的模板
我知道有人问过我这个问题,但我要么就是不明白,要么就是前面的答案不适用于我的情况(或者我不知道如何应用它们)。下面是: 我有一个自定义模块,名为: /sites/all/modules/custom/my_module中的“我的模块” 我有一个模块文件: /站点/all/modules/custom/my_module/my_module.module 我的模块中没有页面模板名“page mypage”: /sites/all/themes/myteme/pages/page-mypath-mypage.tpl.php 我为这个制作了钩子菜单:Drupal:将自定义变量从自定义模块传递到我的模板,drupal,drupal-6,preprocessor,drupal-modules,customization,Drupal,Drupal 6,Preprocessor,Drupal Modules,Customization,我知道有人问过我这个问题,但我要么就是不明白,要么就是前面的答案不适用于我的情况(或者我不知道如何应用它们)。下面是: 我有一个自定义模块,名为: /sites/all/modules/custom/my_module中的“我的模块” 我有一个模块文件: /站点/all/modules/custom/my_module/my_module.module 我的模块中没有页面模板名“page mypage”: /sites/all/themes/myteme/pages/page-mypath-my
$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;
}
在模板中(同样,不是在我的模块文件夹中,而是在主题子文件夹“页面”中),我有:
或
或者别的什么。我很确定我的思路是对的,但我遇到了麻烦。要完成这项工作,你必须遵循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,
),
)
);
}
现在我们可以在模块的根文件夹中创建一个文件“my module theme.tpl.php”,并粘贴类似“foo and bar!”
回到my_module.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;
}
很大程度上取决于您使用的Drupal版本是-6还是7?Drupal 6。对不起,我应该在前面提到这一点。我的页面模板不在我模块的目录中。它们在themes/mytheme/pages中。很多。不幸的是,这是交给我的,所以这是我必须使用的。抱歉,但这个答案没有回答我的问题。这这是我在其他任何地方都能找到的答案。如果我按照这些说明操作,我的模块文件夹中就会有一个模板,但我仍然无法在/sites/all/themes/myteme/pages/page-mypage.tpl.php中的页面模板上下文中呈现该模板-所以我仍然被卡住了。不过,谢谢:)
// 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,
),
)
);
}
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;
}