【问题标题】:Drupal 7 - How to load a template file from a module?Drupal 7 - 如何从模块加载模板文件?
【发布时间】:2011-07-15 09:01:29
【问题描述】:

我正在尝试在 Drupal 7 中构建自己的模块。

所以我创建了一个名为“moon”的简单模块

function moon_menu() {
  $items = array();
      $items['moon'] = array(
      'title' => '',
      'description' => t('Detalle de un Programa'),
      'page callback' => 'moon_page',
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK
  );

  return $items;
}

function moon_page(){


$id = 3;

$content = 'aa';

}

在moon_page() 函数中,我喜欢从我的主题文件中加载自定义模板“moon.tpl.php”。

这可能吗?

【问题讨论】:

    标签: drupal drupal-theming


    【解决方案1】:

    为了你自己的东西(不覆盖另一个模块的模板)?

    当然,您只需要:

    • hook_theme()注册您的模板

    • 调用主题('moon', $args)

    $args 是一个数组,其中包含由 hook_theme() 实现指定的模板参数。

    【讨论】:

    • // 我只是放了 print theme('moon',$content),但它显​​示一个空白页。我在这里做错了吗?
    • 如果您在页面回调中打印某些内容,则表示“我确实希望打印此内容。示例:文件、json、xml、...”。改用“return theme('moon', $args)”。是否更新了主题注册表(重新保存主题选择表单或使用 devel.module)。
    【解决方案2】:
    /*
     * Implementation of hook_theme().
     */
    function moon_theme($existing, $type, $theme, $path){
      return array(
        'moon' => array(
          'variables' => array('content' => NULL),
          'file' => 'moon', // place you file in 'theme' folder of you module folder
          'path' => drupal_get_path('module', 'moon') .'/theme'
        )
      );
    }
    
    function moon_page(){
    
      // some code to generate $content variable
    
      return theme('moon', $content); // use $content variable in moon.tpl.php template
    }
    

    【讨论】:

    • 谢谢,这对我有帮助。但需要一些修改。作为 moon_theme() 函数的返回数组,数组键“文件”应替换为“模板”,否则会在 Drupal 6 中产生错误
    • 'path' 在 drupal7 中运行良好,但是当使用 'path' 调用时,该文件实际上应该被称为 'theme/moon',没有任何扩展名。如果你使用'template',drupal7 会搜索'theme/moon.tpl.php'。
    【解决方案3】:

    对于 Drupal 7,它对我不起作用。我在 hook_theme 中替换了行

    'file' => 'moon', by 'template' => 'moon' 
    

    现在它对我有用。

    【讨论】:

      【解决方案4】:

      在 drupal 7 中使用时出现以下错误:

      return theme('moon', $content);

      导致“致命错误:第 1071 行的 drupal_install\includes\theme.inc 中不支持的操作数类型”

      这已通过以下方式修复:

      theme('moon', array('content' => $content));

      【讨论】:

        【解决方案5】:

        你可以使用moon_menu,和hook_theme

        <?php
        
        /**
         * Implementation of hook_menu().
         */
        function os_menu() {
          $items['vars'] = array(
            'title' => 'desc information',
            'page callback' => '_moon_page',
            'access callback' => TRUE,
            'type' => MENU_NORMAL_ITEM,
          );
          return $items;
        }
        
        function _moon_page() {    
          $fields = [];
          $fields['vars'] = 'var';
        
          return theme('os', compact('fields'));
        }
        
        /**
         * Implementation of hook_theme().
         */
        function os_theme() {
          $module_path = drupal_get_path('module', 'os');
        
          return array(
            'os' => array(
              'template' => 'os',
              'arguments' => 'fields',
              'path' => $module_path . '/templates',
            ),
          );
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多