新手问题 <%%= xxx %>双百分号啥个意思

as181920 · 2014年02月21日 · 最后由 jicheng1014 回复于 2014年02月25日 · 7598 次阅读

有 html 代码加了 js 后好像可以动态写一些 template,如

<%for(var i=0,il=data.length;i<il> ... <p>这个时候在 erb 文件里面会有问题,改成&lt;%% xxx %&gt;即可,这种双百分号是啥个意思?我肯定漏了哪个基础知识哈。</p> </il>

我倒是见到过这样写的 <%== yield %>,顺便搭车求答案。。。

看起来怪怪的 <% XXX %> 里的 XXX 应该是 ruby 代码吧,但你这个似乎像是 js 代码?

我猜:双层的编译。先转换一次,把<%%= AAA %>变回<%= BBB %>,再把<%= BBB %>变成结果 CCC……

From https://github.com/jnicklas/templater#templates

If you need to render templates where the result should contain actual erb, simply use a double percentage sign, this will prevent the statement from being executed.

例如:

<%= 2+2 %> 输出 4

<%%= 2+2 %> 输出 <%= 2+2 %> 可以避免执行 erb

头一次看到这种写法,学习了 :plus1: 一般用 <%= 来注释

#1 楼 @ywjno <%== something %><%= raw something %>的简写。见:http://edgeguides.rubyonrails.org/active_support_core_extensions.html#output-safety

To insert something verbatim use the raw helper rather than calling html_safe:

<%= raw @cms.current_template %> <%# inserts @cms.current_template as is %>

or, equivalently, use <%==:

<%== @cms.current_template %> <%# inserts @cms.current_template as is %>

跟一些前端模板混用的时候会需要这个语法

#2 楼 @jicheng1014 上面单百分号其实是 html/js 模板语法,只是和 erb 语法同样了;下面双百分号是 erb 语法

#7 楼 @edokeh 对的,上面那个是 js 引入的语法,下面 erb 就需要如此来正确解析,最近才遇到。

#8 楼 @as181920 受教了,多谢指点

需要 登录 后方可回复, 如果你还没有账号请 注册新账号