从 GitHub 页面中的 JSON 文件生成带有预制结构的 HTML 页面。

huangapple go评论64阅读模式
英文:

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

        &lt;div class=&quot;image-container&quot;&gt;
            &lt;img src=&quot;images/placeholder.png&quot; alt=&quot;Image N&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;entry-content&quot;&gt;
            &lt;div class=&quot;entry-title&quot;&gt;EntryName&lt;/div&gt;
            &lt;div class=&quot;entry-rating&quot;&gt;Rating: X&lt;/div&gt;
            &lt;div class=&quot;entry-comment&quot;&gt;Comment: Lorem ipsum&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

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 = `&lt;div class=&quot;image-container&quot;&gt;
        &lt;img src=&quot;{{imgPath}}&quot; alt=&quot;{{imgAlt}}&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;entry-content&quot;&gt;
        &lt;div class=&quot;entry-title&quot;&gt;{{entryTitle}}&lt;/div&gt;
        &lt;div class=&quot;entry-rating&quot;&gt;Rating: {{entryRating}}&lt;/div&gt;
        &lt;div class=&quot;entry-comment&quot;&gt;Comment: {{entryComment}}&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;`;

const data = [
  {
    imgPath:&#39;https://picsum.photos/id/24/200/200&#39;,
    imgAlt:&#39;a picture of a book&#39;,
    entryTitle:&#39;BOOK&#39;,
    entryRating:&#39;4&#39;,
    entryComment:&#39;Nice&#39;
  },
  {
    imgPath:&#39;https://picsum.photos/id/23/200/200&#39;,
    imgAlt:&#39;an image of forks&#39;,
    entryTitle:&#39;FORKS&#39;,
    entryRating:&#39;3&#39;,
    entryComment:&#39;Food!&#39;
  },
  {
    imgPath:&#39;https://picsum.photos/id/2/200/200&#39;,
    imgAlt:&#39;a laptop at a coffee shop&#39;,
    entryTitle:&#39;LAPTOP&#39;,
    entryRating:&#39;5&#39;,
    entryComment:&#39;I like coffee&#39;
  },
];


let htmlString = &#39;&#39;;
data.forEach(d =&gt; {
  let tmplCopy = template;
  Object.keys(d).forEach(k =&gt; {
    const pattern = new RegExp(&#39;{{&#39; + k + &#39;}}&#39;, &#39;g&#39;);
    tmplCopy = tmplCopy.replace(pattern, d[k]);
  });
  
  htmlString += tmplCopy;
});

document.getElementById(&#39;output-container&#39;).innerHTML = htmlString;

<!-- language: lang-html -->

&lt;div id=&quot;output-container&quot;&gt;&lt;/div&gt;

<!-- 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(&quot;#page&quot;);

let entries = [
    { title: &quot;Title1&quot;, rating: &quot;Rating1&quot;, comment: &quot;comment1&quot; },
    { title: &quot;Title2&quot;, rating: &quot;Rating2&quot;, comment: &quot;comment2&quot; },
    // ...
];
//You will have to get the entries array from your json file using JSON.parse()

entries.forEach((entry) =&gt; {
    page.innerHTML += `&lt;div class=&quot;entry-content&quot;&gt;
            &lt;div class=&quot;entry-title&quot;&gt;${entry.title}&lt;/div&gt;
            &lt;div class=&quot;entry-rating&quot;&gt;Rating: ${entry.rating}&lt;/div&gt;
            &lt;div class=&quot;entry-comment&quot;&gt;Comment: ${entry.comment}&lt;/div&gt;
        &lt;/div&gt;`;
});

<!-- language: lang-html -->

&lt;div id=&quot;page&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 22:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定