1

这是一个总是困扰我的好人。

我很想看到D6和D7的解决方案。

我创建自定义模块,通常使用菜单api创建菜单类型 - > MENU_CALLBACK。

这是一个例子:

function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

我想看到确切的代码解决方案,根据以下代码提供三个不同的变量打印在自定义主题模板中。要添加到该挑战,我希望代码只能在菜单回调函数中编写。我知道有很多可能的方法可以做到但却无法弄清楚如何在菜单回调函数中执行此操作,这将是最有用的方法。

//****************************************
// This code block goes in a custom module called example_menu_variables.module

// hook_menu example
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

// hook_meny callback function
function example_page1_callback() {
  // please provide code within this function to turn these next three
  // variables into separate output variables for the template.
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');
  return;
}
//****************************************



//****************************************
// This block of code represents the part of the custom theme template related to the menu location from the hook_menu call
// D6 template name would be page-example-page1.tpl.php
// D7 template name would be page--example--page1.tpl.php

<div><?php print  $variable_to_print1?></div>
<div><?php print  $variable_to_print2?></div>
<div><?php print  $variable_to_print3[0] . ' ' . $variable_to_print3[1] . ' ' . $variable_to_print3[2]?></div>

/*
output should look like:
Foo
Bar
bing bang bong
*/

//****************************************

希望这是有人不介意解决的问题。谢谢你的时间。:)

雷詹姆斯

  • 如果我理解正确,那就是我的目的hook_theme()为什么代码需要包含在菜单回调函数中? -  Adam Balsam 2013年5月3日17:07 
  • 嗨亚当,谢谢你的回应。我想看看代码看起来两种方式都会很棒。使用hook_theme()需要大量的设置才能将一个自定义变量放入主题层。我想要一个简单的解决方案,可以在您从数据库收集数据以显示为输出的相同位置完成。拥有一个像这样发送变量的函数真的很棒 - > drupal_add_template_variable($ var,$ varname); 或类似的东西。我不确定Drupal中是否存在这样的事情。谢谢。 -  rayjamesfun 2013年 5月3日19:04

5答案   正确答案

1

我在Drupal 7中使用了这个,只需在页面回调中返回的render数组中设置变量即可。

在.module文件中

function example_page1_callback() {
$render_array['variable_to_print1'] = 'Foo';
$render_array['variable_to_print2'] = 'Bar';

// set other render array items for the page

return $render_array;
}

在page.tpl.php文件中

<div class="someDiv">
  1st variable = <?php print render($page['content']['system_main']['variable_to_print1']); ?><br />
  2nd variable = <?php print render($page['content']['system_main']['variable_to_print2']); ?><br />
</div>
  • 我的神人你做到了!你想通了!你完全摇滚。我有一个编辑。至少对于Drupal 7来说,system_main不是system-main。非常感谢。 -  rayjamesfun 2014年 1月23日20:29
6

我不相信你可以不使用hook_theme来做到这一点,但这正是它的用途,它非常简单。(此代码适用于Drupal 6.)

/**
 * Implements hook_menu().
 */
