2016-04-29 127 views
3

From the TWIG documentaion about json_encode() filter他們說:嫩枝json_encode多個選項

json_encode

的json_encode過濾器返回一個值的JSON表示:

{{ data|json_encode() }} 

內部,嫩枝使用PHP json_encode功能。

參數

選項:的json_encode選項

({{data|json_encode(constant('JSON_PRETTY_PRINT')) }}) 

我試圖做的位掩碼是添加的這些選項多。

我想JSON_PRETTY_PRINTJSON_UNESCAPED_SLASHES

我已經試過

{{ array|json_encode(constant('JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES')) }} 
{{ array|json_encode(constant('JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES')) }} 
{{ array|json_encode(constant('JSON_PRETTY_PRINT', 'JSON_UNESCAPED_SLASHES')) }} 

但他們沒有工作。我怎樣才能將TWIGs json_encode()過濾器的兩個選項結合起來?

TwigFiddle here

{% set array = {'xxx': "one", 'yyy': "two", 'path': "/hello/world" } %} 

{% autoescape false %} 
    {{ array|json_encode() }} 
    {{ array|json_encode(constant('JSON_PRETTY_PRINT')) }} 
    {{ array|json_encode(constant('JSON_UNESCAPED_SLASHES')) }} 
{% endautoescape %} 

所需的輸出應該是

{ 
    "xxx": "one", 
    "yyy": "two", 
    "path": "/hello/world" 
} 

回答

7

看來你需要b-or在樹枝按位或操作(docs)。

所以這樣的事情應該工作:

{{ array|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_UNESCAPED_SLASHES')) }}