twig:拿一个 api 并在 html 中打印它
twig: take an api and print it in html
我正在使用 grav,而对于 twig,我有一个 html 文件,如下所示:
<div class="singlePostContainer">
<span class="postNumber">
01
</span>
<div class="postTextContainer">
<div class="postTitle">
title
</div>
<div class="postDate">
date
</div>
<div class="postAuthor">
author
</div>
</div>
</div>
然后我有这个API:https://5efc440acf235d0016ad72be.mockapi.io/api/floating-point/floating-point
我想从 api 中获取数据(日期、作者等)并将其打印在相应的 html div
中
获取数据并将 JSON
转换为数组,然后将其传递给您的视图,例如
$api_data = json_decode(file_get_contents('https://5efc440acf235d0016ad72be.mockapi.io/api/floating-point/floating-point'), true);
<div class="postTextContainer">
<div class="postTitle">title</div>
<div class="postDate">date</div>
<div class="postAuthor">author</div>
</div>
{% for row in api_data %}
<div class="postTextContainer">
<div class="postTitle">{{ row.title }}</div>
<div class="postDate">{{ row.date }}</div>
<div class="postAuthor">{{ row.author }}</div>
</div>
{% endfor %}
我正在使用 grav,而对于 twig,我有一个 html 文件,如下所示:
<div class="singlePostContainer">
<span class="postNumber">
01
</span>
<div class="postTextContainer">
<div class="postTitle">
title
</div>
<div class="postDate">
date
</div>
<div class="postAuthor">
author
</div>
</div>
</div>
然后我有这个API:https://5efc440acf235d0016ad72be.mockapi.io/api/floating-point/floating-point
我想从 api 中获取数据(日期、作者等)并将其打印在相应的 html div
中获取数据并将 JSON
转换为数组,然后将其传递给您的视图,例如
$api_data = json_decode(file_get_contents('https://5efc440acf235d0016ad72be.mockapi.io/api/floating-point/floating-point'), true);
<div class="postTextContainer">
<div class="postTitle">title</div>
<div class="postDate">date</div>
<div class="postAuthor">author</div>
</div>
{% for row in api_data %}
<div class="postTextContainer">
<div class="postTitle">{{ row.title }}</div>
<div class="postDate">{{ row.date }}</div>
<div class="postAuthor">{{ row.author }}</div>
</div>
{% endfor %}