twigkit / tempo

Tempo is an easy, intuitive JavaScript rendering engine that enables you to craft data templates in pure HTML.
http://tempojs.com/
Apache License 2.0
709 stars 73 forks source link

Traverse Up Nested Templates to Get Value #68

Open difrnt opened 11 years ago

difrnt commented 11 years ago

Here's a working jsfiddle with the JSON and in the current form it works. http://jsfiddle.net/FHZcA/

However is it possible to do something like: {{episodes.episode.name}}

<div id="list">
    <div data-template style="display: none;" data-from-map>Created On: {{value.createDate}}
        <div data-template-for="value.character">
             <h1>{{who.firstName}} {{who.lastName}}</h1>

            <div>
                 <h4>Episodes:</h4>

                <div data-template-for="episodes.episode">{{name}} {{date}}
                    <div data-template-for="details.places">{{episodes.episode.name}} - {{country}}</div>
                </div>
            </div>
            <div>
                 <h4>Ratings:</h4>

                <p data-template-for="ratings.category">{{type}}</p>
            </div>
        </div>
    </div>
    <p data-template-fallback>Sorry, JavaScript required!</p>
</div>
mrolafsson commented 11 years ago

Yeah this should be straightforward using the _parent variable in a nested template. You can see this here: http://tempojs.com/2.0/examples/parentdata.html For some reason it's not working here and I'm not exactly sure why - maybe it's the fact that the nested templates are two levels deep. I will have to investigate this - do you need the extra nesting around episodes > episode -> []?

difrnt commented 11 years ago

@mrolafsson the JSON is a mockup to reflect one that I can't post for security reasons and that I also have no control over the formatting. :-1:

Thanks, I'll take a look at the _parent var.

mrolafsson commented 11 years ago

I understand completely - I'm trying to fix this now!

mrolafsson commented 11 years ago

I think I'm on to something here - reporting back shortly.

difrnt commented 11 years ago

@mrolafsson awesome! Thanks!

mrolafsson commented 11 years ago

I've found the problem - looking for solution - thanks for your patience!

mrolafsson commented 11 years ago

OK I hope I have fixed it - you can see your example here: http://jsfiddle.net/mr_olafsson/Jjwgb/ I altered HTML a little bit for my benefit - but principle is the same (I just prefer lists to divs with children).

Please can you check if this also works in IE? I can't test it here?

difrnt commented 11 years ago

Works great (in Chrome/Firefox) in IE10 and IE9 I receive Sorry, JavaScript required!

mrolafsson commented 11 years ago

Is that possibly just because IE doesn't like the mime type of the RAW from Github? I've tried testing the same file locally and when not using jsFiddle it works like a charm in IE 8 and 9.

mrolafsson commented 11 years ago

Resolved one more corner case - how is it looking your side now?

difrnt commented 11 years ago

Sorry about getting back to you a little later than I expected on this one. But on occasion I seem to receive a mixture of either blank data where it should be populated or the Sorry, JavaScript required! phrase.

mrolafsson commented 11 years ago

Do you see any browser errors? It is a problem that Tempo fails to silently in some cases. I am working on a way around that.

mrolafsson commented 11 years ago

You can now add your own error handler which might help with this:

Tempo.prepare('list').errors(function (err) {
    console.log('Whoa! something happened!');
    console.log(err);
}).render(data);
mrolafsson commented 11 years ago

I take it this is fixed?