gsf / whiskers.js

Whiskers templating library for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Partial by reference or extended if

gsf opened this issue · comments

---------- Forwarded message ----------
From: Dmitry Kolesnikov
Date: Wed, Feb 8, 2012 at 5:29 PM
Subject: Re: whiskers.js
To: Gabriel Farrell

Hello,

Sorry to be a silent for a while. I've been playing with whiskers and trying to fit it into my use-cases.
So, the major focus on client-side templates. I need render UI in multiple passes due to mash-up requirements. In the way a data-context becomes broken into pieces. Besides, partial-by-reference I had idea to associate a closure with object property and partial. It worked pretty cool but unfortunately closures violates a pure data approach (used by me JSON comes from dummy storage). All tweaks about data mash-up are done by application code outside of whiskers.

In this respect, I am almost agree with you that current status of library is self sufficient and no more features are needed. However, I hit only one use-case where either partial-by-reference or sophisticate {if a == b} statement is required. This is a render of atom feed where each entry requires  dedicated template. As an example, you data is following

{"title":"myfeed", "entry" : [{type:"a"}, {type:"b"}...]}

then, I'd like to have a template in a format either
{for e in entry}
  {if e.type == "a"}

this is type of a
{/if}
  {if e.type == "b"}
this is type of b
{/if}
{/for}

or
{for e in entry}
  {$e.type} <- partial by ref
{/for}

I would prefer an first option, it is more clear for developers! What is you opinion? Should we push this feature? If yes, I can submit you a patch.

Best Regards,
Dmitry

On Jan 26, 2012, at 9:41 PM, Gabriel Farrell wrote:

Great! I'm interested in the alternative you found as well.

On Thu, Jan 26, 2012 at 2:36 PM, Dmitry Kolesnikov wrote:

Hello,

Sure I'll create it. However, I found some alternative to partial-by-reference.
Please give me couple of days, I need to think more and I'll came back to you.

  • Dmitry

On Jan 25, 2012, at 10:41 PM, Gabriel Farrell wrote:

Would you mind creating an issue for this at
https://github.com/gsf/whiskers.js/issues? It will be easier for me to
keep track of there, and I'd like to get comments from others.

On Tue, Jan 24, 2012 at 5:59 PM, Gabriel Farrell wrote:

Glad you like whiskers.js! The partial-by-reference tag is an
interesting idea. As you can see, I'm trying to keep the Whiskers
feature set as minimal as possible, so I'll have to think about it.
Let me play with it and test it in some templates, then I'll get back
to you.

On Tue, Jan 24, 2012 at 4:48 PM, Dmitry Kolesnikov wrote:

Hello Gabriel,

I found you whiskers.js library pretty cool =) Very light & powerful to compare with others.
Honestly, I am going to use it in my projects.

I was missing one feature there: a partial by reference. I have made a patch to it and exposed this feature via tag {$partial}. I hope you would like the feature and bring it into mainstream.

As an example, template is following:

{for e in list}

{$e.myref}

{/for}

partials are

{
  p1: '{title}',
  p2: '{title}'
}

with the following context
{
  list: [
     {
        myref: 'p1',
        title: 'p1 in use!'
     },
     {
        myref: 'p2',
        title: 'p2 in use!'
     }
  ]

the result is

p1 in use
p2 in use
  • Dmitry

I'm not in favor of extending the "if" tag. Comparisons of any kind should be done in the context preparation. The template "if" only tests for true/false.

As for the partial by reference, I have been playing with the idea of allowing string literals in the partial tag, so one could do {>"some-partial.html"}, but I'd really have to extend the big RegExp to catch it. Another possibility is that partial tags could first attempt a lookup in the passed-in partial object, and if no match is found, then do a lookup in the context, and if a a matching key is found there then look up the value of that key in the partial object. That takes a few more words to explain than I would like, however, and could lead to some really unexpected results if one isn't aware of the behavior.

Oops, forgot to close this. As of 32da3b7, partials are just included in the context object.