Asked  7 Months ago    Answers:  5   Viewed   28 times

In Smarty, is there a standard function or an easy way to generate json from an array, as json_encode() does in php?

I could not see it in Smarty documentation, wanted to ask here.

 Answers

72

This should work. The @ makes smarty run the modifier against the whole array, otherwise it does it for each element.

{$myarray|@json_encode}

If $escape_html is enabled, you will need to use nofilter:

{$myarray|@json_encode nofilter}
Wednesday, March 31, 2021
 
macha
answered 7 Months ago
26

JSON string is just string. To access its members you have to create array/object from this string:

{foreach from=$items item=entry}
  {* create array from JSON string*}
  {assign var=person value=$entry->nb_persons|json_decode:1}
  <pre>
    {$person.company}
  </pre>
{/foreach}
Wednesday, March 31, 2021
 
subroutines
answered 7 Months ago
22

You have 2 possible solutions.

First solution

in PHP you use:

$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);

In Smarty you use:

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

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

Second solution

In PHP you use:

$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));

In Smarty file:

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

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

Wednesday, March 31, 2021
 
jsuissa
answered 7 Months ago
17

Recently I got to work with Yii used with Smarty and gettext. but poedit not support the syntax of smarty.

https://github.com/Garefild/PoeditSmarty

And here comes the use of PoeditSmarty allowed Poedit recognize the syntax of Smarty.

{t}Text to be localized{/t}
{"Text to be localized"|_} 
{_("Text to be localized")}

Very simple to use, PoeditSmarty creates a "pot" file which Poedit know to read.

For example, I have a file "Header.tpl"

        <form class="login" method="post" id="login">
        <h2>{__('login')}</h2>
        <div class="wrap">
            <input type="text" autofocus="" placeholder="{__('Username')}" class="form-control" name="username">
            <br>
            <input type="password" placeholder="{__('Password')}" class="form-control" name="password">
            <div class="captcha">
                <img alt="" src="" id="codeImage">
                <label class="checkbox">
                    <span class="pull-right">
                        <a href="#" id="code" class="forgot">{__('Get a new code')}</a>
                    </span>
                </label>
                <input type="text" placeholder="{__('verify Code')}" class="form-control" name="verifyCode">
            </div>
            <button type="submit" class="btn login-btn" name="submitLogin">
                <i class="fa fa-lock"></i>
                {__('Log me in')}
            </button>
            <hr>
            <div class="password">
                <p>{__('Do you not remember your password')} ?</p>
                <a data-toggle="modal" class="forgot">{__('Password Recovery')}</a>
            </div>
            <hr>
            <h3 class="logo">{__('siteName')}</h3>
        </div>
    </form> 

I'll run the PoeditSmarty via cmd

java -jar "C:Program FilesPoeditGettextToolsbinPoeditSmarty.jar" -o C:UsersMikeDesktopxxxtempPot.pot -c UTF-8 -k __ -f header.tpl

The result of pot file

    # Garefild2@gmail.com.
    # Garefild <Garefild2@gmail.com>, 2015-2020.
    #

    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSIONn"
    "Report-Msgid-Bugs-To: n"
    "POT-Creation-Date: 2015-12-29 16:47n"
    "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONEn"
    "Last-Translator: FULL NAME <Garefild2@gmail.com>n"
    "Language-Team: LANGUAGE <Garefild2@gmail.com>n"
    "Language: n"
    "MIME-Version: 1.0n"
    "Content-Type: text/plain; charset=CHARSETn"
    "Content-Transfer-Encoding: 8bitn"

    #: header.tpl:48
    msgid "Password Recovery"
    msgstr ""

    #: header.tpl:36
    msgid "Get a new code"
    msgstr ""

    #: header.tpl:29
    msgid "Username"
    msgstr ""

    #: header.tpl:39
    msgid "verify Code"
    msgstr ""

    #: header.tpl:51
    msgid "siteName"
    msgstr ""

    #: header.tpl:27
    msgid "login"
    msgstr ""

    #: header.tpl:47
    msgid "Do you not remember your password"
    msgstr ""

    #: header.tpl:31
    msgid "Password"
    msgstr ""

    #: header.tpl:43
    msgid "Log me in"
    msgstr ""

via Poedit

enter image description here

Saturday, May 29, 2021
 
Claudio
answered 5 Months ago
76

You could modify your PHP code to ensure that $logo is set, e.g:

function hookFooter($params)
{
    global $smarty;
    $smarty->assign('ENT_QUOTES', ENT_QUOTES);
    if( file_exists('modules/ebbrandingfooter/logo-footer.jpg')){
        $smarty->assign('logo','modules/ebbrandingfooter/logo-footer.jpg');
    } else {
        $smarty->assign('logo', null);
    }
    $FOOTERdescription=Configuration::get('FOOTER_DESC');
    $smarty->assign('description',$FOOTERdescription );
    return $this->display(__FILE__, 'ebbrandingfooter.tpl');
}

Also note that after a } you don't need a semicolon.

Saturday, May 29, 2021
 
Optimus
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :