【问题标题】:Drupal 8 assign module template to a pageDrupal 8将模块模板分配给页面
【发布时间】:2019-07-28 02:53:42
【问题描述】:

我是 drupal 的新手,我正在创建一个模块以允许客户使用 Fabric js 自定义卡片。我创建了一个模块,并在其中创建了一个模板文件 my_module > templates > page--my_module.html.twig。问题是我无法在页面上查看模板的内容。

这是我的 my_module.module 文件的代码

    <?php

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function my_module_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.my_module':
      return t('
        <h2>Lorem ipsum generator for Drupal.</h2>
        <h3>Instructions</h3>
        <p>Lorem ipsum dolor sit amet... <strong>Just kidding!</strong></p>
        <p>Unpack in the <em>modules</em> folder (currently in the root of your Drupal 8 installation) and enable in <strong>/admin/modules</strong>.</p>
        <p>Then, visit <strong>/admin/config/development/loremipsum</strong> and enter your own set of phrases to build random-generated text (or go with the default Lorem ipsum).</p>
        <p>Last, visit <strong>www.example.com/loremipsum/generate/P/S</strong> where:</p>
        <ul>
          <li><em>P</em> is the number of <em>paragraphs</em></li>
          <li><em>S</em> is the maximum number of <em>sentences</em></li>
        </ul>
        <p>There is also a generator block in which you can choose how many paragraphs and
phrases and it\'ll do the rest.</p>
        <p>If you need, there\'s also a specific <em>generate lorem ipsum</em> permission.</p>
        <h3>Attention</h3>
        <p>Most bugs have been ironed out, holes covered, features added. But this module is a work in progress. Please report bugs and suggestions, ok?</p>
      ');
  }
}

/**
* Implements hook_theme() to add the template definition.
**/
function my_module_theme($existing, $type, $theme, $path) {
  return array(
    'my_module_template' => array(
        'template' => 'page--my_module',
      'variables' => array('test_var' => NULL),
    ),
  );
}

这里是控制器MyModuleController.php的代码

    <?php
/**
 * @file
 * Contains \Drupal\card_designer\Controller\FirstController.
 */

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;

class MyModuleController extends ControllerBase {
  public function content() {
    return array(
      '#theme' => 'card_designer_template',
      '#test_var' => $this->t('Test Value'),
    );
  }
}

这里是my_module.routing.yml的代码

  my_module.content:
  path: '/card-design'
  defaults:
    _controller: 'Drupal\my_module\Controller\MyModuleController::content'
    _title: 'Hello world'
  requirements:
    _permission: 'access content'**strong text**

这里是页面--my_module.html.twig 文件

<p> This is the lotus template with a value of {{ test_var }} </p>

现在不知道如何将我的自定义模板分配给页面,以便我可以在网站上检查模板的输出。

谁能告诉我我错在哪里以及如何查看页面上的输出。

提前致谢。

【问题讨论】:

    标签: templates module drupal-8


    【解决方案1】:
    1. 在“my_module.routing.yml”中将_permission: 'access content'**strong text** 修复为_permission: 'access content'。我不确定你从哪里得到**strong text**,但它不应该在这里。

    2. 将“page--my_module.html.twig”重命名为“page--my-module.html.twig”。 Twig 模板名称不应包含下划线。

    3. 在“MyModuleController.php”中将'#theme' =&gt; 'card_designer_template', 更改为'#theme' =&gt; 'page__my_module',,因为模板的机器名称必须匹配所有其他主题命名用法。请注意,page__my_module 在 PHP 中使用时使用下划线,但是当用作 twig 模板文件的名称时,下划线会转换为连字符 => page--my-module.html.twig

    4. 在“my_module.module”中更改

    'my_module_template' => array(
            'template' => 'page--my_module',
          'variables' => array('test_var' => NULL),
        ),
    

    'page__my_module' => array(
      'variables' => array('test_var' => NULL),
    ),
    
    

    因为模板机器名必须是父键,变量应该是子键。也可以删除“模板”键,因为修复后它在父键中定义,如前所述。

    1. 我不确定您是否有“my_module.info.yml”,但应该是这样的:
    name: my_module
    type: module
    description: 'My custom module'
    package: 'Custom'
    core: 8.x
    
    1. 我不确定您的结构,但应该是这样的:

    我希望我没有遗漏任何东西 :) 我修复并测试了您的代码,所以如果它不起作用,请告诉我,我会修复我的答案。

    【讨论】:

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