我已经创建了一个自定义模块并分配了一个树枝模板文件,但它没有显示。以下是文件和文件夹结构
1.workbuster.module文件的代码如下
<?php
/**
* Implements hook_theme().
*/
function workbuster_theme()
{
return array(
'block_workbuster' => array(
'variables' => array('title' => NULL, 'description' => NULL),
'template' => 'block--workbuster-custom',
),
);
}
Run Code Online (Sandbox Code Playgroud)
2. WorkbusterBlock文件的代码如下
<?php
/**
* @file
*/
namespace Drupal\workbuster\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Workbuster' Block
* @Block(
* id = "block_workbuster",
* admin_label = @Translation("Workbuster block"),
* )
*/
class WorkbusterBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#title' => 'Workbuster',
'#description' => 'Workbuster'
);
}
}
Run Code Online (Sandbox Code Playgroud)
3. block--workbuster-custom.html.twig 文件的代码如下
{#
/**
* @file
* Profile Workbuster block.
*/
#}
<div class="col-sm-3 ws-custom--block">
<h1>{{ title }}</h1>
<p>{{ description }}</p>
</div>[![directory structure][1]][1]
Run Code Online (Sandbox Code Playgroud)
小智 5
尝试:
在 WorkbusterBlock.php 中,您必须按如下方式设置模板:
public function build() {
return array(
'#theme' => 'block__workbuster_custom',
'#title' => 'Workbuster',
'#description' => 'Workbuster'
);
}
Run Code Online (Sandbox Code Playgroud)
在您的 .module 中,使用您的模板作为键:
function workbuster_theme()
{
return array(
'block__workbuster_custom' => array(
'variables' => array('title' => NULL, 'description' => NULL),
),
);
}
Run Code Online (Sandbox Code Playgroud)
注意:我将模板名称中的连字符(-)替换为下划线(_)