【问题标题】:Invoke and include a separate .php from within a callback for a drupal 6 page在 drupal 6 页面的回调中调用并包含一个单独的 .php
【发布时间】:2011-04-06 06:44:45
【问题描述】:

我是 Drupal (v6) 和 PHP 的新手,我正在尝试通过自定义模块实现一些内容。我遵循了一个教程,并弄清楚了如何让 Drupal 了解我的模块,甚至为我的自定义页面注册了一个 URL。它按我的意图出现在导航中——到目前为止一切都很好。

这对于我的玩具示例非常有用……但对于我真正打算编写的页面来说不太好。现在我有这个:

function twtevents_menu() {

  $items = array();

  $items['gingerbread'] = array(
    'title' => 'Gingerbread Gallery',
    'page callback' => 'twtevents_gallery_gingerbread',
    'access arguments' => array('access twtevents content'),
    'type' => MENU_NORMAL_ITEM
  );

  return $items;
}

function twtevents_gallery_gingerbread() {
  // content variable that will be returned for display
  $page_content = '';

  $page_content = '<p>'.  t("Some super-cool content") .'</p>';

  return $page_content;
}

但我不想以$page_content = '&lt;p&gt;'. t("Some super-cool content") .'&lt;/p&gt;'; 的风格写一个大而复杂的页面——等等。

我想用更接近这个的样式来编写实际页面:

<div class="comment<?php print ($comment->new) ? ' comment-new' : ''; print ' '. $status ?> clear-block">
  <?php print $picture ?>

  <?php if ($comment->new): ?>
    <span class="new"><?php print $new ?></span>
  <?php endif; ?>

  <h3><?php print $title ?></h3>

</div>

php 代码被添加到 HTML 标记中,而不是相反。

在我的函数中,我可以成功调用 include($path),但是(当然)这种方法只是将我的页面的输出放在 broser 的左上角...我需要发送单独的页面作为我的回调函数的返回。

是否有一个 PHP 函数来解决这个问题? Drupal 函数?最佳做法?

【问题讨论】:

    标签: php drupal


    【解决方案1】:

    要在模块中使用您的自定义模板文件,您应该提供您的 hook_theme 实现。下面是来自advanced forum 模块的示例。

    function advanced_forum_theme() {
      // ...
      $items['advanced_forum_topic_header'] = array(
          'template' => 'advanced_forum-topic-header',
          'arguments' => array(
            'node' => NULL,
            'comment_count' => NULL,
            )
      );
      // ...
      return $items;
    }
    

    这就是调用钩子的方式:

      // Build the topic header
      $variables['topic_header'] = theme('advanced_forum_topic_header',
                                          $variables['node'],
                                          $variables['comment_count']);
    

    包含布局的模板文件是advanced_forum-topic-header.tpl.php

    更多详细信息的一些链接:"Using hook_theme to style module output"hook_theme description for Drupal 6

    【讨论】:

      【解决方案2】:

      正如 Kniganapolke 所说,您需要一个钩子主题实现来调用模板;

      类似的东西;

      function twtevents_theme(){
          return array(
              'template' => 'twtevents_gallery_gingerbread',
              'arguments' => array()
          ); }
      

      然后将您的模板代码放入模块文件夹中名为 twtevents_gallery_gingerbread.tpl.php 的文件中。

      然后更新你的页面回调,像这样调用主题函数;

      function twtevents_gallery_gingerbread(){
          return theme('twtevents_gallery_gingerbread');
      }
      

      现在很重要的一点是 - 将主题挂钩添加到模块后,您必须清除 Drupal 缓存,否则它将找不到您的新模板。

      【讨论】:

      • 谢谢!两个答案都非常有帮助......希望我能“接受”两者。作为一个 PHP/Drupal 新手,你的代码示例更适合我的情况并且更完整(显示完整的 twtevents_gallery_gingerbread() 函数),这有很大的不同。
      猜你喜欢
      • 2014-10-06
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多