function example_menu() {
  $items['example/page1'] = array(
    'page callback'     => 'example_page1_callback',
    'access callback'   => TRUE,
    'type'              => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Page callback function
 */
function example_page1_callback() {
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  return theme('example_contents', $variable_to_print1, $variable_to_print2);
}

/**
 * Implements hook_theme().
 */
function example_theme() {
  return array(
    'example_contents' => array(
      'arguments' => array('variable_to_print1' => null, 'variable_to_print2' => null),
      'template' => 'example-contents',
    ),
  );
}

$variable_to_print1$variable_to_print2现在可用example-contents.tpl.php

  • 嗨亚当,谢谢你的代码。我把它放在一起并运行它但是有很多错误声明所有其他标准变量都是未声明的 - >注意:未定义的变量:include()中的徽标(C:\ xampp \ htdocs \ d7themeaddvar \ site \ themes的第92行\ bartik \ templates \ example-contents.tpl.php)我把文件放在一个git存储库中以便于下载 - > github.com/rayjames/d7themeaddvar -  rayjamesfun 2013年 5月3日 20:39 
  • content-example.tpl.php不会取代page.tpl.php只有定义的变量将在其提供(在这种情况下,$variable_to_print1$variable_to_print1)。如果你想为page.tpl创建一个新变量,你应该研究一下hook_preprocess() -  Adam Balsam 2013年5月3日20:47 
  • 嗯,我们似乎在这里走向不同的方向。我不确定你的代码会做什么。根据最初的问题,我要求代码在自定义模板中创建用于打印的变量,但我想我需要更具体,因为我假设每个人都会理解hook_menu项目将指向网站上的“页面”并且将存在变量可用的所述“页面”的自定义模板。我无法弄清楚如何让您的代码打印变量。谢谢。 -  rayjamesfun 2013年 5月3日21:03
  • 1
    @rayjamesfun凭借世界上最好的意志,你现在正走向完全错误的方向。Drupal有一个模板系统,如果你想利用你需要遵守的规则,亚当完美地概述了这个规则。如果你不这样做,那么你需要诉诸于我们无法进入的普通的PHP; 我们在这里提供Drupal解决方案,而不是绕过 Drupal的解决方案:) -  Clive ♦ 2013年5月3日21:53 
  • 嗨克莱夫。我听到了,我完全同意。我现在正试图绕过Drupal,只是寻找一个我已经想到的更简单的解决方案。我不能让Adam的代码工作。我不是Drupal的新手。我已经建立了一些非常复杂的网站,但似乎仍然无法弄清楚如何像忍者一样使用主题系统。只是在Drupal的方式上寻找一个完整的示例解决方案。谢谢。 -  rayjamesfun 2013年 5月3日22:02
2

Adam编写的代码是正确的(我的+1)您的变量在example-contents.tpl.php文件中可用 - 它是模块输出的模板文件。

但是,如果您需要在page.tpl文件中使用这些变量,那么您可以创建它们:

在您的模块文件中:

/**
 * Page variables
 *
 * @return
 *  custom page variables
 */ 
function custom_variables(){

  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');

  return array( 'variable_to_print1'    => $variable_to_print1,
                'variable_to_print2'    => $variable_to_print2,
                'variable_to_print3'    => $variable_to_print3
  );
}

在你的template.php文件中:

/**
 * Override or insert variables into the page templates.
 *
 * @param $variables
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("page" in this case.)
 */
function YOURTHEME_preprocess_page(&$variables, $hook) {

  // pass your module variables to page--example1.tpl.php file
  if (in_array('page__example1', $variables['theme_hook_suggestions'])){
    if (module_exists('your_module')){
        $your_module_variables = custom_variables();
        $variables['variable_to_print1'] = $your_module_variables['variable_to_print1'];
        $variables['variable_to_print2'] = $your_module_variables['variable_to_print2'];
        $variables['variable_to_print3'] = $your_module_variables['variable_to_print3'];
    }
  }
}
  • 嗨杰克,这是D6还是D7解决方案?我想知道你是否可以链接到教程或Drupal页面,它可以更好地解释你的代码中发生了什么。我假设您不知道可以在菜单回调中实现的解决方案,对吗?我还假设您可以/可以为每个“页面”编写不同的函数,以便保持变量的组织。这似乎不适用于具有动态页面参数的页面。谢谢。 -  rayjamesfun 2013年 5月4日2:08
  • 1
    所有上述例子都很完美。我对D7解决方案的回答。您的页面是否具有动态参数并不重要。您的变量将在每个“example1”页面上可用(“example1”它是一种内容类型)如果您在每个内容类型页面上都需要它们,那么只需从我的示例中删除preprocess_page中的第一个IF语句。您可以在任何地方(例如在模块中)创建变量,但是,它们必须通过YOURTHEME_preprocess_page函数,因为它是将变量定义为页面变量的位置。 -  Jack-PL 2013年5月4日12:59
1

有同样的问题,这对Drupal 7.x有用

function example_menu() {
  $items['example/page1'] = array(
  'page callback'     => 'example_page1_callback',
  'access callback'   => TRUE,
  'type'              => MENU_CALLBACK,
  );
return $items;
}


function example_page1_callback() {
  $variable_to_print1 = 'Foo';
  $variable_to_print2 = 'Bar';
  $variable_to_print3 = array('bing', 'bang', 'bong');

  //use below given statement when you only want to pass one variable
  // to your template
  //return theme('example',array('output'=>$variable_to_print1));

  //use this statement when you want to pass array
  return theme('example',array('output' => $variable_to_print3)); 
}

除此之外,您还需要一个example_theme()钩子

function example_theme(){
    //lets say your template file is called example.tpl.php and it resides
    //within your module directory.
    $custom_example_template = array();

    $custom_example_template['example'] = array(
        'template' => 'example',
        'variables' => array('output' => NULL)
    );
}

那你的example.tpl.php怎么样?

<div id='someotherid'>
    <?php
             //use this code when you have passed only one variable
             print $variables['output'];
    ?>
</div>

您可以根据需要为变量命名,不需要像我一样坚持使用“输出”。出于某种原因,你无法看到我的foreach循环遍历我们传递的数组。

0

我得到了Jack的工作方法,尽管我必须说render_array的想法更简单。这对我有用:

在模块中:

//page callback to create the page and include some CSS & Javascript
function my_callback() {
   drupal_add_css(...); 
   drupal_add_js(...); 
   return '';   
}

function _my_module_vars(){
   $var1 = 'foo'
   $var2 = 'bar'
}

在主题的Template.php中

function MYTHEME_preprocess_page(&$vars) {
   //add some custom variables to the module page
    if(in_array('page__my_content_type', $vars['theme_hook_suggestions'])) { 
       if(module_exists('my_module')) { 
          $my_vars = _my_module_vars();  
          $vars['var1'] = $my_vars['var1'];
          $vars['var2'] = $my_vars['var2'];
    }
  }
}

在page.tpl.php中(或者您可以使用page - my_content_type.tpl.php):

print $var1; 
print $var2; 
  • 只是为了澄清,我也想这样做的原因(与模块方法中的hook_theme相反)就是这里,变量在page.tpl.php文件中可用。在hook_theme方式中,模块模板文件被渲染到页面的内容变量中(如果我记得正确的话),这使得很难对它做很多事情。 -  wheelercreek  2013年11月14日17:43

来自  https://drupal.stackexchange.com/questions/72028/create-custom-variables-for-a-template-file-inside-a-menu-callback-function