简体   繁体   中英

Drupal 8 custom block (module) twig template file content not displaying

I have created a custom module and assigned a twig template file but it is not displaying. Below are files and folder structure

文件夹结构

1.Code for workbuster.module file is as below

<?php

/**
 * Implements hook_theme().
 */
function workbuster_theme()
{
    return array(
    'block_workbuster' => array(
            'variables' => array('title' => NULL, 'description' => NULL),
            'template' => 'block--workbuster-custom',
        ),
    );
}

2. Code for WorkbusterBlock file is as below

<?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. Code for block--workbuster-custom.html.twig file is as below

{#
/**
 * @file
 * Profile Workbuster block.
 */
#}
 <div class="col-sm-3 ws-custom--block">
    <h1>{{ title }}</h1>
    <p>{{ description }}</p>
 </div>[![directory structure][1]][1]

Try:

Inside your WorkbusterBlock.php you must set the template as follows:

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

In your.module, use your template as key:

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

Note: I replaced hyphens (-) with underscores (_) in the template name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM