jsselect scope question

4 views
Skip to first unread message

Alexis

unread,
Dec 9, 2008, 10:27:58 PM12/9/08
to google-jstemplate
Hi,

I'm looking for a way to access array's length from inside the scope
of jsselect

For example, I would like to get

<div>
<span>a of 2</span>
<span>b of 2</span>
</div>

from

{
testArray: ["a", "b"]
}

where "2" is the length of the array.


Because jsselect is evaluated before jsvars, you'd need to set jsvars
on a parent element, and jsselect on the child element. Like so:

<div id="t" jsvars="$len:testArray.length;">
<div jsselect="testArray">
<span jscontent="$this + ' of ' + $len"></span>
</div>
</div>

This, of course produces something like
<div>
<div><span>a of 2</span></div>
<div><span>b of 2</span></div>
</div>


Is there a way to set variables and select the scope using a single
element?
Any other ideas on how to get the desired output?

Thanks,
Alexis

mesch

unread,
Dec 9, 2008, 10:54:30 PM12/9/08
to google-jstemplate
> Because jsselect is evaluated before jsvars, you'd need to set jsvars
> on a parent element, and jsselect on the child element. Like so:
>
>       <div id="t" jsvars="$len:testArray.length;">
>         <div jsselect="testArray">
>           <span jscontent="$this + ' of  ' + $len"></span>
>         </div>
>       </div>
>
> This, of course produces something like
> <div>
>           <div><span>a of 2</span></div>
>           <div><span>b of 2</span></div>
> </div>
>
> Is there a way to set variables and select the scope using a single
> element?
> Any other ideas on how to get the desired output?

That's the way to do what you want. Slightly better style would be so:

<div jsselect="testArray">
<span jscontent="$this">a</span> of <span
jscontent="$len">2</span>
</div>

Alexis

unread,
Dec 10, 2008, 8:58:56 AM12/10/08
to google-jstemplate
It makes sense to separate content and length into two spans - thanks.
With that in mind, the desirable output is

<div>
<span>a</span> of <span>2</span>
<span>b</span> of </span>2</span>
</div>

Hoewever, some element still needs to define that "$len" variable,
right? If an outher div is used for this, then the template would
looks like this:


<div id="t" jsvars="$len:testArray.length">
<div jsselect="testArray">
<span jscontent="$this"></span> of <span jscontent="$len"></
span>
</div>
</div>

but that would still produce an extra wrapping div as in:

<div>
<div><span>a</span> of <span>2</span></div>
<div><span>b</span> of </span>2</span></div>
</div>


The issue would be solved if there was a way to define variables
before jsselect is evaluated, on *the same element*. Something like
<div jsvars_evaludated_before_jsselect="$len:$this.testArray.length"
jsselect="testArray">...


Alexis

mesch

unread,
Dec 10, 2008, 9:40:20 AM12/10/08
to google-jstemplate
> Hoewever, some element still needs to define that "$len" variable,

Good point. Yes, that's how it is. If there really is no outer element
that lends itself to it, then it may be acceptable to just pass in the
length as global variable from the processing code.

Alexis

unread,
Dec 10, 2008, 9:57:54 AM12/10/08
to google-jstemplate
Yes, passing a variable from the processing code is an option I'm
using right now.

There's a couple of drawbacks to doing it this way. Processing code
becomes tied to the logic of the template, thus eroding the benefits
of using jstemplate. In case is a template has nested loops this
approach is simply not applicable since the variables would need to be
defined inside of a loop.

Any advice if I were to try to add a new keyword similar to jsvars,
but with different order of evaluation?

Alexis
Reply all
Reply to author
Forward
0 new messages