Using interpolation within Node.js EJS includes

蓝咒 提交于 2019-12-03 10:29:28

I think there is no way to do this kind of dynamic includes in EJS. It might break the separation of business logic and view. The solution can be to rendering the subtemplate in the controller, and passing its content to the layout.

For rendering subtemplate in the controller use something like this:

var ejs = require('ejs'),
, fs = require('fs')
, home = ejs.render(fs.readFileSync("contents/home.ejs", "utf-8"))

In the version 2 of EJS, the include function does it well. With it, includes are inserted at runtime so variables can be used as pathnames.

In this case, the solution may be :

<%- include('contents/' + contents) %>

The function can also have another argument if necessary :

<%- include('mypathname', {foo:"bar"}) %>

The pathname has to be relative to the template which calls the function.

Currently this hasn't been implemented into ejs but, there is this discussion and pull request that offers the functionality.

https://github.com/visionmedia/ejs/issues/93

in your render function you can include fs.readFileSync and __dirname.

Render your page with options like this

res.render('pages/'+req.file,{file_get_contents:fs.readFileSync,__dirname:__dirname});

Then you can use it in your .ejs page like this. This remains in server side.

<% var products=JSON.parse(file_get_contents(__dirname+'/web/data/products.json','utf8')) %>

You can print the data on client HTML like this.

<%- JSON.stringify(products)%>

Note : Using this method means you have fs included somewhere at the top of your script.

var fs = require('fs')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!