欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal 7 drupal7 d7 drupal 8 drupal8 d8 模板文件中,要嵌套 包含另一个模板文件。 有大用 有大大用

在 page.tpl.php  中 要嵌套 a.tpl.php 文件。 1、同级文件夹下面: include'a.tpl.php'); 2、template/block 下面: include'block/b.tpl.php');


来自  https://blog.csdn.net/bingzi_822/article/details/7550869



Drupal8 模版包含


路径为:your_site.com/themes/theme_name/templates/includes/

{% include '@theme_name/includes/header.html.twig' %}

不推荐以下方式:

{% include directory ~ '/includes/header.html.twig' %}

官方文档:including-part-template


来自  https://blog.csdn.net/weixin_33995481/article/details/89028492

来自  https://segmentfault.com/a/1190000010511181


我可以在模板中包含其他模板吗?

3

我在自定义主题na(templates/block--[module]--[delta].tpl.php)中覆盖了模板,并且我想在此包含其他模板(来自同一目录)。我可以这样做吗?

目录结构

sites\all\themes\
   my_name_of_theme\
        templates\
             block--views--front-page-block-1.tpl.php
             example-file-template.tpl.php

block--views--front-page-block-1.tpl.php (我想做什么):

<?php 
     /* init $path (or maybe it is not needed) */
     include($path . 'example-file-template.tpl.php');
?>
  改善这个问题   

2个答案    正确答案

11

你可以得到另一个Drupal主题路径与drupal_get_path('theme', 'theme_name');最后

<?php 
     include(drupal_get_path('theme', 'theme_name').'/example-file-template.tpl.php');
?>
  改善这个答案   
0

您可以使用主题的路径(使用当前允许的主题):

<?php
include(path_to_theme().'/templates/TEMPLATE_NAME.tpl.php');
?>

或Drupal获取路径(您可以设置要使用的主题):

<?php
    include(drupal_get_path('theme', 'THEME_NAME').'/templates/TEMPLATE_NAME.tpl.php');
?>
  改善这个答案   

你的答案

来自  https://drupal.stackexchange.com/questions/129187/can-i-include-other-template-into-template


在您自己的模块中使用模板(.tpl.php)文件

上次更新时间: 
2018年9月13日

该文档不完整添加更多信息

通常(.tpl.php)模板文件位于主题文件夹中

但是有时自定义模块可能需要覆盖Drupal提供的默认模板文件-页面,节点等。弄清楚如何做到这一点可能很棘手,因此本页面将介绍Drupal 5、6、7和8的方法。

为什么要覆盖模块而不是主题中的模板文件?
有多种原因使模块需要控制如何呈现内容。模块可以提供具有定制节点模板的新内容类型。对于“打印”模块,有必要排除页眉,页脚和侧栏,然后添加一些其他元素。

Drupal 6和7的方法类似:告诉主题系统在模块目录中查找模板以及主题目录。在两种情况下,都可能需要限制使用自定义模板的时间。

即,如果模块具有自己的page.tpl.php,则可能不应将其用于站点上的每个页面。

这可以通过以仅在某些情况下使用的方式命名自定义模板来完成。 

为了进行更好的控制,也可以使用预处理功能来完成。

有关模板建议的其他文档,请参阅以下页面。

Drupal 8

这是通过使用HOOK_theme()注册模板来完成的。

/**
 * Implements hook_theme().
 */
function MODULE_theme() {
  $theme['paragraph__table'] = [
    'base hook' => 'paragraph',
  ];

  return $theme;
}

可以将其与hook_theme_suggestions_HOOK()结合使用以提供独特的模板建议。

/**
 * Implements hook_theme_suggestions_HOOK().
 */

function MODULE_paragraphs_theme_suggestions_paragraph(array $variables) {
  $suggestions = [];

  $suggestions[] = 'paragraph__table';

  return $suggestions;
}

然后将相应的TWIG模板复制到模块模板目录中,并重命名为与您的模板建议名称匹配,并用连字符替换所有下划线。

即从这个例子中,文件将是/templates/paragraph--table.html.twig

Drupal 7

Drupal 7方法是从向Drupal 7主题注册表添加模块路径而改编而成

首先,在模块目录中创建一个模板文件。该文件需要两个连字符:

节点--mymodule.tpl.php

下一步是告诉主题系统在那里。

/**
 * Implements hook_theme_registry_alter().
 */
