我希望使用JavaScript在客户端呈现我的haml代码。服务器端有很好的haml解析器,如Jade或haml.js,但我不知道客户端有任何haml解析器/解码器。
更新:Jade几乎是haml现在支持客户端。
答案 0 :(得分:8)
经过一些谷歌搜索后,我找到了"client-side-haml-js" github project。看起来它应该满足您的需求:
clientside-haml-js是用CoffeeScript编写的编译器 将HAML格式的文本模板编译成Javascript函数 生成HTML。它受到了服务器端haml Javascript的启发 项目,并已编写为与Ruby兼容的功能 服务器端HAML,支持所有主流浏览器(IE 7 +,Firefox 3.6 +, Chrome 10+,Safari)具有最小的运行时依赖性(仅限 underscore.js,underscore.string和CoffeeScript如果使用的话 模板中的CoffeeScript。
注意:haml编译器需要带有JSON解析器的浏览器。对于 像IE7这样的浏览器,您还需要包含JSON实现。看到 http://www.json.org/了解更多详情。 JSON实现是 可在https://github.com/douglascrockford/JSON-js获得。
来自他们的github页面的示例:
var fn = haml.compileStringToJs("%h1\n %div\n %p\n %span");
var html = fn();
看起来它也支持类似于jquery-templates的text / haml-template方法:
<script type="text/haml-template" id="simple">
%h1
%div
%p
%span
</script>
<script type="text/javascript">
var fn = haml.compileHaml('simple');
var html = fn();
</script>