0

I am trying to figure out way to add array elements from *.tpl files in PrestaShop.

What I have is js variable:

var combinations = [];

What I need is to add elements to this array, from *.tpl file. I have available $combinations array of items, arrays, arrays of arrays etc., with keys assigned - so imploding makes no point.

I am trying something like:

{addJsDef combinations[]=$combinations} but it of course won't work.

Since PS documentation is worse than poor, I guess it's just guessing day, but maybe someone has experienced similar problems...

1 Answer 1

1

If I understand you well, if in PHP you assign to Smarty elements like this:

$smarty->assign('combinations', array (1,2,3,20));

you can use in Smarty:

<script>
    var combinations = [];
    {foreach $combinations as $item}
    combinations.push({$item})
    {/foreach}
    console.log(combinations);
</script>

As I added console.log(combinations); to log to JS console, in console there is

Array [ 1, 2, 3, 20 ]

so all elements were inserted into JavaScript array.

In case you have more complex PHP array:

$smarty->assign('combinations', array(
        'a' => 'aval',
        'b' => 'bval',
        'c' => array('c1' => 'c1val', 'c2' => 'c2val'),
        'd' => array(
            'd1' => 'd1val',
            'd2' => array(
                'd2‌​1' => 'd21val',
                'd22'   => 'd22val',
                'd23'   => array('d231', 'd232')
            )
        )
    ));

and you want create flat JavaScript array you may use:

{function jsadd}
    {foreach $data as $item}
        {if not $item|@is_array}
            combinations.push('{$item}')
        {else}
            {jsadd data = $item}
        {/if}
    {/foreach}
{/function}

<script>
    var combinations = [];
    {jsadd data=$combinations}
    console.log(combinations);
</script>

and you will get:

Array [ "aval", "bval", "c1val", "c2val", "d1val", "d21val", "d22val", "d231", "d232" ]

EDIT2

And if you need to create multidimensional array in JavaScript using data from PHP as you explained in comment you can use this Smarty template:

{function jsadd keypart=''}
    {foreach $data as $key => $item}
        {if not $item|@is_array}
            {if $keypart eq ''}
                combinations['{$key}'] = '{$item}'
             {else}
                combinations{$keypart}['{$key}'] = '{$item}'
            {/if}
        {else}
            combinations{$keypart}['{$key}'] = [];
            {jsadd data = $item keypart = "`$keypart`['`$key`']" }
        {/if}
    {/foreach}
{/function}

<script>
    var combinations = [];
    {jsadd data=$combinations}
    console.log(combinations['a']);
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

"I have available $combinations array of items, arrays, arrays of arrays etc." So, lets say in PHP I may use: $smarty->assign('combinations', array('a'=>'aval', 'b'=>'bval', 'c'=>array('c1'=>'c1val','c2'=>'c2val'),'d'=>array('d1'=>'d1val','d2'=>array('d21'=>'d21val','d22'=>'d22val','d23'=>array('d231','d232')))))
@KubaStachu And what should JavaScript array contain for those PHP values? Please edit your question with those sample data and desired JS array output for it.
Unfortunately I don't need to flatten the array, I need to duplicate it's structure for javascript. Actually I am fighting with static variables in prestashop module, but they just seem not working in PrestaShop...
OK, I solved the problem with static vars, which turned out to be problem with php's array_merge(), I'll post solution in a few days when I get whole module to work.
@KubaStachu I've added solution how to create multidimensional array in Javascript from PHP data as you requested. I don't think you should put your solution here because you asked how to create JavaScript array from PHP data in Smarty and not how to do it using other method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.