标签: twig-extension

Twig和Symfony2中的自定义反滤镜

trans出于特殊目的需要覆盖标准的Twig 过滤器,例如,我想从自定义存储中获取翻译。所以我不知道我需要做什么。我尝试找出docs。但是没有关于翻译的描述。我找到了Symfony\Bridge\Twig\Extension\TranslationExtension课程,我想也许我只需要改写这个课程?谢谢!

php translation symfony twig twig-extension

5
推荐指数
1
解决办法
2433
查看次数

树枝:url_decode

我用树枝过滤器url_encode编码了url参数。

// app.request.query.get("date") output 01/04/2016

href="{{ path('page', {date: app.request.query.get("date")|url_encode}) }}">
Run Code Online (Sandbox Code Playgroud)

网址中的哪个输出

date=01%252F04%252F2016
Run Code Online (Sandbox Code Playgroud)

因此,在具有url参数的请求页面中

 {{ app.request.query.get("date") }}
Run Code Online (Sandbox Code Playgroud)

显示01%2F04%2F2016但我想拥有01/04/2016

我尝试使用原始过滤器,还做了一个树枝扩展:

<?php
namespace SE\AppBundle\Twig;

class htmlEntityDecodeExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('html_entity_decode', array($this, 'htmlEntityDecode'))
        );
    }

    public function htmlEntityDecode($html)
    {
        $html = html_entity_decode($html);
        return $html;
    }

    public function getName()
    {
        return 'html_entity_decode_extension';
    }
}
Run Code Online (Sandbox Code Playgroud)

但是即使这样,它仍然会显示01%2F04%2F2016

在我的控制器方法中,我得到了相同的结果:

echo html_entity_decode($request->query->get('date'));
Run Code Online (Sandbox Code Playgroud)

正确的方法是什么?

更新:

日期来自“文本”类型的输入。不,这是一个带有数字和/的简单字符串。

php symfony twig twig-extension

5
推荐指数
1
解决办法
6200
查看次数

Symfony Twig Extension打破其他服务 - 在安全之前进行模板化吗?

我正在研究Symfony 2.7 WebApp.我创建的一个包包括一个提供一些用户相关内容的服务,例如userHasPurchases().

问题是,包括Twig Extesion打破另一项服务:

AppShopService

namespace AppShopBundle\Service;

use AppBundle\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
...

class AppShopService {
    protected $user;

    public function __construct(TokenStorageInterface $tokenStorage, ...) {
        $this->user = $tokenStorage->getToken() ? $tokenStorage->getToken()->getUser() : null;
        ...
    }

