javascript "less than" in a genshi template?

347 views
Skip to first unread message

Chris Curvey

unread,
Dec 1, 2007, 3:24:07 PM12/1/07
to TurboGears
I'm adding some inline javascript in a Genshi template, but the engine
seems to be confusing the less-than sign for a tag. So this code
breaks:

<script type="text/javascript">
var foo = 'abcde';
for (var i = 0; i < foo.length; i++ ) {
// do something here
}
</script>

There has to be a workaround do this....

Paul Johnston

unread,
Dec 1, 2007, 3:48:09 PM12/1/07
to turbo...@googlegroups.com
Hi,

>for (var i = 0; i < foo.length; i++ ) {
>
>

for (var i = 0; i &lt; foo.length; i++ ) {

We really should put this somewhere more prominent in the docs!

Paul

Matías Iturburu

unread,
Dec 1, 2007, 3:49:52 PM12/1/07
to turbo...@googlegroups.com
Just wondering. Using [!CDATA] won't do the work?

2007/12/1, Paul Johnston <p...@pajhome.org.uk>:

iain duncan

unread,
Dec 2, 2007, 3:51:32 AM12/2/07
to turbo...@googlegroups.com

+1 on that, took me ages to notice I could do that. When we do, we
should also put in a note that the same thing is required in python
statements. ( At least it is in kid! )

ie
<div py:if="foo &lt; bar">

Iain

Chris Curvey

unread,
Dec 2, 2007, 8:03:12 PM12/2/07
to TurboGears
Well, that keeps the template from blowing up, but it passes the &lt;
right through. So I end up with

for (var i = 0; i &lt; foo.length; i++)

And (understandably), the javascript interpreter has a leeeetle
problem with that.

Paul Johnston

unread,
Dec 3, 2007, 1:08:45 PM12/3/07
to turbo...@googlegroups.com
Hi,

>Well, that keeps the template from blowing up, but it passes the &lt;
>right through. So I end up with
>for (var i = 0; i &lt; foo.length; i++)
>And (understandably), the javascript interpreter has a leeeetle
>problem with that.
>
>

Ah, you're right. Sorry, giving out duff advice here. Looking back, I
realise I've only done this before in Genshi py:for blocks, not in JS.
Well, the quick hack is to use >= instead, and swap the operands. Some
other ideas:

Put this in a static .js file
I wonder if Genshi has an equivalent to XSLT's disable-output-escaping?
Someone mentioned CDATA.

Good luck,

Paul

Diez B. Roggisch

unread,
Dec 3, 2007, 6:23:55 PM12/3/07
to turbo...@googlegroups.com
Paul Johnston schrieb:

The advice was sound. The problem is that escaping of html entities in
HTML only works for XHTML - otherwise, the browser will be in some mixed
parsing mode.

Unfortunately, the reliance of genshi/KID on pure XML even if they
_produce_ HTML will not allow for an easy solution. Except for switching
to 100% XHTML of course.


Diez

Chris Curvey

unread,
Dec 4, 2007, 6:44:23 AM12/4/07
to TurboGears
>
> Ah, you're right. Sorry, giving out duff advice here. Looking back, I
> realise I've only done this before in Genshi py:for blocks, not in JS.
> Well, the quick hack is to use >= instead, and swap the operands. Some
> other ideas:

Nope, Genshi turns ">=" into "&gt;=". I guess I should file an
enhancement request

>
> Put this in a static .js file

This solution did work. I had to pass everything into the function at
each call, rather than relying on text substitution. So the function
went from

function showLetters(showThisLetter) {
for (i = 0; i < '${usedLetters}'.length; i++) {
var letter = '$[letters}'.charAt(i);
if (letter == showThisLetter) {
makeVisible(letter)
} else {
makeVisible(letter)
}
}
}

to this:

function showLetters(showThisLetter, allLetters) {
for (i = 0; i < allLetters.length; i++) {
var letter = allLetters.charAt(i);
if (letter == showThisLetter) {
makeVisible(letter)
} else {
makeInvisible(letter)
}
}
}

And you know what? I like that newer javascript even better. (Not so
much switching-back-and-forth between JS and Genshi.)

> I wonder if Genshi has an equivalent to XSLT's disable-output-escaping?
> Someone mentioned CDATA.

I tried a few flavors, but could not get it to work.

Thanks to everyone for all the help!
>
> Good luck,
>
> Paul
Reply all
Reply to author
Forward
0 new messages