[英]html template + json data -> static html deploy
is it possible to have:是否有可能:
- a static html template - 静态 html 模板
- a JSON with some data - 带有一些数据的 JSON
and create static html file(s)?并创建静态 html 文件?
For example i have to make a portfolio and i code html template:例如,我必须制作一个投资组合并编写 html 模板:
...
<h1> {title} </h1>
<p> {description} </p>
...
Then i have a JSON like this:然后我有一个这样的JSON:
"first work" : {
"title" : "alpha",
"description" : "lorem ipsum"
},
"second work" : {
"title" : "beta",
"description" : "lorem ipsum"
}
I want to "deploy" my website and have 2 static html file我想“部署”我的网站并有 2 个静态 html 文件
first_work.html first_work.html
<h1> alpha </h1>
<p> lorem ipsum </p>
second_work.html second_work.html
<h1> beta </h1>
<p> lorem ipsum </p>
I know Jekyll that uses markdown to produce static html but i prefer JSON in this situation.我知道 Jekyll 使用 Markdown 生成静态 html,但在这种情况下我更喜欢 JSON。
I just wrote a complete program in node.js doing it :我刚刚在 node.js 中编写了一个完整的程序:
var fs = require("fs");
var f = fs.readFileSync('./site.json').toString();
var pages = JSON.parse(f);
for (var key in pages) {
var page = pages[key];
fs.writeFile(
key.replace(/ /g, '_')+'.html',
'<h1>'+page.title+'</h1>'
+ '<p>'+page.description+'</p>'
);
}
If your JSON is in a file named site.json
(note that a comma is missing in your JSON), it writes the two HTML files.如果您的 JSON 位于名为
site.json
的文件中(请注意,您的 JSON 中缺少逗号),它会写入两个 HTML 文件。
Yeah, that is possible.是的,这是可能的。 I would recommend using php cause of same origin policy.. Just load the json from a file or directly and then create a new document with the required contents.
我建议使用同源策略的 php 原因。只需从文件或直接加载 json,然后创建一个包含所需内容的新文档。 It is easily done with phps file_put_contents().
使用 phps file_put_contents() 很容易完成。
Greets问候
Great, one little thing.太好了,一件小事。 Looks like
fs.writeFileSync()
instead of fs.writeFile()
看起来像
fs.writeFileSync()
而不是fs.writeFile()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.