简体   繁体   English

在 ejs 模板引擎中包含非 ejs 文件

[英]Include non ejs files in ejs template engine

Is it posible to add files that dont have the .ejs ending when using the ejs template engine with node.js and express?在将 ejs 模板引擎与 node.js 和 express 一起使用时,是否可以添加不以 .ejs 结尾的文件?

I want to put shaders in seperate files with .glsl ending for example.例如,我想将着色器放在以 .glsl 结尾的单独文件中。 Than I want to include them like this:我想像这样包含它们:

<% include /shaders/example.vertex.glsl %>

If I use example.vertex.glsl.ejs it works, but than syntax highlighting for shaders is not working anymore...如果我使用 example.vertex.glsl.ejs 它可以工作,但是着色器的语法高亮不再工作......

I am actually not bound to ejs.我实际上不受 ejs 的约束。 Any other express templating engine this would work in is fine.任何其他可以使用的快速模板引擎都可以。 I just used ejs because I worked with it already a little bit beforehand.我只是使用 ejs,因为我事先已经使用过它了。

You can do this as long as you tell the express what engine should be used to process the extension with app.engine(ext, callback) .你可以这样做,只要你告诉 express 应该使用什么引擎来处理app.engine(ext, callback)的扩展。

Set the following before your routes somewhere:在您的路线之前设置以下内容:

app.engine('glsl', require('ejs').renderFile);

Then your includes such as <% include /shaders/example.vertex.glsl %> will work fine.然后您的包含(例如<% include /shaders/example.vertex.glsl %>将正常工作。 These are processed in the same way as ejs files then, so as well as the include working as you describe, you can also put inline <%=variableFromNode%> type stuff in the included glsl files and it will process that too.这些文件的处理方式与 ejs 文件相同,因此除了按照您描述的方式工作外,您还可以将内联<%=variableFromNode%>类型的内容放入包含的 glsl 文件中,它也会处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM