markdown 如何在Drupal模板中呈现现有块

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 如何在Drupal模板中呈现现有块相关的知识,希望对你有一定的参考价值。

## Drupal 7

If you want to embed the contents of a block into a node, a custom block, custom module, or a page template, you can use the following snippets in the template file in which you wish your block to appear:
```
$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);
```

To specify which block from which module, you simply edit the two following variables in the first line:

'module_name' = The machine name of the module (i.e. the module's folder name). This is true for core modules too, so for instance 'search', 'user' and 'comment' would all work here.

'block_delta' = The machine name of the block. You can determine what this is by visiting the block administration page and editing the block. The URL for editing a webform block, for instance, would be something like:

Drupal 7: admin/structure/block/manage/webform/client-block-11/configure

In this example, 'client-block-11' contains the block's delta, '11'.

Custom blocks will have module name of 'block' and a number for a delta, which you can also find by editing the block.

#### Example:
```
<?php $corpwatch_searchblock = module_invoke('searchblock', 'block_view', '0'); ?>
<?php print render($corpwatch_searchblock['content']); ?>
```

[Nice writeup](https://www.drupal.org/node/26502)

## Drupal 8

In themename.theme:   
```
function hook_preprocess_thingie {
  $block = \Drupal\block\Entity\Block::load('your_block_id');
  $variables['block_output'] = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);
}
```
Where "thingie" is node or page or wherever you want to use your block

Then in your twig file, just call {{ block_output }}

#### Example:
```
function essentialaccess_preprocess_node(&$variables) {
  $how_to_apply_block = \Drupal\block\Entity\Block::load('howtoapply');
  $variables['how_to_apply_block'] = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($how_to_apply_block);
}
```

Then in my twig file, I called `{{ how_to_apply_block }}`

[original](https://drupal.stackexchange.com/questions/153184/programatically-render-a-block-in-a-twig-template)

以上是关于markdown 如何在Drupal模板中呈现现有块的主要内容,如果未能解决你的问题,请参考以下文章

markdown 如何在Drupal 8中启用模板调试(带有模板建议的html注释)

如何在 express.js 玉模板中呈现 markdown?

VueJS 组件不会在前端呈现(可能是 Drupal 问题)

markdown 将类添加到Drupal 8页面的body标记而不更改模板

markdown 以编程方式为Drupal 8中的错误页面(404等)设置自定义模板

markdown 将类添加到Drupal 8页面的body标记而不更改模板