Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Using a smarty array in javascript

I am trying to use smarty in javascript.

Here is my complete code of the .php and .tpl :

.Php

<?php

$_CRUMBS->Add("User false logs", "/users/user false logs/");


$ufl = $_DB->queryRaw("SELECT `user_id` ,`firstname`,`lastname` FROM `employees`");

while ($row = $ufl->next_assoc()) {
$results[] = $row;
}

$smarty->assign("ufl",$results);
$smarty->TDisplay("users/backend_users.tpl", "MDPI Backend | Backend Users", "general-content.tpl");

?>

.tpl

<h1>Create Backend Users</h1>



<script>
$(function() {
    var availableTags = [
        {foreach from=$ufl item=uflItem}
            <tr>
                <td>{$uflItem['firstname']}</td>
            </tr>
    {/foreach}
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});
</script>


<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>

</div><!-- End demo -->

But I have errors there. I have tryed wit literal but nothing changed. Thank you.

Upvotes: 0

Views: 10011

Answers (4)

bershika
bershika

Reputation: 1000

I suggest using json_encode, like this:

$names = array();
foreach($result in $results)
    $names = $result['firstname']
$smarty->assign("names",json_encode($names));

and then later in js:

<script>
$(function(){
    var availableTags = {/literal}{$names}{literal}; //array is pulled out from smarty
    $("#tags" ).autocomplete({
        source: availableTags
    });
});

</script>

Upvotes: 2

Eugene
Eugene

Reputation: 3375

your code looks fine, the only problem that I see without running it is in javascript. Try something like this:

<script>
$(function() {
    var availableTags = [
        {foreach from=$ufl key=index item=uflItem}
            '<tr>\
                <td>{$uflItem['firstname']}</td>\
            </tr>'{if isset($ufl[$index+1])},{/if}
    {/foreach}
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});
</script>

By the way, since Smarty 3 {literal} tag is not required.

Upvotes: 0

Aleks G
Aleks G

Reputation: 57316

You definitely will have errors here, as you're using { and }, which are reserved characters in smarty. Change them to {ldelim} and {rdelim}. Also, dereferencing an array in smarty has different syntax: one dot. I'm not too sure why you're putting <tr> and <td> around the items, but you know better. You may have further troubles because your autocomplete options have line-breaks in them, so use {strip} to solve this.

Try this code instead:

<script>
$(function() {ldelim}
    var availableTags = [
        {foreach from=$ufl item=uflItem name=uflloop}{strip}
            '<tr>
                <td>{$uflItem.firstname|escape:'quotes'}</td>
            </tr>'
            {if !$smarty.foreach.uflloop.last},{/if}
    {/strip}{/foreach}
    ];
    $( "#tags" ).autocomplete({ldelim}
        source: availableTags
    {rdelim});
{rdelim});
</script>

Upvotes: 2

EaterOfCode
EaterOfCode

Reputation: 2222

<h1>Create Backend Users</h1>



<script>{literal}
$(function() {
    var availableTags = [
        {/literal}{foreach name=things from=$ufl item=uflItem}
            "{$uflItem['firstname']}"{if $smarty.foreach.things.last != true} ,{/if}
    {/foreach}{literal}
    ];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});{/literal}
</script>


<div class="demo">
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>

you have to put literal around the curly's of js otherwise smarty is going to die

Upvotes: 1

Related Questions