根据 drupal 8 中的语言,是否可以有多个缓存条目?
Is it possible to have multiple cache entries depending on the language in drupal 8?
我的自定义块模板中有一个复杂变量 {{course_type.title[language] | nl2br}}
。 language
是当前网站的语言,但内容仅以构建缓存时的语言提供。
我的渲染数组中确实有语言,它适用于树枝模板中的 {% trans %}
命令:
return array(
'#theme' => 'block__vt_course_offer',
'#data' => $courseData,
'#cache' => [
'contexts' => ['languages'],
'tags' => $cacheTags,
]
);
有没有办法让 Drupal 根据页面的当前语言处理多个缓存条目?
非常感谢!
安德烈亚斯
所以我们找到了一个解决方案——我们将不同的部分提取到子渲染数组中,并仅为它们禁用缓存。 重要 是,对数据[$language] 的访问发生在主模块中,不在模板中。
foreach ($courses as &$courseType) {
$courseType['d_url'] = $dcm->getCourseUrl($courseType['id']);
$courseType['output'] = array(
'#theme' => 'block__vt_course_offer_item',
'#data' => [
'id' => $courseType['id'],
'image' => $courseType['image'],
'duration' => $courseType['duration'],
'price' => $courseType['price'],
'd_url' => $courseType['d_url'],
'title' => $courseType['title'][$language],
'short_description' => $courseType['short_description'][$language],
],
'#cache' => [
'disabled' => TRUE,
'contexts' => ['languages'],
'tags' => ['courseType:' . $courseData['data']['course'][0]['id']],
]
);
然后我们可以在树枝中使用它:
{% for course_type in courses|slice(i * 4, (i+1) * 4) %}
{{ course_type.output }}
{% endfor %}
简而言之,它归结为 {{variable.preset_language_value}}
而不是 {{variable.value[language]}}
的模板用法
我的自定义块模板中有一个复杂变量 {{course_type.title[language] | nl2br}}
。 language
是当前网站的语言,但内容仅以构建缓存时的语言提供。
我的渲染数组中确实有语言,它适用于树枝模板中的 {% trans %}
命令:
return array(
'#theme' => 'block__vt_course_offer',
'#data' => $courseData,
'#cache' => [
'contexts' => ['languages'],
'tags' => $cacheTags,
]
);
有没有办法让 Drupal 根据页面的当前语言处理多个缓存条目?
非常感谢!
安德烈亚斯
所以我们找到了一个解决方案——我们将不同的部分提取到子渲染数组中,并仅为它们禁用缓存。 重要 是,对数据[$language] 的访问发生在主模块中,不在模板中。
foreach ($courses as &$courseType) {
$courseType['d_url'] = $dcm->getCourseUrl($courseType['id']);
$courseType['output'] = array(
'#theme' => 'block__vt_course_offer_item',
'#data' => [
'id' => $courseType['id'],
'image' => $courseType['image'],
'duration' => $courseType['duration'],
'price' => $courseType['price'],
'd_url' => $courseType['d_url'],
'title' => $courseType['title'][$language],
'short_description' => $courseType['short_description'][$language],
],
'#cache' => [
'disabled' => TRUE,
'contexts' => ['languages'],
'tags' => ['courseType:' . $courseData['data']['course'][0]['id']],
]
);
然后我们可以在树枝中使用它:
{% for course_type in courses|slice(i * 4, (i+1) * 4) %}
{{ course_type.output }}
{% endfor %}
简而言之,它归结为 {{variable.preset_language_value}}
而不是 {{variable.value[language]}}