英文:
Generate html from a json file with a premade structure in github pages
问题
我有以下模板用于我的网站上的每个条目:
<div class="entry">
<div class="image-container">
<img src="images/placeholder.png" alt="Image N">
</div>
<div class="entry-content">
<div class="entry-title">EntryName</div>
<div class="entry-rating">Rating: X</div>
<div class="entry-comment">Comment: Lorem ipsum</div>
</div>
</div>
我希望能够自动从JSON文件中填充每个条目的图像/标题/评分/评论。我本来想使用Angular和它的for循环来实现这个目标,但在GitHub Pages上设置它时遇到了问题(还有嵌套的Python脚本)。
是否有办法使用JavaScript来实现这个功能?这些条目可能会有多达200个。
英文:
i have following template for each entry on my website
<div class="image-container">
<img src="images/placeholder.png" alt="Image N">
</div>
<div class="entry-content">
<div class="entry-title">EntryName</div>
<div class="entry-rating">Rating: X</div>
<div class="entry-comment">Comment: Lorem ipsum</div>
</div>
</div>
and i want it to automatically fill each image/title/rating/comment from a JSON file.
I would use angular and its for loop to do it but i had problems setting it up on github pages (aswell as nested python scripts).
is there a way to make it work with JS? The entries would be up to 200 entries long
答案1
得分: 1
这里有一种可能有点复杂的方法来完成这个任务,也可能有一些库可以使用,但对于这种情况,只使用一个简单的字符串模板并替换一些标记就能很好地工作。
请参考下面的示例。
<div class="image-container">
<img src="{{imgPath}}" alt="{{imgAlt}}">
</div>
<div class="entry-content">
<div class="entry-title">{{entryTitle}}</div>
<div class="entry-rating">Rating: {{entryRating}}</div>
<div class="entry-comment">Comment: {{entryComment}}</div>
</div>
const template = `<div class="image-container">
<img src="{{imgPath}}" alt="{{imgAlt}}">
</div>
<div class="entry-content">
<div class="entry-title">{{entryTitle}}</div>
<div class="entry-rating">Rating: {{entryRating}}</div>
<div class="entry-comment">Comment: {{entryComment}}</div>
</div>`;
const data = [
{
imgPath: 'https://picsum.photos/id/24/200/200',
imgAlt: 'a picture of a book',
entryTitle: 'BOOK',
entryRating: '4',
entryComment: 'Nice'
},
{
imgPath: 'https://picsum.photos/id/23/200/200',
imgAlt: 'an image of forks',
entryTitle: 'FORKS',
entryRating: '3',
entryComment: 'Food!'
},
{
imgPath: 'https://picsum.photos/id/2/200/200',
imgAlt: 'a laptop at a coffee shop',
entryTitle: 'LAPTOP',
entryRating: '5',
entryComment: 'I like coffee'
},
];
let htmlString = '';
data.forEach(d => {
let tmplCopy = template;
Object.keys(d).forEach(k => {
const pattern = new RegExp('{{' + k + '}}', 'g');
tmplCopy = tmplCopy.replace(pattern, d[k]);
});
htmlString += tmplCopy;
});
document.getElementById('output-container').innerHTML = htmlString;
<div id="output-container"></div>
英文:
There are probably some complicated ways to do this, and possibly some libraries you would use, but just a simple string template with some tokens to replace works pretty well for something like this.
See the example below.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const template = `<div class="image-container">
<img src="{{imgPath}}" alt="{{imgAlt}}">
</div>
<div class="entry-content">
<div class="entry-title">{{entryTitle}}</div>
<div class="entry-rating">Rating: {{entryRating}}</div>
<div class="entry-comment">Comment: {{entryComment}}</div>
</div>
</div>`;
const data = [
{
imgPath:'https://picsum.photos/id/24/200/200',
imgAlt:'a picture of a book',
entryTitle:'BOOK',
entryRating:'4',
entryComment:'Nice'
},
{
imgPath:'https://picsum.photos/id/23/200/200',
imgAlt:'an image of forks',
entryTitle:'FORKS',
entryRating:'3',
entryComment:'Food!'
},
{
imgPath:'https://picsum.photos/id/2/200/200',
imgAlt:'a laptop at a coffee shop',
entryTitle:'LAPTOP',
entryRating:'5',
entryComment:'I like coffee'
},
];
let htmlString = '';
data.forEach(d => {
let tmplCopy = template;
Object.keys(d).forEach(k => {
const pattern = new RegExp('{{' + k + '}}', 'g');
tmplCopy = tmplCopy.replace(pattern, d[k]);
});
htmlString += tmplCopy;
});
document.getElementById('output-container').innerHTML = htmlString;
<!-- language: lang-html -->
<div id="output-container"></div>
<!-- end snippet -->
答案2
得分: 1
在 GitHub 页面上,我认为你需要使用纯 CSS。它会看起来像这样:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let page = document.querySelector("#page");
let entries = [
{ title: "Title1", rating: "Rating1", comment: "comment1" },
{ title: "Title2", rating: "Rating2", comment: "comment2" },
// ...
];
//你需要从你的 JSON 文件中使用 JSON.parse() 获取 entries 数组
entries.forEach((entry) => {
page.innerHTML += `<div class="entry-content">
<div class="entry-title">${entry.title}</div>
<div class="entry-rating">Rating: ${entry.rating}</div>
<div class="entry-comment">Comment: ${entry.comment}</div>
</div>`;
});
<!-- language: lang-html -->
<div id="page"></div>
<!-- end snippet -->
请注意,上述代码中的 HTML 和 JavaScript 部分没有进行翻译,只提供了原始的代码。
英文:
On github pages I think that you will have to use pure css.
It would look like this :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let page = document.querySelector("#page");
let entries = [
{ title: "Title1", rating: "Rating1", comment: "comment1" },
{ title: "Title2", rating: "Rating2", comment: "comment2" },
// ...
];
//You will have to get the entries array from your json file using JSON.parse()
entries.forEach((entry) => {
page.innerHTML += `<div class="entry-content">
<div class="entry-title">${entry.title}</div>
<div class="entry-rating">Rating: ${entry.rating}</div>
<div class="entry-comment">Comment: ${entry.comment}</div>
</div>`;
});
<!-- language: lang-html -->
<div id="page"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论