为 drupal 中的自定义块创建自定义模板文件 [英] Create a custom template file for a custom block in drupal

查看:28
本文介绍了为 drupal 中的自定义块创建自定义模板文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建自定义 .tpl 文件以对自定义块进行主题化的 drupal 方法是什么?具体来说,我正在尝试以编程方式创建一个块,然后找到一种方法将视图代码与模块 php 代码分开.如果它是一个页面,Drupal theme() 将是实现这一目标的非常有效的方法.但是我找不到对自定义块做同样事情的 Drupal 方法是什么.我试过使用 hook_theme() 没有运气.

What is the drupal way to create a custom .tpl file for theming a custom block? Specifically I m trying to create a block programmatically and then find a way to separate the view code from the module php code. If it was a page the Drupal theme() would be very efficient way to achieve this. However I can't find what is the Drupal way to do the same thing for custom blocks. I 've tried to use the hook_theme() with no luck.

    //implementation of hook_block_info
    function mymodule_block_info() {
      $blocks = array();
      $blocks['myblock'] = array(
        'info' => t('My Block Title'),
      );

      return $blocks;
    }

    //implementation of hook_block_view
    function mymodule_block_view($delta='') {
      $block = array();

      switch($delta) {
        case 'myblock' :
          $block['content'] = mymodule_get_block_view();
          break;
      }
      return $block;
    }

    function mymodule_get_block_view(){
        $variables=array();
        return theme('mytemplate', $variables);

    }

    //implementation of hook_theme
    function codefactory_theme() {
      return array(
        'mytemplate' => array(
          'variables' => array(),
          'template' => 'mytemplate',
        ),
      );
    }

推荐答案

这似乎工作正常.

//implementation of hook_block_info
function mymodule_block_info() {
  $blocks = array();
  $blocks['myblock'] = array(
    'info' => t('My Block Title'),
  );

  return $blocks;
}

//implementation of hook_block_view
function mymodule_block_view($delta='') {
  $block = array();

  switch($delta) {
    case 'myblock' :
      $variables = array(); //do stuff here
      $block['content'] = theme('mytemplate', $variables);
      break;
  }
  return $block;
}


//implementation of hook_theme
function mymodule_theme() {
  return array(
    'mytemplate' => array(
      'variables' => array(),
      'template' => 'mytemplate',
    ),
  );
}

这篇关于为 drupal 中的自定义块创建自定义模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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