    public function userHasPurchases(User $user) {
        $user = $user ? $user : $this->user;
        $result = $user...
        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

AppShopBundle \资源\设置\ services.yml

services:
    app_shop.service:
        class: AppShopBundle\Service\AppShopService
        arguments: 
            - "@security.token_storage"
            - ...
Run Code Online (Sandbox Code Playgroud)

到目前为止一切正常:AppShopServices使用当前用户创建userHasPurchases()并按预期工作.

现在我添加了一个Twig扩展,以便能够userHasPurchases()在我的模板中使用:

树枝延伸

namespace …
Run Code Online (Sandbox Code Playgroud)

php symfony twig twig-extension

5
推荐指数
1
解决办法
116
查看次数

Twig转储不打印任何东西

我现在有点困惑.我总是使用twig dump函数像任何其他twig函数,但现在它绝对没有输出.没有错误/异常,只是没有.其他一切工作正常,如反式过滤器.

{{ dump('test') }} # prints nothing
{{ 'layout.booking.chooseArea'|trans }} # prints the translated message
Run Code Online (Sandbox Code Playgroud)

现在这个模板不包含任何东西.转储在父模板或base.html.twig中也不起作用.

同样,转储什么都不打印:不是空字符串,不是空,而不是屏幕上的单个像素.

有什么想法会导致这种情况吗?

Symfony版本:2.6.x-dev

更新

{{ dump('test') }} # doesn't work (anymore?)
{% dump('test') %} # does (still) work
Run Code Online (Sandbox Code Playgroud)

这已被删除或什么?为什么没有错误?顺便说一句...设置调试标志.

symfony twig symfony-2.6 twig-extension

3
推荐指数
1
解决办法
1226
查看次数

Symfony2中的多个Twig_Extension

我想在单独的包中注册我的自定义Twig过滤器(以避免有一个巨大的文件).

我在每个包中都有Yaml配置:

# services.yml
services:
    twig.extension.[BundleName]:
        class: Kuba\[BundleName]\Twig\AppExtension
        public: false
        tags:
            - { name: twig.extension }
Run Code Online (Sandbox Code Playgroud)

然而,最后(按字典顺序)扩展一直覆盖前一个扩展.

如何注册多个扩展名,如果不可能,最好的做法是拆分代码?

symfony twig twig-extension

3
推荐指数
1
解决办法
403
查看次数

如何让Twig将HTML输出写入文件而不是浏览器?

我有一个我要渲染的Twig模板(.html文件)(用我的PHP文件中的值替换标记).但是,我想将其写入新的.html文件,而不是将渲染结果发送到浏览器.我想这个Django模板的解决方案也类似.

我怎么能用PHP做到这一点?

symfony twig twig-extension

3
推荐指数
1
解决办法
1861
查看次数

Timber(Twig)如何调用wordpress函数

在Timber中就是这个功能

{{post}}
Run Code Online (Sandbox Code Playgroud)

你得到的东西

{{post.post_title}}
Run Code Online (Sandbox Code Playgroud)

但是WordPress中还有其他功能

get_the_permalink();
Run Code Online (Sandbox Code Playgroud)

无法从post.get_the_permalink或post.get_permalink访问

这些是我可以访问的变量

  • ID
  • post_author
  • 发布日期
  • post_date_gmt
  • POST_CONTENT
  • 帖子标题
  • post_excerpt
  • post_status
  • comment_status
  • ping_status
  • post_password
  • POST_NAME
  • to_ping
  • ping通
  • post_modified
  • post_modified_gmt
  • post_content_filtered
  • post_parent
  • GUID
  • menu_order
  • post_type
  • post_mime_type
  • COMMENT_COUNT
  • 过滤

正如这个Timber文档描述的那样有行动[Timber Doc],但你必须在functions.php文件中定义每个函数...这意味着我必须从php创建每个已经存在的函数并将其"重定向"到wordpress功能.然后将其绑定到Twig.

还有一种方法,你可以在这里看到现在弃用

php wordpress twig twig-extension timber

3
推荐指数
2
解决办法
6825
查看次数

Twig:如何检查变量是否为DateTime对象

我必须检查变量是DateTime对象还是简单的字符串才能在模板中使用它。

如果变量是DateTime,我必须将其格式化为日期;如果是字符串,只需打印即可。

{% if post.date is DateTime %}
    {% set postDate = post.date|date %}
{% else %}
    {% set postDate = post.date %}
{% endif %}

<p>Il {{ postDate }}
Run Code Online (Sandbox Code Playgroud)

我认为我应该使用Twig测试(也可以在关于StackOverflow的答案中建议)进行此操作arrays,但是我不太了解我应该将代码以及如何在应用程序中注册代码放在Symfony App的哪个文件夹中。

一旦编写了测试,如何在Symfony的Twig模板中使用它?

php symfony twig twig-extension

2
推荐指数
1
解决办法
2097
查看次数

允许在树枝中的沙箱策略中使用新方法(drupal 8)

使用“字段收集”模块时出现此错误:

 Twig_Sandbox_SecurityError: Calling "uri" method on a "Drupal\field_collection\Entity\FieldCollectionItem" object is not allowed in "themes/communitylife/templates/content/node.html.twig" at line 83. in Drupal\Core\Template\TwigSandboxPolicy->checkMethodAllowed() (line 99 of core/lib/Drupal/Core/Template/TwigSandboxPolicy.php).
Run Code Online (Sandbox Code Playgroud)

导致问题的代码是以下代码:

<div class=" title-col col-md-7">

     <a  href="{{file_url(node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_file.entity.uri.value)}}" target="_blank"> <strong> {{node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_description.value}}

      <span class="file-type"> ({{node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_file.entity.uri.value | slice(-3) }} </span>, <span class="file-size"> {{node.field_pressemappe_bildmaterial[key].getFieldCollectionItem().field_presse_bild_file.entity.size }}) </span> 

      </strong></a>

</div>
Run Code Online (Sandbox Code Playgroud)

解决此问题的最佳方法是什么?通过在沙箱策略的允许方法中添加(uri)来实现?如果是,那我该怎么做?

我在树枝文档中阅读到可以执行以下操作:

    $policy = new Twig_Sandbox_SecurityPolicy($tags, $filters, $methods, $properties, $functions);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何或在何处放置此代码。

提前致谢

symfony twig drupal-8 twig-extension

2
推荐指数
1
解决办法
851
查看次数

在 Symfony 中注册 Twig 扩展

我想使用nochso/html-compress-twig 扩展来压缩所有 html、内联 css 和 js。但这是我第一次在 Twig 上注册一个新扩展,我对应该在哪里添加以下几行感到有些困惑我的项目:

$twig = new Twig_Environment($loader);
$twig->addExtension(new \nochso\HtmlCompressTwig\Extension());
Run Code Online (Sandbox Code Playgroud)

我正在阅读 Twig 的文档,但它对我没有多大帮助,因为他们给出了相同的示例并添加了以下内容:

Twig does not care where you save your extension on the filesystem, as all extensions must be registered explicitly to be available in your templates.

You can register an extension by using the addExtension() method on your main Environment object:
Run Code Online (Sandbox Code Playgroud)

我只想全局启用扩展并能够{% htmlcompress %}{% endhtmlcompress %}在任何树枝模板中使用

php symfony twig twig-extension

2
推荐指数
1
解决办法
6725
查看次数

将键和值合并到Twig文件中的数组中

我想将键和值添加到树枝文件中的数组。但我面临以下问题"Twig_Error_Syntax: A hash key must be a quoted string or a number"

{% set phoneCount = 0 %}
{% set phoneNumbers = {} %}
{% for currPhone in currBroker.phones %}
    {% if (currPhone.type == 'Work' or currPhone.type == 'Mobile') and phoneCount <= 2 and currPhone.number !='' %}
        {% set phoneCount = phoneCount + 1 %}                   
        {% set phoneNumbers = phoneNumbers|merge({ currPhone.type:currPhone.type }) %}
    {% endif %}
{% endfor %}
{{ phoneNumbers|print_r }}
Run Code Online (Sandbox Code Playgroud)

我只需要将键和值合并到数组中的语法。我尝试通过提供静态输入及其工作

{% set phoneNumbers = phoneNumbers|merge({ 'work':'(011)112-1233' …
Run Code Online (Sandbox Code Playgroud)

php symfony twig-extension

1
推荐指数
1
解决办法
4472
查看次数

从树枝中的字符串中删除'\'

我有这个文字:

Ch d\`Arnb
Run Code Online (Sandbox Code Playgroud)

现在我想\从字符串中删除所有字符串.我尝试了很多解决方案,其中之一:{{ item|replace({'\/': ''}) }}.但是成功了.你能帮我吗 ?Thx提前和抱歉我的英语.

symfony twig twig-extension twig-filter

1
推荐指数
1
解决办法
196
查看次数

Twig包括:在传递的参数中使用翻译

这很好用

{% include 'site/snippet.html.twig'
    with {'description': 'Some text'}
%}
Run Code Online (Sandbox Code Playgroud)

但是如何使这个工作?使用翻译作为参数

{% include 'site/snippet.html.twig'
    with {'description': '{{ 'solutions.description' | trans }}'}
%}
Run Code Online (Sandbox Code Playgroud)

snippet.html内容为:

<p>
    {{ description }}
</p>
Run Code Online (Sandbox Code Playgroud)

并且{{ 'solutions.description' | trans }}单独调用翻译会按预期显示内容.

它会是什么语法?

php symfony twig twig-extension

0
推荐指数
1
解决办法
409
查看次数