Drupal 8 - Twig 模板不会获取变量值
Drupal 8 - Twig template won't get variables values
我正在尝试制作一个树枝模板以从自定义块中获取变量,当我执行 {{ dumb() }}
它会向我显示变量及其值,但是当我调用变量时它不会'不要显示它,即使我用 dumb {{ dumb(title) }}
调用变量,它也告诉我它是 NULL。谁能帮我理解错误是什么?
阻止:onyx_experiencia.php
/**
* Provides a 'Test' Block.
*
* @Block(
* id = "onyx_experiencia",
* admin_label = @Translation("Servicios OnyxGroup"),
* category = @Translation("Servicios OnyxGroup"),
* )
*/
class onyx_experiencia extends BlockBase implements BlockPluginInterface {
/**
* {@inheritdoc}
*/
public function build() {
$title = 'TestTitle34';
$desc = 'Test text 24';
$test_array = array(
'#title' => $title,
'#description' => $desc
);
return $test_array;
}
block.module: onyx_experiencia.module
<?php
/**
* Implements hook_theme().
*/
function onyx_experiencia_theme($existing, $type, $theme, $path) {
return array(
'block__serviciosonyxgroup' => array(
'template' => 'block--serviciosonyxgroup',
'render element' => 'elements',
'variables' => array(
'title' => 'TitleTest',
'description' => 'DescriptionTest'
),
),
);
}
Twig 文件:块--serviciosonyxgroup.html.twig
{#
/**
* @file
* Profile for onyx_experiencia block.
*/
#}
<h3>Featured Events</h3>
<p>Test: {{ title }} </p>
<p>Test: {{ description }} </p>
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
{{ dump(content) }}
结果:这是我得到的结果
UPDATE Different way still not working
如您的屏幕截图所示:
变量存在于变量 content
中,而不是 _context
您当前的模板代码假定它们是实时的 _context
,因为它们没有任何前缀。
{{ title }}
等于 <?= isset($_context['title']) ? $_context['title'] : null; ?>
所以你需要将模板更改为
<h3>Featured Events</h3>
<p>Test: {{ content['#title'] }} </p>
<p>Test: {{ content['#description'] }} </p>
我正在尝试制作一个树枝模板以从自定义块中获取变量,当我执行 {{ dumb() }}
它会向我显示变量及其值,但是当我调用变量时它不会'不要显示它,即使我用 dumb {{ dumb(title) }}
调用变量,它也告诉我它是 NULL。谁能帮我理解错误是什么?
阻止:onyx_experiencia.php
/**
* Provides a 'Test' Block.
*
* @Block(
* id = "onyx_experiencia",
* admin_label = @Translation("Servicios OnyxGroup"),
* category = @Translation("Servicios OnyxGroup"),
* )
*/
class onyx_experiencia extends BlockBase implements BlockPluginInterface {
/**
* {@inheritdoc}
*/
public function build() {
$title = 'TestTitle34';
$desc = 'Test text 24';
$test_array = array(
'#title' => $title,
'#description' => $desc
);
return $test_array;
}
block.module: onyx_experiencia.module
<?php
/**
* Implements hook_theme().
*/
function onyx_experiencia_theme($existing, $type, $theme, $path) {
return array(
'block__serviciosonyxgroup' => array(
'template' => 'block--serviciosonyxgroup',
'render element' => 'elements',
'variables' => array(
'title' => 'TitleTest',
'description' => 'DescriptionTest'
),
),
);
}
Twig 文件:块--serviciosonyxgroup.html.twig
{#
/**
* @file
* Profile for onyx_experiencia block.
*/
#}
<h3>Featured Events</h3>
<p>Test: {{ title }} </p>
<p>Test: {{ description }} </p>
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
{{ dump(content) }}
结果:这是我得到的结果
UPDATE Different way still not working
如您的屏幕截图所示:
变量存在于变量 content
中,而不是 _context
您当前的模板代码假定它们是实时的 _context
,因为它们没有任何前缀。
{{ title }}
等于 <?= isset($_context['title']) ? $_context['title'] : null; ?>
所以你需要将模板更改为
<h3>Featured Events</h3>
<p>Test: {{ content['#title'] }} </p>
<p>Test: {{ content['#description'] }} </p>