欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal page callback function data to tpl.php 传递数据变量到模板 有大用

在hook和tpl.php之间传递变量的正确方法    

几个月前我问了一个类似的问题,但事实是关于正确的做法的信息非常少。                                                    

最简单的是使用arg()函数,但正如你在文档中读到的那样,这是最错误的做法。试着按照正确的步骤,这是我到目前为止:                                                    

包含我们需要的路径的菜单:                                                    

/*
	 * Category search section
	*/
	$items['products/search/%/%'] = array(
			'title'             => 'Products search filtering',
			'description'       => 'Show review categories.',
			'page callback'     => 'ThemeSearchProducts',
			'page arguments'    => array(2,3),
			'access callback'   => 'user_access',
			'access arguments'  => array('access content'),
			'type'              => MENU_NORMAL_ITEM,
			'file'              => 'includes/mymodule_reviews.inc',
	);

                                                   

这些论点是通过......论证发出的(抱歉这个笑话)。到themesearchproducts函数                                                    

/**
 * THEME List of reviewable products
 *
 */
function ThemeSearchProducts($arg1, $arg2){

// 	$output = theme('search_products', $arg1);
  $output = theme('search_products', 'test');

  echo 'args in theme: ' . $arg1 . $arg2 . ' || ';
  
  
  return $output . theme('pager', 10);
}
                                                   

回声工作正常,但现在问题。如何将此变量发送到模块的hook_theme,并从那里发送到tpl.php?我有类似的东西:                                                    



function MYMODULE_theme ($existing, $type, $theme, $path) {
	$path = drupal_get_path('module', 'MYMODULE'); // . '/templates';
	// ADD THE templates DIRECTORY
	$path = $path . '/templates'; 
	
	
	return array(
			// as in 'theme('verbose_method',...)
			'main_reviews' => array(
			'template' => 'MYMODULE-main_page_reviews',
			'path' => $path,
			'variables' => array('forums' => NULL, 'topics' => NULL),
			),
			

                                                   

正如您所看到的,模块现在调用templates / MYMODULE-main_page_reviews.tpl.php中的tpl,但是我找不到从此处将变量发送到hook_theme的方法。向MYMODULE_theme添加一个新参数会引发错误,我无法摆脱它在ThemeSearchProducts中添加例如这个新参数,如下所示:                                                    

$output = theme('search_products', 'test');
                                                   

我认为使用variable_set也是错误的,不是吗?我试图尽可能地遵循“Drupal正确方法”,但我无法理解如何做到最后一步。                                                    

非常感谢你提前                                                    

评论                                    

alexmoreno的头像                                        

                                       

好吧,我终于明白了:-)。                                                        

我的_theme功能错了。我使用的是变量而不是参数。这就是它现在的样子:                                                        

// LIST OF PRODUCTS, POSSIBLY FILTERED
			'search_products' => array(
					'template' => 'bounty-search_products',
					'path' => $path,
					'arguments' => array('argument1' => NULL, 'argument2' => NULL),
			),
                                                       

然后,从以前的功能我有:                                                        

function ThemeSearchProducts($arg1, $arg2){

  $output = theme('search_products', array('argument1' =>$arg1, 'argument2' =>"test"));

  return $output . theme('pager', 10);
}
                                                       

theme()是神奇的。它将参数发送到一个数组中,可以在tpl.php中读取它:                                                        

dpm($argument1);                                                        

或者,当然:                                                        

echo '<br>argument 1::';
echo $argument1['argument1'];

echo '<br>argument 2::';
echo $argument1['argument2'];






来自  https://www.drupal.org/forum/support/module-development-and-code-questions/2013-02-07/correct-way-of-passing-variables

                                                   



2

我正在开发一个我使用的模块hook_menu

我知道我可以向'page callback'传递一个返回带有HTML / PHP代码的字符串的函数。

function mymodule_menu(){
    $items = array();

    $items['test'] = array(
        'title' => 'page test',
        'description' => 'my test',
        'page callback' => 'my_page',
        'access arguments' => array('access content'),
        'type' => MENU_NORMAL_ITEM,
        'menu_name' => 'main-menu',
    );

    return $items;
}

function my_page(){
    return '<h1>hello</h1>';
}

如何将整个PHP / HTML页面作为参数传递'page callback' => '[my_page.php]'而不是在my_page()函数内创建字符串

1答案  正确答案

7

“页面回调”的值只能是函数名称。

如果你必须使用'一个完整的PHP / HTML页面',那么使用hook_theme来解决。

<?php
function my_example_menu() {
  $items['my/example'] = array(
    'title' => 'My title',
    'page callback' => 'my_example_page', /* callback function */
'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
);

return $items;
}

/**
 * Implements hook_theme
 *
 */
function my_example_theme($existing, $type, $theme, $path) {
  return array(
    'the_html_page' => array(
    'template' => 'the-html-page', /* create file with this name + .tpl.php */
  ),
);
}

function my_example_page(){
  return theme('the_html_page');
}
?>

hook_theme()中'template'的值是文件的名称。创建扩展名为.tpl.php的模板文件,并将其放在与模块相同的目录中。您可以在文件中放置所需的任何html。

  • 如果我在'the-html-page'中有一个表格,我怎么接受它的回调? -  NickF 2014年 2月9日16:18
  • 你为什么要在'-html-page'中有一个表格?如果回调表单是您的目标,请将菜单项的“页面回调”设置为drupal_get_form,然后使用hook_form()来定义表单。 -  sri20198 2014年 2月20日11:00

来自  https://drupal.stackexchange.com/questions/101659/pass-php-page-to-page-callback-with-hook-menu



aaaa
普通分类: