BorisMoore / jquery-tmpl

The original official jQuery Templates plugin. This project was maintained by the jQuery team as an official jQuery plugin. It is no longer in active development, and has been superseded by JsRender.
3.23k stars 1.01k forks source link

Add "set" tag with syntax {{set(variable) value}} to set variable without affecting template output #128

Closed alekstorm closed 13 years ago

alekstorm commented 13 years ago

This functionality is present in at least the Tornado template engine; I'm sure there are others. This syntax is quite non-standard, but I was hesitant to add a new construct to jQuery Templates myself. Current workaround is ${variable = value, ''}, which requires above-beginner Javascript knowledge to guess.

trans commented 13 years ago

+1

without it some code can get a lot more complex then need be, because you have pre-collect all the data you plan to use up front rather than as the template is being rendered.

drewwells commented 13 years ago

It may be cool to do something like this on the fly inside templates, but I don't believe templates are meant as a replacement for JavaScript code.

Is there an example of a time that it would be more suitable to be setting variables on the fly inside a template rather than organizing your data in a template/html friendly way prior to rendering?

trans commented 13 years ago

Here is small piece of code I am working on:

<!-- Included Modules -->
{{if includes}}
<div id="includes-section" class="section">
  <h3 class="section-header">Included Modules</h3>
  <ul class="link-list">
  {{each(i,x) includes}}
    ${doc = Rebecca.documentation_by_key[x],''}
    <li class="module">
      <a class="include" href="${doc.id}">${doc.name}</a>
    </li>
  {{/each}}
  </ul>
</div>
{{/if}}

<!-- Constants -->
{{if constants}}
<div id="constants-list" class="section">
  <h3 class="section-header">Constants</h3>
  <dl>
  {{each(i,x) constants}}
    ${doc = Rebecca.documentation_by_key[x],''}
    <dt><a name="${doc.id}" href="${doc.id}">${doc.name}</a></dt>
    <dd class="description">${doc.comment}</dd>
  {{/each}}
  </dl>
</div>
{{/if}}

And it goes on like this for a few more sets.

If I had to collect all these doc upfront, it would require building and storing arrays that I don't really need to keep and it would also require that I write a special method to handle it, adding more complexity then is really required.

I certainly do not recommend that anyone put hefty chunks of logic in their templates, but for a temporary variable, being able to do this is invaluable.

drewwells commented 13 years ago

In this example, I personally would remove the duplicated code in template and pass along a title field. That way the first two lines of the template will use the same code. Then you can branch the ul, dl stuff with the following code. I try to setup up my templates to expand an array and remove the {{each}} too, but it's not always possible.

For instance,

//var data = { lots, of, stuff };
data.name = data.includes? 'includes' : 'constants';
data.title = data.includes ? 'Includes' : 'Constants';
data.arr = $.map( /* same named array, or data[ name ] */, function( n ){
    return Rebecca.documentation_by_key[ n ];
});

Now for the the minor HTML things that are repeated/slightly different. You could use data.name or use data.title. Throwing a lot of ifs in there makes templates look very ugly, so your preferred solution may be to do {{if name === 'includes'}} and {{ else }} and continue to use the repeated code.

My two cents,

rdworth commented 13 years ago

Thanks for taking the time to submit this pull request. Unfortunately this plugin is no longer being actively developed or maintained by the jQuery team. See README for more info.

drewwells commented 13 years ago

Hmm... that was 6 months ago. We are heavily depending on this plugin, that is quite unfortunate. If I had known it was DOA in April, I wouldn't have touched this thing.

rdworth commented 13 years ago

Knowing that you can invest in and depend on a plugin that is well supported by the jQuery Team is precisely the reason we felt this change was necessary (that was unfortunately not the case with jquery-tmpl as no sub-team within the jQuery project took ownership of it). I'm sorry we took as long as we did to make the decision final and communicate it more widely.

trans commented 13 years ago

So now how/where does one submit this issue/patch?

rdworth commented 13 years ago

Not here and if there is another place today, I don't know of it. This repo is no longer accepting or reviewing issues/patches. Any of the 166 current forks or a future fork could take that on. I don't know if any has such an interest. If one does, I'd be happy to point people to it from this repo.

BorisMoore commented 13 years ago

Hi Steven,

See this post for a jQuery conference where I spoke of the roadmap for jQuery Templates, and JsViews, as I see it.

http://www.borismoore.com/2011/10/jsviews-presentation-at-jquery.html

I hope to post specifically on the roadmap aspect, probably tomorrow.

The short answer is that I won't be working on jquery.tmpl, as such, given jQuery UI's ownerships and plans. But my work on templates is by no means 'dying', but is moving forward with JsRender and JsViews...

I hope also to be able to change the readme on jquery.tmpl to something providing a bit more context...

Boris

-----Original Message----- From: Steven Black [mailto:reply@reply.github.com] Sent: Saturday, October 08, 2011 4:22 PM To: Boris Rivers-Moore (MOORE) Subject: Re: [jquery-tmpl] Add "set" tag with syntax {{set(variable) value}} to set variable without affecting template output (#128)

@BorisMoore are you still maintaining jquery-tmpl at https://github.com/BorisMoore/jquery-tmpl or is that dead as well?

Reply to this email directly or view it on GitHub: https://github.com/jquery/jquery-tmpl/pull/128#issuecomment-2333666