twig 相关问题

Twig是一款适用于PHP的现代,快速,灵活且安全的模板引擎。为Symfony创建并由Drupal 8采用。

尝试使用twig功能时Twig环境未注入shopware 6.5.4.0如何解决

我正在开发一个自定义插件,它接受来自 api 的数据。 我正在尝试使用 twig 函数来获取数据,如下所示 树枝功能类: 我正在开发一个自定义插件,它接受来自 api 的数据。 我正在尝试使用 twig 函数来获取如下数据 twig功能类: <?php declare(strict_types=1); namespace Nst\Twig; use Nst\Service\StoryblokService; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; class StoryblokTwigExtension extends AbstractExtension { public function __construct(private StoryblokService $storyblokService) { } public function getFunctions(): array { return [ new TwigFunction('get_story', [$this, 'getStory']), ]; } public function getStory(string $slug): array { return $this->storyblokService->getStory($slug); } } 也在我的服务中: <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services"> <services> <service id="Nst\Service\StoryblokService" public="true"> <argument type="service" id="Storyblok\Client"/> </service> <service id="Storyblok\Client" class="Storyblok\Client"> <argument>%env(dumpcode)%</argument> </service> <service id="Nst\Storefront\Controller\CodesController" public="true"> <argument type="service" id="twig"/> <call method="setContainer"> <argument type="service" id="service_container"/> </call> </service> </services> </container> 然后在我渲染页面的控制器中,我尝试注入到我的构造中以供使用。 <?php declare(strict_types=1); namespace Nst\Storefront\Controller; use Shopware\Storefront\Controller\StorefrontController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Twig\Environment; /** * @Route(defaults={"_routeScope"={"storefront"}}) */ class CodesController extends StorefrontController { public function __construct(Environment $twig) { $this->twig = $twig; } public function setTwig(Environment $twig): void { $this->twig = $twig; error_log('Twig has been set.'); // or use your preferred logging method } /** * @Route("/mystore", name="frontend.example.example", methods={"GET"}, defaults={"_routeScope"={"storefront"}}) */ public function showPage(): Response { if (!$this->twig) { error_log('Twig is not set.'); } return $this->renderStorefront('@Nst/storefront/page/example.html.twig', [ 'example' => 'Hello world' ]); } } 我的 twig 页面的副本,我需要这个 twig 函数来显示从 api 消耗的数据。 {% sw_extends '@Storefront/storefront/base.html.twig' %} {% block base_content %} <h1>Our example controller!</h1> {% endblock %} {% set story = get_story('ola-landing-page') %} {% if story %} {# Display your story content here #} {{ story.content.title }} {# Add more fields as required from the story #} {% endif %} 但我不断 hp.CRITICAL: Uncaught Error: Too few arguments to function Nst\Storefront\Controller\CodesController::__construct(), 0 passed in Uncaught PHP Exception Shopware\Storefront\Controller\Exception\StorefrontException: "Class Nst\Storefront\Controller\CodesController does not have twig injected. Add to your service definition a method call to setTwig with the twig instance" at 我不知道该怎么办,已经坚持了很长时间,请帮忙 您可以通过添加needs_environment选项并将其设置为true来自动注入twig环境,然后正确的实例将自动传递给您的TwigFunction <?php declare(strict_types=1); namespace Nst\Twig; use Nst\Service\StoryblokService; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; class StoryblokTwigExtension extends AbstractExtension { public function __construct(private StoryblokService $storyblokService) { } public function getFunctions(): array { return [ new TwigFunction('get_story', [$this, 'getStory'], [ 'needs_environment' => true), ]; } public function getStory(\Twig\Environment $twig, string $slug): array { /* Now you can use $twig to render something or you could even pass it to the service if needed/wanted .... return $twig->render(....); $this->storyblockService->setTwig($twig); ... */ return $this->storyblokService->getStory($slug); } }

回答 1 投票 0

如何在twig模板中显示图像并充当Drupal的链接?

我的网站上的 .twig 文件中有以下代码 我的网站上的 .twig 文件中有以下代码 <a href="https://appointments.detailandsupply.com/easyappointments/index.php?service=14" style="color: lightgreen; font-weight: 600; font-size: 18px; text-align: center; text-decoration: underline;">Click here to schedule an appointment</a> 我有一个名为schedule.png的图像位于/files/中,并且希望将此显示为链接并删除“单击此处安排约会”文本。我该如何去做呢? 我尝试在“单击此处安排预约”部分添加<img src="{{ asset('files/schedule.png') }}"> 我添加了 alt 标签和 style="border: none;" <a href="https://appointments.detailandsupply.com/easyappointments/index.php?service=14" style="text-align: center;"> <img src="{{ asset('files/schedule.png') }}" alt="Schedule an appointment" style="border: none;"> </a>