function my_module_theme_registry_alter(&$theme_registry) {
  // Defined path to the current module.
  $module_path = drupal_get_path('module', 'my_module');
  // Find all .tpl.php files in this module's folder recursively.
  $template_file_objects = drupal_find_theme_templates($theme_registry, '.tpl.php', $module_path);
  // Iterate through all found template file objects.
  foreach ($template_file_objects as $key => $template_file_object) {
    // If the template has not already been overridden by a theme.
    if (!isset($theme_registry[$key]['theme path']) || !preg_match('#/themes/#', $theme_registry[$key]['theme path'])) {
      // Alter the theme path and template elements.
      $theme_registry[$key]['theme path'] = $module_path;
      $theme_registry[$key] = array_merge($theme_registry[$key], $template_file_object);
      $theme_registry[$key]['type'] = 'module';
    }
  }
}

自定义模块中内容类型的模板

作为上述解决方案的替代方案,可以实现hook_theme将内容类型的模板添加到自定义模块。

    /**
     * Implements hook_theme().
     */
    function mymodule_theme($existing, $type, $theme, $path) {
      $theme = array();
      $theme['node__blog_post'] = array(
        'render element' => 'content',
        'base hook' => 'node',
        'template' => 'node--blog_post',
        'path' => drupal_get_path('module', 'mymodule') . '/templates',
       );
      return $theme;
    }

注意:此处的定制模块称为“ mymodule”,定制内容类型称为“ blog_post”。使用的tpl.php称为“ node--blog_post.tpl.php”,它位于自定义模块的“ templates”子文件夹中。

Drupal 6

Drupal 6技术来自模块层的主题Drupal 6

首先,在模块目录中创建一个模板文件。

然后告诉主题系统去那里。

// Create a variable to store the path to this module
define('MY_MODULE_PATH', drupal_get_path('module', 'my_module'));

function special_page_theme_registry_alter(&$theme_registry) {
  // A list of templates the module will provide templates for
  $hooks = array('page');

  foreach ($hooks as $h) {
    // Add the module path on top in the array of paths
    array_unshift($theme_registry[$h]['theme paths'], MY_MODULE_PATH);
  }
}

Drupal 5

对于Drupal 5,我将描述一种受打印模块启发的解决方法。

解决方案概述

1.设置将在页面模板中使用的变量。
2.在模块内部创建一个新的页面模板mypage.tpl.php。
3.包括页面模板。

解决方案的详细信息

1.由于这是一种解决方法,而不是主题系统的一部分,因此您的模板将无法访问常规主题变量。您必须设置希望新模板有权访问的变量:

function _mymodule_print_vars_setup() {
    $print['language'] = $GLOBALS['locale'];
    $print['title'] = 'Page XYZ';
    $print['head'] = drupal_get_html_head();
    $print['favicon'] = theme_get_setting("toggle_favicon") ? "\n" : "";

    $css_files = array(drupal_get_path('module','mymodule').'/stylesheet1.css', drupal_get_path('module','mymodule').'/stylesheet2.css');
    foreach ($css_files as $css_file)
        $print["css"] .= "@import \"". base_path() . $css_file['file'] ."\";\n";

    $print["content"] = _mymodule_page_content();

    return $print;
}

}

2.创建一个模板文件,该模板文件将与主题的page.tpl.php非常相似,不同之处在于您将使用的变量来自步骤1中创建的$ print数组。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php print $print['language'] ?>" lang="<?php print $print['language'] ?>" >
  <head>
    <title><?php print $print['title'] ?></title>
    <?php print $print['head'] ?>
    <?php print $print["favicon"] ?>
    <?php print $print['css'] ?>
  </head>
  <body>
    <div id="wrapper">
        <div id="main-content">
	    <?php print $print['content']; ?>
        </div>
        <div id="right-bar">
          <?php print theme('blocks', 'right_bar'); ?>
        </div>
      </div>
    </div>
  </body>
</html>

您可以使用theme_blocks访问主主题的区域,如右栏所示。

3.然后创建一个函数,该函数将作为模块hook_menu实现中项目之一的回调:

function mymodule_special_page() {
    $print = _mymodule_print_vars_setup();
    include_once(drupal_get_path('module','mymodule').'/mypage.tpl.php');
}

有关此方法的完整示例,请研究“打印”模块。

来自  https://www.drupal.org/docs/7/creating-custom-modules/howtos/using-template-tplphp-files-in-your-own...

普通分类: