【问题标题】:Drupal 7 Dynamic ThemingDrupal 7 动态主题
【发布时间】:2012-01-02 20:21:32
【问题描述】:

我正在尝试开发一个模块,其输出将以与 Views 模块非常相似的方式进行主题化,但我似乎无法让它工作。我关注了使用主题层 (http://drupal.org/node/933976) 并搜索了drupal 论坛无济于事。

主题钩子在 hook_theme 中定义为

'beerxml_hop' => array (
    'template' => 'beerxml-hop',
    'render element' => 'beerxml',
    'pattern' => 'beerxml_hop__',
    'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)

我通过

调用主题钩子
print render($element);

node--beer.tpl.php(beer 是内容类型名称)中,$element 是带有#theme 的渲染数组

array(3) {
    [0] => string(19) "beerxml_hop__simcoe"
    [1] => string(11) "beerxml_hop"
    [2] => string(15) "beerxml_unknown"
}

被调用的模板是beerxml_hop,而不是我希望的beerxml_hop__simcoebeerxml-hop--simcoe.tpl.phpbeerxml-unknown.tpl.php 都存在于同一目录中,beerxml-hop.tpl.phpbeerxml-unknown.tpl.php 在输出的其他地方使用。

我错过了什么? :)

【问题讨论】:

    标签: drupal drupal-7 drupal-modules drupal-theming


    【解决方案1】:

    Drupal 不在模块文件夹内搜索具有动态部分的模板。您必须使用几行代码手动完成:

    /**
     * Implements hook_theme_registry_alter().
     */
    function MY_MODULE_theme_registry_alter(&$registry) {
      $path = drupal_get_path('module', 'MY_MODULE') . '/subfolder/with/templates';
      $registry += drupal_find_theme_templates($registry, '.tpl.php', $path);
    }
    

    但是,这个技巧有一些限制:

    • 您不能对模板使用单独的预处理,只会启动基本预处理。
    • 模板文件的扩展名是硬编码的。

    【讨论】:

      【解决方案2】:

      您的模式必须与您的第一个 $element['#theme'] 选项匹配

      你可以试试

      'beerxml_hop' => array (
          'template' => 'beerxml-hop',
          'render element' => 'beerxml',
          'pattern' => 'beerxml_hop__[a-z]+',
          'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
      )
      

      【讨论】:

      • 是的,我还认为模式中有一些关于正则表达式的东西。它是正常的PHP正则表达式语法吗?但是,我无法让上面的示例正常工作。并且查看 Views 模块的源代码,它们的主题挂钩中没有任何正则表达式的迹象。这是 Views 模块的钩子示例:$hooks['views_exposed_form'] = $base + array( 'template' => 'views-exposed-form', 'pattern' => 'views_exposed_form__', 'render element' => 'form', );
      【解决方案3】:

      hook_theme_registry_alter 的实施是解决问题的关键。

      另一件非常重要的事情是避免在模板名称中使用“-”!

      例如,这是行不通的:

      'beerxml-hop' => array (
          'template' => 'beerxml-hop',
          'render element' => 'beerxml',
          'pattern' => 'beerxml-hop__',
          'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
      )
      

      这是关键(尽管在问题中):

      • 在主题挂钩名称 ('beerxml_hop') 和模式 ('beerxml_hop__') 中使用“_”
      • 但“模板”参数中的“-”(“beerxml-hop”)和模板文件名(“beerxml-hop--something.tpl.php”)

      .

      'beerxml_hop' => array (
          'template' => 'beerxml-hop',
          'render element' => 'beerxml',
          'pattern' => 'beerxml_hop__',
          'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
      )
      

      渲染 beerxml-hop--something.tpl.php 文件然后应该使用:

      echo theme('beerxml-hop--something', array('n' => 10));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多