使用 for 循环将多个 html 元标记附加到头部
我目前正在开发一个商店插件,我在其中添加一些元数据到 html 标签head。我使用 twig 来扩展模板并向其添加一些代码。 我触发一个事件,将元实体数组 mykn_metaFields 传递到我的树枝模板。在我的 twig-template 中,我循环遍历 mykn_metaFields 并将这些实体的值应用于元标记属性。
Twig 代码:
{% sw_extends '@Storefront/storefront/layout/meta.html.twig' %}
{% block layout_head_meta_tags %}
{{parent()}}
{{dump()}}
{% if page.mykn_metaFields.total > 0 %}
{% for metaField in page.mykn_metaFields.entities.elements %}
{% if metaField.active == true %}
<meta name="{{metaField.name}}" content="{{metaField.value}}">
{{dump(metaField)}}
{% endif %}
{% endfor %}
{% endif %}
{% endblock %}
预期 html 输出:
<html>
<head>
...
<meta name="foo" content="foo">
<meta name="bar" content="bar">
</head>
<body>
...
</body>
</html>
返回 html 输出:
<html>
<head>
...
</head>
<body>
<meta name="foo" content="foo">
<meta name="bar" content="bar">
...
</body>
</html>
问题:为什么它不将元标记附加到头部?
这就是我传递变量 mykn_metaFields 的方式:
<?php declare(strict_types=1);
namespace MyPluginRoot\Subscriber\Storefront\Page;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class PageLoadedSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $metaFieldRepository;
public function __construct(EntityRepositoryInterface $metaFieldRepository)
{
$this->metaFieldRepository = $metaFieldRepository;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'addMetaToHead'
];
}
public function addMetaToHead(GenericPageLoadedEvent $event): void
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('active', true));
$metaFields = $this->metaFieldRepository->search($criteria, $event->getContext());
$array = ['mykn_metaFields' => $metaFields];
$event->getPage()->assign($array);
}
}```
编辑 我通过删除 twig-template 中的 dump()
解决了这个问题。我猜商店软件将其呈现为类似 pre
标签的东西,将其放入正文中。
i'm currently working on a shopware plugin, where I add some meta data to the html tag head. I use twig to extend a template and add some some code to it.
I trigger an event which passes the array of meta entities mykn_metaFields to my twig-template. In my twig-template I loop over mykn_metaFields and apply the values of these entities to meta-tag attributes.
Twig code:
{% sw_extends '@Storefront/storefront/layout/meta.html.twig' %}
{% block layout_head_meta_tags %}
{{parent()}}
{{dump()}}
{% if page.mykn_metaFields.total > 0 %}
{% for metaField in page.mykn_metaFields.entities.elements %}
{% if metaField.active == true %}
<meta name="{{metaField.name}}" content="{{metaField.value}}">
{{dump(metaField)}}
{% endif %}
{% endfor %}
{% endif %}
{% endblock %}
expected html Output:
<html>
<head>
...
<meta name="foo" content="foo">
<meta name="bar" content="bar">
</head>
<body>
...
</body>
</html>
returned html Output:
<html>
<head>
...
</head>
<body>
<meta name="foo" content="foo">
<meta name="bar" content="bar">
...
</body>
</html>
Question: Why doesn't it append the meta tags to the head?
This is how i pass the variable mykn_metaFields:
<?php declare(strict_types=1);
namespace MyPluginRoot\Subscriber\Storefront\Page;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class PageLoadedSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $metaFieldRepository;
public function __construct(EntityRepositoryInterface $metaFieldRepository)
{
$this->metaFieldRepository = $metaFieldRepository;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'addMetaToHead'
];
}
public function addMetaToHead(GenericPageLoadedEvent $event): void
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('active', true));
$metaFields = $this->metaFieldRepository->search($criteria, $event->getContext());
$array = ['mykn_metaFields' => $metaFields];
$event->getPage()->assign($array);
}
}```
EDIT
I solved this problem by removing dump()
in my twig-template. I guess shopware renders it as something like a pre
-tag which puts it in the body.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设
page.mykn_metaFields
是EntitySearchResult
在这种情况下,您应该能够直接循环该SearchResult
,无需访问内部elements
属性,也无需事先检查总数。这已经简化了您的树枝模板:
此外,我会过滤后端已有的非活动元字段,您可以在其中加载自己的实体并将其添加到页面对象中。
这样你就可以摆脱模板中的
active
检查,并且模板大大简化:I assume that
page.mykn_metaFields
is aEntitySearchResult
in that case you should be able to loop directly over thatSearchResult
, no need to access the internalelements
property and also no need to check the total beforehand.this would already simplify your twig template:
Additionally i would filter the inactive metaFields already on the backend side, where you load your own entity and add it to the page object.
That way you can get rid of the
active
check in the template, and the template is greatly simplified: