How to "translate" SMARTY to JAVASCRIT or even JQUERY?

Asked

Viewed 108 times

3

I have this code in a "Smarty template", (which runs on a server Fuelphp), I simply wish to "rewrite" this in Javascript or Jquery:

<select size="1" name="dw01">
  {section name=item start=1 loop=$item+1 step=1}
    {if $item_push}
      {foreach from=$item_push item=val}
        <option value="{$smarty.section.item.iteration}">
          {assign var="name" value="item_name`$smarty.section.item.iteration`"}
          {$val.$name|default:''}
        </option>
      {/foreach}
    {/if}
  {/section}
</select>

1 answer

1


Smarty is a PHP templating engine, that is, it helps you generate HTML quickly and dynamically as in the case of your example, where Smarty is generating a select form dynamically.

To do the same thing with Javascript or Jquery there are several ways.

Pulling from an object, which can be a JSON generated with PHP, using Javascript only:

var options = '',
    //essa é a lista de onde sairão os valores das opções do select
    movie_list = {
      'The Revenant'       : 'The Revenant',
      'Deadpool'           : 'Deadpool',
      'Fight Club'         : 'Fight Club',
      "Birdman"            : 'Birdman',
      'Dallas Buyer\'s Club': 'Dallas Buyer\'s Club'
    };
//itera pela lista pra gerar as opções do select
for(var key in movie_list) {
    options += '<option value="' + key  + '">' + movie_list[key] + '</option>'
}
//inclui as opções no select de id movies
document.getElementById('movies').innerHTML = options;
<select id="movies"></select>

JSFIDDLE

Pulling from an object, which can be a JSON generated with PHP, using Jquery:

var options = '',
    movie_list = {
      'The Revenant'       : 'The Revenant',
      'Deadpool'           : 'Deadpool',
      'Fight Club'         : 'Fight Club',
      "Birdman"            : 'Birdman',
      'Dallas Buyer\'s Club': 'Dallas Buyer\'s Club'
    };
   
for(var val in movie_list) {
    $('<option />', {value: val, text: movie_list[val]}).appendTo($('#movies'));
}
<select id="movies"></select>

JSFIDDLE 2

There are many more possibilities, it depends a lot on what you want to do and how you want to do it.

  • 1

    SENSATIONAL... with the examples already in codes it is easy for us to understand! I’ll be able to study that now.. Thank you very much!

  • No problem! Good luck in your studies!!!

  • Now.. moving on.. I have one more question, for example on this page .. this SMARTY code, working with an object ( with the name "$item_push"). further down in the part of < scripts > am putting this java code (which you gave as example...) however with the object "$item_push"... but the error... Then the question would be... how to use the "$item_push" object in the java part? or .... how do I know if I can use the same object that was in SMARTY, la in the part of < scripts > ? with JS ??

  • 1

    The answer is, you can’t! Javascript is used to manipulate the Document Object Model (DOM), creating, changing behavior, passing new attributes, modifying existing attributes, etc., acting on the front end (client side or client side). PHP (Smarty) acts together with HTML, but acts on the back end (server side or llado server), so you can use Smarty to generate a list and capture that list with Javascript to generate select.

  • 1

    Here are some examples of the Smarty forum: http://www.smarty.net/forums/viewtopic.php?p=87452&sid=bbb70837243b574dc88c07f38d9fed81

  • got it! and already adapted my need! thanks, including the extra links!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.