回答 1 投票 0

Drupal主题安装错误及迁移问题

我的名字是 Shahzad,我是 Drupal 和 PHP 的新手。我需要对 Drupal 项目进行一些更改。我只获得了存储库。我需要社区的指导和解决方案。 谢谢

回答 1 投票 0

为什么 twig 模板在“dev”环境中使用 if 语句时会抛出未知的“dump”函数?

我有一个使用 Symfony3 的树枝模板,如下所示: {% if app.environment == 'dev' %} {{转储(学生)}} {% 万一 %} 但在“prod”环境中它会抛出此错误,如 va...

回答 4 投票 0

Shopware 6:Vue.js 中 sw-snippet-field 和 sw-media-field 组件的 sw-inherit-wrapper

我正在使用 Vue.js 在 Shopware 6 中编写自定义管理模块(插件配置)。 我遇到的问题是缺少显示 w...

回答 1 投票 0

如何删除 Twig 上数组中的重复项

如何在 Twig 上删除数组中的重复项? 我在树枝中有数组值,例如。 {{ 设置数组 = ["testA","testB","testA","testC","testB"] }} 我想删除重复的项目并仅使用...

回答 7 投票 0

如何从 symfony 中删除 twig?

我的项目不需要任何树枝功能或模板。 我把树枝从 作曲家.json 捆绑包.php 我删除了所有配置树枝文件(yml 文件) 我删除了 html temp...

回答 1 投票 0

如何在 Symfony2/Twig 模板中包含原始 HTML 文件?

我正在 Symfony2 中开发一个项目,我有几小段 html 需要包含在我的主要视图之一中。根据 Twig 官方文档,我应该能够简化...

回答 4 投票 0

Shopware 6:如何访问管理模块的 Twig 模板中的配置参数

我正在寻找一种访问管理模块的树枝模板中的配置参数的方法。我尝试通过以下方式访问它: {{ shopware.config.pluginName.config.fieldName }} 问题是在...

回答 2 投票 0

如何使用 Twig 检查 Shopware 6 中当前活动的类别?

我正在使用 Shopware 6 并且需要检查 Twig 条件中当前活动的类别名称? 像这样的东西: {% if page.header.navigation.active.category == '图书' %} 是的&... 我正在使用 Shopware 6,需要检查 Twig 条件下的当前活动类别名称? 类似这样的: {% if page.header.navigation.active.category == 'Books' %} <h1>yes</h1> {% endif %} 在产品详细信息页面上,可以在page.product.seoCategory中找到该类别。按名称检查类别: {% if page.product.seoCategory.translated.name == 'Books' %} <h1>yes</h1> {% endif %} 在所有其他页面上,您可以使用变量page.header.navigation.active: {% if page.header.navigation.active.translated.name == 'Books' %} <h1>yes</h1> {% endif %} 如果有人仍然活跃,我也有类似的情况,但我需要产品详细信息页面中的主要类别名称,例如我有一顶分配给“女性”类别的帽子,女性类别是衣服的子类别, 我试过这个: page.product.seoCategory.translated.name 但它返回子类别 有人可以帮助我吗?谢谢

回答 2 投票 0

检查(在 twig 模板中)Shopware 6 客户是否属于特定客户组

我知道这个Shopware 6 twig的状况: {% 如果 context.customer 已定义 %} 做表演 {% 万一 %} 但如何检查客户是否属于特定客户组呢?

回答 1 投票 0

