0

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.

3
  • 1
    Smarty is not for javascript. Smarty is for PHP. Don't mix concerns :( Commented Jun 11, 2012 at 7:28
  • items in array should be separated by commas and wrapped in quotes, since they are strings. Commented Jun 11, 2012 at 7:30
  • I am a novice in PHP, smarty and Javascript developping. My goal here is o create an autocomplete box with an smarty array named $ufl. The vorking example is taken form jqueryui.com/demos/autocomplete but my goal is to usee the smarty variable instead of static array. Commented Jun 11, 2012 at 7:34

4 Answers 4

2

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you Aleks, may be I forgot to mention that I am trying to use this in a .tpl file. When I am trying your code, I am getting also some errors like Multiple annotations found at this line: - Missing semicolon - Syntax error on tokens, delete these
@Ana Well, that would be caused by javascript syntax errors in your original code. I updated my answer to (hopefully) fix those.
thank you again, I updated my question with the entire code. I tryed your edited code but still the same errors, I don't know wyy :-(
Could may be help: the {ldelim} and {rdelim} are not in purple color when inside of the script but they are when outside. May be that's the reason why I hgave those errors.
Thank you for your help, I solved the problem by using quotes.
|
2

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>

Comments

1
<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

12 Comments

Thank you EaterOfCorpses, As I said to Alex, I am trying to use this code in a .tpl file and When I am trying to use your example, I am getting some errors.
Multiple annotations found at this line: - Syntax error on tokens, delete these tokens
It's like there is something missing and the code is not recogniwed as is.
and if you replace $uflItem['firstname'] by $uflItem.firstname ?
Yes, the smarty part shuld be right. I updated my question with the complete code.
|
0

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.

1 Comment

Thank you, I am using one of the earlyest smarty.

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.