【问题标题】:Create a custom template file for a custom block in drupal为 drupal 中的自定义块创建自定义模板文件
【发布时间】:2013-05-15 16:40:30
【问题描述】:

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

    //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',
        ),
      );
    }

【问题讨论】:

    标签: drupal-7 drupal-theming


    【解决方案1】:

    它遵循以下建议:block_MODULE_DELTA。按照上面的示例,如果您只有一个块,我会尝试将文件命名为 block--mymodule.tpl.php,或者 block--mymodule--1.tpl.php 如果你有多个。

    参考:api.drupal.orgdrupal.org

    【讨论】:

      【解决方案2】:

      这似乎工作正常。

      //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',
          ),
        );
      }
      

      【讨论】:

        猜你喜欢
        • 2016-10-05
        • 1970-01-01
        • 2018-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多