简体   繁体   English

如何在Express&Node.js中创建一个包含所有.html模板的.js文件?

[英]How would I create a .js file that contains all of my .html templates in Express & Node.js?

I am using jquery templates on both the server-side and client-side. 我在服务器端和客户端都使用jquery模板。 I'm using the jqtpl and express modules on Node.js for the server, and I have jquery and its template plugin for the client. 我在服务器上使用Node.js上的jqtpl和express模块​​,并且为客户端提供了jquery及其模板插件。

Wherever possible the client recieves JSON instead of html, and builds the template itself, using the HTML5 history api to change url and allow session navigation. 客户端尽可能接收JSON而不是html,并使用HTML5历史记录api更改url并允许会话导航来构建模板本身。 If this isn't possible, the server does the templating instead and sends html. 如果无法做到这一点,则服务器改为执行模板并发送html。

Both the server and the client have the exact same templates, but the server has them in the form of 12 .html files, and the client has them in the form of an object in a .js file. 服务器和客户端都具有完全相同的模板,但是服务器以12个.html文件的形式存在它们,而客户端以.js文件中的对象形式存在它们。

Each time I change or add a template, I have to alter the client .js file to be the same, which is a bit of a pain. 每次更改或添加模板时,都必须将客户端.js文件更改为相同,这有点麻烦。

What I would like to do is have the .js file dynamically take all of the .html files in my templates folder and compile them into an object. 我想做的是让.js文件动态获取我的template文件夹中的所有.html文件,并将它们编译为一个对象。 Once I had finished development, this file would be cached, as it no longer needs to look for changes. 一旦完成开发,该文件将被缓存,因为它不再需要查找更改。

Not sure if this answers your question, but if you're interested in having the same templating code on both client and server, you should take a look at weld which would allow you to use the same code on both client and server. 不知道这是否能回答您的问题,但是如果您有兴趣在客户端和服务器上使用相同的模板代码,则应该研究一下焊接 ,以便您可以在客户端和服务器上使用相同的代码。

Could you be a bit more clear about what you're doing with regards to the 12 html files vs 1 object on the client? 关于客户端上的12个html文件和1个对象,您能否更清楚地了解自己在做什么?

Code samples would rock, if possible. 如果可能的话,代码示例会摇摆不定。

What I was looking for was Node's file system module: 我正在寻找的是Node的文件系统模块:

http://nodejs.org/docs/v0.4.8/api/fs.html http://nodejs.org/docs/v0.4.8/api/fs.html

I used fs.readdir to find all of the template files in the folder, then iterated through the array that produced to read all of its containing files. 我使用fs.readdir在文件夹中找到所有模板文件,然后遍历生成的数组以读取其所有包含文件。

var templates = {};
fs.readdir("./templates", function(err, files) {
    for(var i = 0, len = files.length; i<len; i++) {
        fs.readFile("./templates/" + files[i], function (err, data) {
            if (err) {
                throw err
            }
            templates[files[i]] = data;
        });
    }
}

And then in express: 然后在快递:

app.get("/templates.js", function(req, res){
    res.header("Content-Type", "text/javascript");
    res.end("var templates = { " + JSON.stringify(templates) + " }; ");
});

However, I found that server-side templating was better suited to my needs, and where appropriate, I now send rendered HTML in my JSON responses instead of just data. 但是,我发现服务器端模板更适合我的需求,并且在适当的时候,我现在在JSON响应中发送渲染的HTML,而不仅仅是数据。

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

相关问题 如何使用 node.js 修改我的 HTML 文件? - How can I modify my HTML file with node.js? 如何使用express 3.x在node.js中的EJS模板中包含js文件 - How to include js files in my EJS templates in node.js using express 3.x 我可以将.ejs文件更改为.html以使其与Node.js和Express一起使用吗? - Can I change my .ejs file to .html to make it work with Node.js and Express? 如何在 html 文件中使用 Node.js Express 功能 - How to use Node.js Express function in html file 如果我使用 jQuery 的 ajax 方法将 CSV 文件发送到服务器,文件将位于我的 Node.js + Express 服务器上的什么位置? - Where would a file be located on my Node.js + Express server if I send a CSV file to the server using jQuery's ajax method? 如何使用 express 读取我的 css 文件 node.js - How to read my css file with express, node.js 如何使用express node.js打印到html - How to print to html with express node.js 如何将我的所有资产(Html、JS、CSS、node.JS)放入反应中? - How can I put my all asset(Html, JS, CSS, node.JS) into the react? 我将如何解析 node.js 中的大型 TSV 文件? - How would I parse a large TSV file in node.js? 如何使用node.js生成xml文件并将其放在服务器上的公共目录中? - How would I generate an xml file with node.js and put it in a public directory on my server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM