如何打印在Smarty的一个JSON [英] How to print a json in Smarty

查看:621
本文介绍了如何打印在Smarty的一个JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自一个API,如何打印使用的Smarty的JSON得到一个JSON。

Am getting a json from an api, how to print the json using Smarty.

JSON格式

[
    {
        "first_name": "jinu",
        "last_name": "mk",
        "loginid": "jinu@amt.in",
        "timezone": "5.5",
        "team_id": "c964ef415f157ddd99173f5b481ee1e3",
        "user_type": 1,
        "last_login_date": null
    },
    {
        "first_name": "avatar second",
        "last_name": "test",
        "loginid": "avatar2@gmail.com",
        "timezone": "5.5",
        "team_id": "ec40f5feda8643135bc20be44f897b03",
        "user_type": "3",
        "last_login_date": null
    },
    {
        "first_name": "avatar testing admin",
        "last_name": "amt 1",
        "loginid": "avatar@amt.in",
        "timezone": "5.5",
        "team_id": "ec40f5feda8643135bc20be44f897b03",
        "user_type": 1,
        "last_login_date": null
    }
]

我已经厌倦了下面的foreach,但它不是任何印刷

I have tired the following foreach but its not printing anything.

{foreach from=$games item=foo}
     <li>{$foo.first_name}</li>
{/foreach}

请帮我解决这个问题。谢谢

Please help me to solve this issue. Thanks

推荐答案

您有两个可能的解决方案。

You have 2 possible solutions.

一解

在PHP中使用:

$data = '[
    {
        "first_name": "jinu",
        "last_name": "mk",
        "loginid": "jinu@amt.in",
        "timezone": "5.5",
        "team_id": "c964ef415f157ddd99173f5b481ee1e3",
        "user_type": 1,
        "last_login_date": null
    },
    {
        "first_name": "avatar second",
        "last_name": "test",
        "loginid": "avatar2@gmail.com",
        "timezone": "5.5",
        "team_id": "ec40f5feda8643135bc20be44f897b03",
        "user_type": "3",
        "last_login_date": null
    },
    {
        "first_name": "avatar testing admin",
        "last_name": "amt 1",
        "loginid": "avatar@amt.in",
        "timezone": "5.5",
        "team_id": "ec40f5feda8643135bc20be44f897b03",
        "user_type": 1,
        "last_login_date": null
    }
]';


$smarty->assign('games',$data);

在Smarty的使用:

In Smarty you use:

{foreach from=$games|json_decode item=foo}
     <li>{$foo->first_name}</li>
{/foreach}

但是我不知道在这种情况下,如果 json_de code 是在 $游戏运行刚一次或在每次调用。

However I'm not sure in this case if json_decode is run on $games just once or on each invocation.

第二类解决方案

在PHP中使用:

$data = '[
    {
        "first_name": "jinu",
        "last_name": "mk",
        "loginid": "jinu@amt.in",
        "timezone": "5.5",
        "team_id": "c964ef415f157ddd99173f5b481ee1e3",
        "user_type": 1,
        "last_login_date": null
    },
    {
        "first_name": "avatar second",
        "last_name": "test",
        "loginid": "avatar2@gmail.com",
        "timezone": "5.5",
        "team_id": "ec40f5feda8643135bc20be44f897b03",
        "user_type": "3",
        "last_login_date": null
    },
    {
        "first_name": "avatar testing admin",
        "last_name": "amt 1",
        "loginid": "avatar@amt.in",
        "timezone": "5.5",
        "team_id": "ec40f5feda8643135bc20be44f897b03",
        "user_type": 1,
        "last_login_date": null
    }
]';



$smarty->assign('games',json_decode($data));

在Smarty的文件:

In Smarty file:

{foreach from=$games item=foo}
     <li>{$foo->first_name}</li>
{/foreach}

我总是建议使用第二种方法,因为如果可能的Smarty的,你应该避免使用任何计算,只显示数据。

I always recommend using second method, because if possible in Smarty you should avoid using any calculations and just display data.

这篇关于如何打印在Smarty的一个JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