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.

Answers


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>

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>

<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


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.


Need Your Help

sudo command not found on Cygwin

windows terminal cygwin sudo

I am currently trying to install and update a few packages on a Windows OS. The only matter is that I am using Cygwin in order to type the commands through a terminal (as I'm aware, I can type in L...

ASP.NET MVC 3 : - Using database instead of resource files as the localization store

asp.net-mvc-3 localization globalization

We have localised strings in the database and would like to know if extending the ASP.NET Resource Provider Model would work with the ASP.NET MVC 3 Razor view engine.