【发布时间】:2012-04-02 09:41:10
【问题描述】:
如何编写这样的内容以包含在模板中,但在 Haml 中?
<script>
$(document).ready( function() {
$('body').addClass( 'test' );
} );
</script>
【问题讨论】:
标签: javascript jquery haml
如何编写这样的内容以包含在模板中,但在 Haml 中?
<script>
$(document).ready( function() {
$('body').addClass( 'test' );
} );
</script>
【问题讨论】:
标签: javascript jquery haml
:javascript
$(document).ready( function() {
$('body').addClass( 'test' );
} );
文档:http://haml.info/docs/yardoc/file.REFERENCE.html#javascript-filter
【讨论】:
您实际上可以执行 Chris Chalmers 在他的回答中所做的事情,但您必须确保 HAML 不会解析 JavaScript。当您需要使用与text/javascript 不同的类型时,这种方法实际上很有用,这是我需要为MathJax 做的。
您可以使用plain 过滤器来阻止 HAML 解析脚本并引发非法嵌套错误:
%script{type: "text/x-mathjax-config"}
:plain
MathJax.Hub.Config({
tex2jax: {
inlineMath: [["$","$"],["\\(","\\)"]]
}
});
【讨论】:
所以我尝试了上面的 :javascript 有效 :) 但是 HAML 将生成的代码包装在 CDATA 中,如下所示:
<script type="text/javascript">
//<![CDATA[
$(document).ready( function() {
$('body').addClass( 'test' );
} );
//]]>
</script>
以下 HAML 将生成包含(例如)typekit 或 google 分析代码的典型标记。
%script{:type=>"text/javascript"}
//your code goes here - dont forget the indent!
【讨论】:
CDATA,如果 js 中有任何不规则的缩进,%script 也没有为我工作。
我在 haml 中使用 fileupload-jquery。原js如下:
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else { %}
<td class="preview">{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>{%=locale.fileupload.destroy%}</span>
</button>
<input type="checkbox" name="delete" value="1">
</td>
</tr>
{% } %}
</script>
一开始我使用:cdata进行转换(来自html2haml),它不能正常工作(删除按钮不能删除回调中的相关组件)。
<script id='template-download' type='text/x-tmpl'>
<![CDATA[
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else { %}
<td class="preview">{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>{%=locale.fileupload.destroy%}</span>
</button>
<input type="checkbox" name="delete" value="1">
</td>
</tr>
{% } %}
]]>
</script>
所以我使用:plain 过滤器:
%script#template-download{:type => "text/x-tmpl"}
:plain
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else { %}
<td class="preview">{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>{%=locale.fileupload.destroy%}</span>
</button>
<input type="checkbox" name="delete" value="1">
</td>
</tr>
{% } %}
转换后的结果和原来的完全一样。
所以:plain 这个场景中的过滤器符合我的需要。
:plain 不解析过滤后的文本。当您不想以 . 开头的行时,这对于没有 HTML 标签的大块文本很有用。或 - 被解析。
更多详情请参考haml.info
【讨论】: