【问题标题】:'Smart' way to include the same HTML in multiple TPL files?在多个 TPL 文件中包含相同 HTML 的“智能”方式?
【发布时间】:2015-06-15 14:17:40
【问题描述】:

我在多个视图 TPL 文件中使用相同的 HTML(如下)。它用于使我自己的自定义寻呼机与 JavaScript 一起使用。

目前我为每个视图创建一个 TPL 文件,然后每次都添加完整的标记。从维护的角度来看,这并不是很好,就好像我需要更改每个实例都需要这样做的 HTML。

有没有“更聪明”的方法来做到这一点?例如,我可以创建一个新的 TPL 文件来容纳此 HTML,并在需要时在每个视图 TPL 文件中调用该文件吗?

  <div class="picThumb">
    <div class="picThumb-inner">
      <div class="picThumb-trig picThumb-trigLeft">Previous</div>
      <div class="picThumb-trig picThumb-trigP1">1</div>
      <div class="picThumb-trig picThumb-trigP2">2</div>
      <div class="picThumb-trig picThumb-trigRight">Next</div>
    </div>
  </div>

【问题讨论】:

  • 不知道,这是否朝着正确的方向发展,但如果我猜对了,你想使用类似 sn-ps 的东西:drupal.org/node/1288700
  • 它更像是一个 HTML sn-p 而不是 PHP。有没有办法创建新的 TPL 文件?

标签: drupal-theming


【解决方案1】:

这里最好的选择是通过hook_theme()注册一个自定义模板:

/**
 * Implements hook_theme().
 */
function THEME_OR_MODULE_theme() {
  return array(
    'custom_template'  => array(
      'template' => 'custom-template',
      'variables' => array(),
    ),
  );
}

然后将您的自定义 HTML 放入 custom-template.tpl.php 文件中。

然后你可以使用theme()函数在任何地方“包含”它:

<?php print theme('custom_template') ?>

【讨论】:

  • 这对我不起作用。我已将 THEME_OR_MODULE 更改为我的主题名称并将其放在我的 template.php 中。我的新文件位于模板目录中,名为 custom-template.tpl。我已将 放入各种 TPL 文件中,但没有任何反应。
  • 应该叫“custom-template.tpl.php”,而不是“custom-template.tpl”!还要确保清除缓存。
  • 这是一个错字,文件名为 custom-template.tpl.php。我放入的所有内容都是

    Test

    。我已经多次清除缓存。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多