BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.68k stars 340 forks source link

fail to compile template {for} #112

Closed fieder closed 12 years ago

fieder commented 12 years ago

Hi Boris,

The error I get is on line 455, markup.replace() is not a function. Following the code I saw in tmplFn(), line 479 : markup = node[5]. For some reason that node does not contain the markup string but instead a number -the second time it gets into the for loop 466- and that's why markup.replace() fails.

The json structure is very similar to the example of Films you have in the wiki. I'm using jquery-mobile and below's my example

<div id="eventsContent" data-role='content'></div>
<script id="eventsListTemplate" type="text/x-jsrender">         
            <ul data-role="listview">
                {{for results}}
                <li class="eventItem">
                    <img src={{:flyerURLSmall}} />
                    <h3><a href={{:flyerURLLarge}}>{{:name}}</a></h3>

                    <p><strong>{{:place.name}}</strong></p>

                    <div class="ui-li-aside">
                        <p><strong>{{:checkins}} checkins</strong></p>
                    </div>

                </li>
                {{for}}
            </ul>
        </script>
//this is the call to fill the template out with the content of my json object which contains several nested objects in 'results'
  $( "#eventsContent" ).html($("#eventsListTemplate").render( events ));
//where events is the complex json similar to the movies example.

Any idea what coud be wrong? I debugged and double checked all things I can think of. 
Cheers.
Felipe
BorisMoore commented 12 years ago

The closing {{for}} should be {{/for}}

waratah commented 12 years ago

The same basic issue markup.replace is not a function line 461 on zip downloaded today.

The replace function is hitting a pure number and therefore replace is not defined and the template crashed.

BorisMoore commented 12 years ago

As in the case above, that is likely because you made a mistake matching tags. Check that you have {{foo...}} ... {{/foo}} for any block tags (such as if or for), and {{foo ... tmpl=... /}} for tags using external templates. Also that your HTML is well-formed... If necessary, build up your template step by step to find which construct is leading to the error...

waratah commented 12 years ago

A few strange things happened number disappeared in html.  the html was poorly formed as you suggested.

i placed a guard condition on the logic. To get further if(markup.replace) in two places.

I found i was using {{name}} without the : and the whole chunk disappeared.

I also tried a helper for formatting numbers. Could not get that right ended up formatting input rather than work it out.

Is it worth writing a separate Verification of the the template?  for developer usability?

Ken Foskey

-------- Original message -------- Subject: Re: [jsrender] fail to compile template {for} (#112) From: Boris Moore reply@reply.github.com To: waratah kfoskey@tpg.com.au CC:

As in the case above, that is likely because you made a mistake matching tags. Check that you have {{foo...}} ... {{/foo}} for any block tags (such as if or for), and {{foo ... tmpl=... /}} for tags using external templates. Also that your HTML is well-formed...


Reply to this email directly or view it on GitHub: https://github.com/BorisMoore/jsrender/issues/112#issuecomment-5877956

BorisMoore commented 12 years ago

Yes at some point both better error handling and some template validation tooling would be very valuable. But the priority is first to get the basic design and features established, and the primary scenarios working. Then we will have an official beta or beta2. Full documentation will be the next thing. Improved error handling after that. And additional developer tools probably after reaching V1.0. So I'm afraid some patience will be required as we move towards those goals... :)