【问题标题】:Custom Drupal7 Module: How to use PHPTemplate engine for generating a custom template?自定义 Drupal7 模块:如何使用 PHPTemplate 引擎生成自定义模板?
【发布时间】:2016-03-18 20:23:27
【问题描述】:

我有点不确定,hook_theme 上的文档相当广泛。

我希望能够为我的模块提供一个自定义 .tpl.php(模板)文件以及一些变量,以生成/呈现一些可以通过电子邮件发送的 HTML。

我将如何处理这个问题?我假设我必须使用 hook_theme,但我有点不确定如何实际呈现和访问 HTML,而不在网站上的任何地方显示它。

【问题讨论】:

    标签: templates drupal drupal-7 drupal-modules


    【解决方案1】:

    我从另一个有用的来源找到了答案:

    /**
     * Implements hook_theme
     *
     * @param $existing
     * @param $type
     * @param $theme
     * @param $path
     * @return array
     */
    function MYMODULE_theme($existing, $type, $theme, $path)
    {
      return array(
        'name_of_theme_function' => array(
          'variables' => array('name_of_array' => array()),
          'template' => 'name-of-my-template',
          'path' => drupal_get_path('module', 'MYMODULE') . '/theme'
        ),
       )
    }
    
    /**
     * My Module Callback
     *
     * @return string
     * @throws \Exception
     */
    function MYMODULE_callback_function(){
      $output = array('array value', 'array value 2');
    
      $output = array(
        '#theme' => 'name_of_theme_function',
        '#name_of_array' => $output
      );
    
      return render($output);
    }
    
    /*** Inside the template name-of-my-template.tpl.php
    dpm($name_of_array)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      相关资源
      最近更新 更多