我嘗試了無數的選項,但是一旦值字串中有空格,我就永遠不會用引號將整個值括起來:
{% set result_string= elements
? ( "data-custom-attribute=%s"|format( elements ) )
: ''
%}
例如,您如何使這項作業獲得data-custom-attribute="this is a test"具有價值的元素'this is a test'?
uj5u.com熱心網友回復:
過濾器format不會為您在代碼段中添加的示例添加任何引號。
您可能正在某些開發人員工具中驗證您的輸出,這些工具會給出誤報。使用生成的源 ( CTRL U)驗證您的輸出
在變數中添加引號elements或將它們添加到您的html代碼段中:
{% set elements = "\"foo bar foo\"" %}
{% set result_string= elements
? ( "data-custom-attribute=%s"|format( elements ) )
: ''
%}
{% set elements = "foo bar foo" %}
{% set result_string= elements
? ( "data-custom-attribute=\"%s\""|format( elements ) )
: ''
%}
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471468.html
