【问题标题】:Drupal 8 custom block (module) twig template file content not displayingDrupal 8自定义块(模块)树枝模板文件内容不显示
【发布时间】:2019-10-11 11:53:35
【问题描述】:

我创建了一个自定义模块并分配了一个树枝模板文件,但它没有显示。 以下是文件和文件夹结构

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',
        ),
    );
}

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'
        );
    }

}

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]

【问题讨论】:

    标签: twig drupal-8


    【解决方案1】:

    试试:

    在您的 WorkbusterBlock.php 中,您必须按如下方式设置模板:

     public function build() {
            return array(
              '#theme' => 'block__workbuster_custom',
              '#title' => 'Workbuster',
              '#description' => 'Workbuster'
            );
        }
    

    在您的 .module 中,使用您的模板作为键:

    function workbuster_theme()
    {
        return array(
          'block__workbuster_custom' => array(
              'variables' => array('title' => NULL, 'description' => NULL),
            ),
        );
    }
    

    注意:我将模板名称中的连字符 (-) 替换为下划线 (_)

    【讨论】:

      猜你喜欢
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多