在erb模板中嵌入ejs模板 [英] Embedding an ejs template inside of an erb template

查看:54
本文介绍了在erb模板中嵌入ejs模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个JavaScript繁重的Rails 3应用程序。它使用underscore.js,它具有基于ejs( http://embeddedjs.com/ )构建的非常优雅的模板机制。

I'm building a javascript-heavy rails 3 app. It uses underscore.js, which has a very elegant templating mechanism built on top of ejs ( http://embeddedjs.com/).

问题:Embeddedjs大量借鉴了erb语法,因此在erb模板中包含ejs模板会导致视图渲染问题。

The problem: embeddedjs borrows heavily from the erb syntax, so including ejs templates in an erb template causes rendering problems with the view.

是否可以在erb文件中包含非erb部分?这可以让我在erb文件中定义ejs模板。现在,我正在使用一个黑客工具,其中有一个帮助程序,该帮助程序读取包含ejs模板的文件的原始内容,并将其作为原始字符串输出到erb模板中。

Is there a way to include "non-erb" sections in an erb file? This would let me define ejs templates inside erb files. Right now I'm using a hack where I have a helper that reads the raw contents of a file containing ejs templates, and outputting that as a raw string in the erb template.

推荐答案

我使用此技巧来解决问题:

I use this trick to solve the problem:

// Using custom tags to be able to use regular for templates in templates
var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';

// Using html extension for custom ejs tags
app.register('.html', ejs);

app.set('views', __dirname + '/views');
app.set('view engine', 'html');

这将<%%>更改为{{}},让我使用<%% >用于JS使用的模板。这对我有用,因为我没有经典的样式模板(<%%>)。

This changes <% %> to {{ }}, and let me use <% %> for templates which are used by JS. This works for me as I don't have classic style templates (<% %>).

如果您有很多这样的模板,则可能希望这样做技巧,但适用于underscore.js模板。

If you have a lot of those you may want to do the same trick but for underscore.js templates.

这篇关于在erb模板中嵌入ejs模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