尝试格式化 {$

尝试将该代码格式化为twig格式: <p>尝试将代码格式化为twig格式:</p> <pre><code><textarea class="form-control" name="email_review_subject_<?php echo $language['language_id']; ?>" ><?php echo isset(${'email_review_subject_' . $language['language_id']}) ? ${'email_review_subject_' . $language['language_id']} : ''; ?></textarea> </code></pre> <p>所以我尝试使用这种格式:</p> <pre><code><textarea class="form-control" name="email_review_subject_{{ language['language_id'] }}" >{{ isset({'email_review_subject_' . language.language_id}) ? {'email_review_subject_' . language.language_id} : '' }}</textarea> </code></pre> <p><strong>错误:</strong></p> <blockquote> <p>未捕获的异常“Twig_Error_Syntax”,消息为“哈希键必须 后跟冒号 (:)。意外的代币价值“标点符号” “。” (“标点符号”预期值为“:”)</p> </blockquote> </question> <answer tick="true" vote="3"> <p><pre><code>isset</code></pre>中不存在<pre><code>twig</code></pre>这样的东西。如果你想在 twig 中使用动态变量,你还需要访问特殊变量 <pre><code>_context</code></pre> </p>一些可能的解决方案,<p> </p><code><textarea class="form-control" name="email_review_subject_{{ language['language_id'] }}" >{{ attribute(_context, 'email_review_subject_'~language.language_id)|default('') }}</textarea> <textarea class="form-control" name="email_review_subject_{{ language['language_id'] }}" >{{ attribute(_context, 'email_review_subject_'~language.language_id) is defined ? attribute(_context, 'email_review_subject_'~language.language_id) : '' }}</textarea> </code><pre> </pre><p>小提琴<a href="https://twigfiddle.com/p8h4wn" rel="nofollow noreferrer"></a> </p> </answer> <answer tick="false" vote="3">我很确定你不能使用 <p><code>.</code><pre> 在 Twig 中连接字符串。尝试</pre><code>~</code><pre>,然后看这里:</pre>如何在树枝中连接字符串<a href="https://stackoverflow.com/questions/7704253/how-to-concatenate-strings-in-twig"></a> </p> </answer></body>

回答 0 投票 0

opencart |打印 twig 文件中的对象数组

我正在使用opencart。在那里,我将对象数组从控制器发送到树枝文件。 控制器 $data['饼干'] = []; $results = $this->model_quickorder_order->getCrackers(); $美食...

回答 1 投票 0

Drupal 9 - 将视图“内容:链接到内容”url 输出到未格式化的视图视图....html.twig 模板

我正在尝试以views-view-unformatted....html.twig 格式的Twig 模板中的视图“内容:链接到内容”输出url 别名。 url别名在V中成功输出...

回答 1 投票 0

如何将全局变量注入到所有模板中?

在我的 twig 模板中,每个页面都需要一些变量,例如 userName、userId 或 userImage。 将它们注入模板的最佳方法是什么? 我已经读过文章 H...

回答 2 投票 0

Shopware 6 插件:在搜索结果分页中生成 SEO 友好的 URL 时出现问题

在 Shopware 6 中实现搜索结果分页时,我遇到了生成 SEO 友好 URL 的问题。我有一个自定义 Twig 模板,它扩展了 pagination.html.twig 模板......

回答 1 投票 0

如何在 VS Code 中的字符串内获得括号对着色?

在 VS Code 中,我使用 Twig Language 2 扩展来处理 HTML 文件内类似 Twig 的语法(实际上我使用 Pebble 模板引擎)。我配置了 VS Code,因此括号是彩色的: &

回答 2 投票 0

Craft CMS 4 & Enupal Stripe 支付插件 - 生产环境问题

我已在我的 Craft CMS 4 网站上安装了 Enupal Stripe 付款插件。它在本地运行良好,但当我尝试在生产环境上创建付款表单时,出现此错误: 数据库

回答 0 投票 0

使用 twig 和 craft CMS 从文本字段获取字符串

我正在使用 twig 和 craft CMS。 如何从文本字段中仅访问一个单词并为其添加下划线? 作为我想要的结果的示例,我提供了 HTML 和 CSS 代码。 L... 我正在使用 twig 和 craft CMS。 如何从文本字段中仅访问一个单词并为其添加下划线? 作为我想要的结果的示例,我提供了 HTML 和 CSS 代码。 <p> Lorem Ipsum is simply dummy text of the <span>printing</span> and <span>typesetting</span> industry. </p> span { text-decoration: underline; } 这是我在 twig 中使用的代码 {% for text in texts %} <div class="col-12 col-md-8"> <h1>{{ text.title }}</h1> </div> {% endfor %} 可以使用 Twig 的 split 过滤器将文本字段分为单词数组,然后您可以使用数组的索引来获取所需的单词。

回答 1 投票 0

如何在 Twig 中按字母顺序对带有逗号的名称进行排序?例如:John Doe,CFP

我正在尝试使用 Twig Sort 按名字或姓氏的字母顺序对名称中包含逗号的名称进行排序。 例子 约翰·多伊,CFP 简·多伊,工商管理硕士 我想按字母顺序排序......

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.