【问题标题】:twig: take an api and print it in htmltwig:获取一个 api 并在 html 中打印它
【发布时间】:2020-07-03 14:40:03
【问题描述】:

我正在使用 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 中打印出来

【问题讨论】:

    标签: twig grav


    【解决方案1】:

    获取数据并将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 %}
    

    【讨论】:

    • 我如何循环内容?语法是什么?
    • 它不起作用,我必须包含包含 API 但我不知道如何使用 twig 的文件
    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多