Symfony 2,Twig:如何不逃避字段值(与backbonejs和symfony 2一起使用)(Symfony 2, Twig: how not to escape field value (used with backbonejs & symfony 2))
我正在使用以下代码渲染原型:
{{form_widget(form.get('prototype').myField, {'attr': {'value': '<%= myModelProperty %>'} }) }}
BackboneJS应该读取此twig块生成的代码,并用某个model属性值替换<%= myModelProperty%>。
并且这不会发生,因为值在树枝中被转义,因此被替换为:
<%= viewport %>
我试图在* form_div_layout.html *文件中将值强制为RAW:
> {% block field_widget %} {% spaceless %} > {% set type = type|default('text') %} > <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value|raw }}" {% endif %}/> {% > endspaceless %} {% endblock field_widget %}
但没有成功。
所以我的问题是如何避免在树枝中逃脱现场价值。
谢谢!
编辑
解决方案:事实上,该方法是正确的,我必须使用“原始”过滤器来获取我的变量未转义。 我有一个autoescape块集,它可以使这个特定的输出成为可能,这就是为什么它必须是“未转义”的原因。
Symfony 2的Twig包提供了几个块来呈现表单数据,并且这些块使用特定的块来进行属性呈现,称为“{%block widget_attributes%}”。
我做的是编辑这个块(我有一个单独的模板文件,包含我所有的自定义块),所以我可以添加一层“如果这个值被转义”:
{% block widget_attributes %} {% spaceless %} id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %} {% for attrname,attrvalue in attr %} {# Attribute value can be defined as an array. ['data']: contains the actual value, ['escape']: boolean (true if the value HAS to be escaped)#} {% if attrvalue.data is defined %} {% if not attrvalue.escape %} {{attrname}}="{{ attrvalue.data|raw }}" {% else %} {{attrname}}="{{ attrvalue.data|e }}" {% endif %} {% else %} {{attrname}}="{{attrvalue}}" {% endif %} {% endfor %} {% endspaceless %} {% endblock widget_attributes %}
从我的twig文件中调用:
{{ form_widget(form.displays.get('prototype').myField, {'attr': {'value': { 'data': myvalue, 'escape': false } } }) }}
在{{}} twig标签中打印值时完成转义,因此我之前所做的是将未转义的值发送到实际调用打印的块以及因此转义值的位置。
这适合我! 谢谢!
I'm rendering a prototype using below code:
{{form_widget(form.get('prototype').myField, {'attr': {'value': '<%= myModelProperty %>'} }) }}
BackboneJS is supposed to read the code generated by this twig block, and replace the <%= myModelProperty %> by some model property value.
And this doesn't happen because the value is escaped in twig and thus replaced by:
<%= viewport %>
I've tried to force the value to RAW in the *form_div_layout.html* file:
> {% block field_widget %} {% spaceless %} > {% set type = type|default('text') %} > <input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value|raw }}" {% endif %}/> {% > endspaceless %} {% endblock field_widget %}
but without success.
So my question is how not to escape the field value in twig.
Thanks!
EDIT
Solution: So in fact the method was right, I have to use the "raw" filter to get my variable not escaped. I've an autoescape block set that englobe this particular output which is why the reason it has to be "un-escaped".
Twig bundle of Symfony 2 provided several block to render form data, and those uses a specific block for attribute rendering called "{% block widget_attributes %}".
What I did is edit this block (I've a separated template file with all my customized blocks) so I can add a layer of "should this value be escaped or not":
{% block widget_attributes %} {% spaceless %} id="{{ id }}" name="{{ full_name }}"{% if read_only %} disabled="disabled"{% endif %}{% if required %} required="required"{% endif %}{% if max_length %} maxlength="{{ max_length }}"{% endif %}{% if pattern %} pattern="{{ pattern }}"{% endif %} {% for attrname,attrvalue in attr %} {# Attribute value can be defined as an array. ['data']: contains the actual value, ['escape']: boolean (true if the value HAS to be escaped)#} {% if attrvalue.data is defined %} {% if not attrvalue.escape %} {{attrname}}="{{ attrvalue.data|raw }}" {% else %} {{attrname}}="{{ attrvalue.data|e }}" {% endif %} {% else %} {{attrname}}="{{attrvalue}}" {% endif %} {% endfor %} {% endspaceless %} {% endblock widget_attributes %}
Called from my twig file:
{{ form_widget(form.displays.get('prototype').myField, {'attr': {'value': { 'data': myvalue, 'escape': false } } }) }}
The escape is done when printing the value so in the {{ }} twig tag, so what I was doing earlier was sending an unescaped value to a block where the print is actually called and where the value was thus escaped.
This works for me! thanks!
原文:https://stackoverflow.com/questions/11417510
最满意答案
你得到的错误是(在toploop中键入以下内容)¹:
# (Obj.magic 43: Unix.error);; - : Unix.error = Unix.EPROTOTYPE
这意味着: 协议错误类型的套接字 。 因此,您必须检查如何初始化套接字。
¹您还可以计算
unix.mli
的异常,因为知道第一个例外,E2BIG
为0
。 EmacsCu 43 ↓
帮助。The error you get is (type the following in the toploop)¹:
# (Obj.magic 43: Unix.error);; - : Unix.error = Unix.EPROTOTYPE
which means: Protocol wrong type for socket. So you have to examine how you initialize your socket.
¹ You can also count the exceptions in
unix.mli
, knowing that the first one,E2BIG
, is0
. EmacsC-u 43 ↓
helps.
相关问答
更多-
错误:(31,0)未找到Gradle DSL方法:'jniDebugBuild()'(Error:(31, 0) Gradle DSL method not found: 'jniDebugBuild()')[2022-09-13]
该属性的名称在Android Gradle插件的v0.14.0及更高版本中已更改,因此需要更新为早期版本创建的构建文件。 浏览项目中的所有build.gradle文件,当看到jniDebugBuild ,将其更改为jniDebuggable 。 你可能遇到其他需要重新命名的东西; 重新映射列表位于http://tools.android.com/tech-docs/new-build-system上的发行说明中。 The name of this property has changed in v0.14. ... -
使用PowerShell将数据写入31天中的每一天(Write data into table for each of the 31 days using PowerShell)[2022-07-25]
我要补充一个答案。 虽然我的另一个答案对你的情况不正确,但我想留下它,以防它为别人增加价值。 我们需要做的是获取所有$realsessions ,同时保持他们的endtime来自Get-VBRBackupSession的原始输出 $days = 31 $sessions = Get-VBRBackupSession | Where-Object {$_.JobType -eq "Backup" -and $_.EndTime -ge (Get-Date).AddDays(-$days)} $realsess ... -
问题是'sechs'的月份没有设定。 这是因为您正在调用sechs.withMonthOfYear但是将其分配给austritt 。 withMonthOfYear返回修改后的副本,它不会修改调用它的元素。 考虑以下: austritt = sechs.withYea(...) .withMonthOfYear(...) .withDayOfMonth(...); 每个API 返回此日期的副本,并更新月份字段。 DateMidnight是不可 ...
-
使用UIAccessibilityTraits时出现三个20 p31语法错误(three20 p31 syntax error with UIAccessibilityTraits)[2022-12-29]
您使用的是哪个版本的iPhone SDK以及您编译的版本是什么? 可访问性特征在3.0中添加。 What version of the iPhone SDK do you use and what version do you compile for? The accessibility traits were added in 3.0. -
为什么SimpleDateFormat显示1969年12月31日的unix日期?(Why does SimpleDateFormat show unix date as December 31 1969?)[2022-08-25]
因为DateFormat也有一个TimeZone (并且您的系统未设置为UTC)。 您可以使用DateFormat.setTimeZone(TimeZone)更改它。 就像是 String myFormat = "MM/dd/yyyy"; SimpleDateFormat sdf = new SimpleDateFormat(myFormat); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); System.out.println("Printable: " + ... -
这是旧版OpenJDK中的一个已知错误 。 你应该尝试更新到至少8u40,因为它显然在那里解决了。 This is a known bug in an old version of OpenJDK. You should try updating to at least 8u40 as it is apparently resolved there.
-
不确定这是否适合你。 在我的机器上,我在编译时遇到的唯一错误与表DBRO有关(因为我没有该名称的表),但这并不意味着这会进行您希望它进行的计算。 无论如何,如果你有更多的问题,请使用它并回写。 我将NUMERIC更改为NUMBER(数字的Oracle数据类型的正确名称),我改变了IntDATEDIFF的计算方式,当你查看它时会立即显而易见(Trunc将Oracle DATE的时间部分更改为00: 00:00,并在两个日期之间取算术“减去”,返回差值为天数),我删除了BEGIN和END; 对于内部块 - 不需 ...
-
你得到的错误是(在toploop中键入以下内容)¹: # (Obj.magic 43: Unix.error);; - : Unix.error = Unix.EPROTOTYPE 这意味着: 协议错误类型的套接字 。 因此,您必须检查如何初始化套接字。 ¹您还可以计算unix.mli的异常,因为知道第一个例外, E2BIG为0 。 Emacs Cu 43 ↓帮助。 The error you get is (type the following in the toploop)¹: # (Obj.magi ...
-
它适用于我。 这是一个顶级会话(Mac OS X 10.8.2): $ ocaml OCaml version 4.00.0 # #load "unix.cma";; # open Unix;; # let t = Unix.localtime (Unix.time ());; val t : Unix.tm = {tm_sec = 39; tm_min = 27; tm_hour = 16; tm_mday = 3; tm_mon = 11; tm_year = 112; tm ...
-
如果您正在使用angular-cli将静态文件(如images , fonts等)放在assets文件夹中,并在HTML文件中引用它们,如下所示 If you are using angular-cli put static files such as images, fonts etc.. in assets folder and refer them in you HTML files as follows
相关文章
更多- Symfony2网站开发
- Solr4.7---Field、CopyField、DynamicField
- 我是如何在15天开发出一个网站的
- Solr Document [null] missing required field: id 的原因
- solr error logs org.apache.solr.common.SolrException: ERROR: [doc=17] unknown field alias
- solr required field: id
- Solr/Lucene escape char handling
- 启动Solr时报 _version_ field must exist in schema 错误的解决方法
- Solr boost某字段的特殊值
- elasticsearch 添加字段
最新问答
更多- 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
- 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
- OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
- 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
- codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
- 在计算机拍照在哪里进入
- 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
- No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
- 单页应用程序:页面重新加载(Single Page Application: page reload)
- 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
- System.StackOverflow错误(System.StackOverflow error)
- KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
- 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
- android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
- TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
- 企业安全培训的各项内容
- 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
- C#类名中允许哪些字符?(What characters are allowed in C# class name?)
- NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
- 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
- 将多个行和可变行移动到列(moving multiple and variable rows to columns)
- 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
- 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
- Angular $资源不会改变方法(Angular $resource doesn't change method)
- 在Angular 5中不是一个函数(is not a function in Angular 5)
- 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
- 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
- 常见的python rpc和cli接口(Common python rpc and cli interface)
- Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
- 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)