【发布时间】: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>
现在不知道如何将我的自定义模板分配给页面,以便我可以在网站上检查模板的输出。
谁能告诉我我错在哪里以及如何查看页面上的输出。
提前致谢。
【问题讨论】: