Drupal 7 - 从 hook_theme 获取变量 [英] Drupal 7 - get variables from hook_theme

查看:38
本文介绍了Drupal 7 - 从 hook_theme 获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将变量从自定义模块传递到 tpl 文件.在我的自定义模块中(命名示例)

I tried to pass a variable from a custom module to a tpl file. In my custom module (named example)

1.我通过 hook_menu 创建了一个带有参数的路由:

function example_menu() {
  $items['example/fancybox-photos/%'] = array(
        'page callback' => 'example_display_fancybox_photos',
        'page arguments' => array(2),
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
      );
    return $items;
    }

<强>2.我创建了我的页面回调函数:

function example_display_fancybox_photos($nid) {
  $nodePhoto = node_load($nid);
  $field_photo = field_get_items('node', $nodePhoto, 'field_photo');

  $photo = [
  "field_photo" => $field_photo[0]['uri'],
  ....
  ];

  return theme('example_fancybox_photos', array('infosPhoto' => $photos));
}

3 .我创建了一个 hook_theme

function example_theme() {
  $themes = array();

  $themes['example_fancybox_photos'] = array(
    'template' => 'templates/example-fancybox-photos',
    'variables' => array('infosPhoto' => NULL),
  );

  return $themes;
}

4 .我终于在模板文件夹(在主题文件夹中)中创建了一个名为example-fancybox-photos.tpl.php"的tpl

<pre><?php print var_dump($infosPhoto); ?></pre>

结果为空

我做了一些研究,但我不明白为什么这个变量仍然是 NULL.

I did some researchs but i dont understand why the variable is still NULL.

感谢您的帮助!

推荐答案

你已经传递了 $photos 但你有一个数组作为 $photo.尝试改变它

You have passed $photos but you have array as $photo. try to change that

希望下面的代码对你有所帮助.

Hope the below code helps you.

function example_menu(){
    $items['example/fancybox-photos/%'] = array(
        'page callback' => 'example_display_fancybox_photos',
        'page arguments' => array(2),
        'type' => MENU_CALLBACK,
        'access arguments' => array('access content'),
    );
    return $items;
}

function example_display_fancybox_photos($nid){
    $photos = 'value from example module!';
    return theme('example_fancybox_photos',array('photos' => $photos));
}

function example_theme() {
    $path = drupal_get_path('module', 'example');
    return array(
        'example_fancybox_photos' => array(
            'variables' => array('photos' => null),
            'template' => 'example_fancybox_photos',
            'path' => $path, 
        ),
    );
}

将您的 tpl 文件 example_fancybox_photos.tpl.php 放在您的模块目录中,并在其中使用以下代码.

Place your tpl file example_fancybox_photos.tpl.php in your module directory and inside it use the below code.

<?php print $photos; ?>

function example_theme() {
    return array(
        'example_fancybox_photos' => array(
            'variables' => array('photos' => null),
            'template' => 'example_fancybox_photos',
        ),
    );
}

将您的 tpl 文件 example_fancybox_photos.tpl.php 放在您的主题目录中,并在其中放置以下代码

Place your tpl file example_fancybox_photos.tpl.php in your theme directory and inside it place the below code

<?php print $photos; ?>

这篇关于Drupal 7 - 从 hook_theme 获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