1 if(!dojo._hasResource["dojox.dtl.tests.text.tag"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.dtl.tests.text.tag"] = true;
3 dojo.provide("dojox.dtl.tests.text.tag");
5 dojo.require("dojox.dtl");
6 dojo.require("dojox.dtl.Context");
8 doh.register("dojox.dtl.text.tag",
10 function test_tag_block_and_extends(t){
13 // Simple (messy) string-based extension
14 var template = new dd.Template('{% extends "../../dojox/dtl/tests/templates/pocket.html" %}{% block pocket %}Simple{% endblock %}');
15 t.is("Simple Pocket", template.render());
17 // Variable replacement
18 var context = new dd.Context({
19 parent: "../../dojox/dtl/tests/templates/pocket.html"
21 template = new dd.Template('{% extends parent %}{% block pocket %}Variabled{% endblock %}');
22 t.is("Variabled Pocket", template.render(context));
24 // Nicer dojo.moduleUrl and variable based extension
25 context.parent = dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html");
26 template = new dd.Template('{% extends parent %}{% block pocket %}Slightly More Advanced{% endblock %}');
27 t.is("Slightly More Advanced Pocket", template.render(context));
29 // dojo.moduleUrl with support for more variables.
30 // This is important for HTML templates where the "shared" flag will be important.
32 url: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html")
34 template = new dd.Template('{% extends parent %}{% block pocket %}Super{% endblock %}');
35 t.is("Super Pocket", template.render(context));
37 function test_tag_block(t){
40 var context = new dd.Context({
41 parent: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket2.html"),
42 items: ["apple", "banana", "lemon" ]
45 var template = new dd.Template("{% extends parent %}{% block pocket %}My {{ item }}{% endblock %}");
46 t.is("(My apple) (My banana) (My lemon) Pocket", template.render(context));
48 function test_tag_comment(t){
51 var template = new dd.Template('Hot{% comment %}<strong>Make me disappear</strong>{% endcomment %} Pocket');
52 t.is("Hot Pocket", template.render());
56 template = new dd.Template('Hot{% comment %}<strong>Make me disappear</strong> Pocket');
58 t.is("Unclosed tag found when looking for endcomment", e.message);
63 function test_tag_cycle(t){
66 var context = new dd.Context({
67 items: ["apple", "banana", "lemon"],
70 var template = new dd.Template("{% for item in items %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
71 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
72 // Make sure that it doesn't break on re-render
73 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
75 // Test repeating the loop
76 context.items.push("guava", "mango", "pineapple");
77 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));
79 // Repeat the above tests for the old style
80 // ========================================
81 context.items = context.items.slice(0, 3);
82 template = new dd.Template("{% for item in items %}{% cycle Hot,Diarrhea,Torrey,Extra %} Pocket. {% endfor %}");
83 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
84 // Make sure that it doesn't break on re-render
85 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
87 // Test repeating the loop
88 context.items.push("guava", "mango", "pineapple");
89 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));
91 // Now test outside of the for loop
92 // ================================
93 context = new dojox.dtl.Context({ unplugged: "Torrey" });
94 template = new dd.Template("{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' as steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket.");
95 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket.", template.render(context));
97 template = new dd.Template("{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' as steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket.");
98 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket.", template.render(context));
100 // Test for nested objects
102 list: ["apple", "banana", "lemon"]
104 template = new dd.Template("{% for item in items.list %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
105 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
106 // Make sure that it doesn't break on re-render
107 t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
109 function test_tag_debug(t){
112 var context = new dd.Context({
113 items: ["apple", "banana", "lemon"],
116 var template = new dd.Template("{% debug %}");
117 t.is('items: ["apple","banana","lemon"]\n\nunplugged: "Torrey"\n\n', template.render(context));
119 function test_tag_filter(t){
122 var template = new dd.Template('{% filter lower|center:"15" %}Hot Pocket{% endfilter %}');
123 t.is(" hot pocket ", template.render());
125 function test_tag_firstof(t){
128 var context = new dd.Context({
132 var template = new dd.Template("{% firstof one two three four found %}");
133 t.is("unicorn", template.render(context));
136 t.is("null", template.render(context));
138 context.three = false;
139 t.is("false", template.render(context));
141 function test_tag_for(t){
144 var context = new dd.Context({
145 items: ["apple", "banana", "lemon"]
147 var template = new dd.Template("{% for item in items %}<li>{{ item }}</li>{% endfor %}");
148 t.is("<li>apple</li><li>banana</li><li>lemon</li>", template.render(context));
150 template = new dd.Template("{% for item in items reversed %}<li>{{ item }}</li>{% endfor %}");
151 t.is("<li>lemon</li><li>banana</li><li>apple</li>", template.render(context));
154 apple: "Red Delicious",
158 template = new dd.Template("{% for key, value in items.items %}<li>{{ value }} {{ key|title }}</li>{% endfor %}");
159 t.is("<li>Red Delicious Apple</li><li>Cavendish Banana</li><li>Citrus Lemon</li>", template.render(context));
161 // The same thing above, but using "zipped" sets
163 ["apple", "Red Delicious", 1.99],
164 ["banana", "Cavendish", 0.49],
165 ["lemon", "Citrus", 0.29]
167 template = new dd.Template("{% for fruit, type, price in items %}<li>{{ type }} {{ fruit|title }} costs ${{ price}}</li>{% endfor %}");
168 t.is("<li>Red Delicious Apple costs $1.99</li><li>Cavendish Banana costs $0.49</li><li>Citrus Lemon costs $0.29</li>", template.render(context));
170 template = new dd.Template("{% for fruit, type, price in items reversed %}<li>{{ type }} {{ fruit|title }} costs ${{ price}}</li>{% endfor %}");
171 t.is("<li>Citrus Lemon costs $0.29</li><li>Cavendish Banana costs $0.49</li><li>Red Delicious Apple costs $1.99</li>", template.render(context));
173 // Now to create some errors
176 template = new dd.Template("{% for item initems %}<li>{{ item }}</li>{% endfor %}");
179 t.is("'for' statements should have at least four words: for item initems", e.message);
185 template = new dd.Template("{% for item ni items %}<li>{{ item }}</li>{% endfor %}");
188 t.is("'for' tag received an invalid argument: for item ni items", e.message);
194 template = new dd.Template("{% for my item in items %}<li>{{ item }}</li>{% endfor %}");
197 t.is("'for' tag received an invalid argument: for my item in items", e.message);
201 function test_tag_if(t){
204 var context = new dd.Context({
211 var template = new dd.Template("Comedian is {% if jokes.hot_pockets and jokes.unicycles and jokes.bacon %}funny{% else %}not funny{% endif %}");
212 t.is("Comedian is funny", template.render(context));
214 context.jokes.unicycles = false;
215 t.is("Comedian is not funny", template.render(context));
217 context.comedians = {
222 template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan %}worth seeing{% else %}not worth seeing{% endif %}");
223 t.is("Show will be worth seeing", template.render(context));
225 // NOTE: "and" is implied by nesting. eg {% if sunny %}{% if windy %}It's Sunny and Windy{% endif %}{% endif %}
226 // Not mixing ands and ors allows for MUCH faster rendering
227 template = new dd.Template("Show will {% if comedians.hedberg or comedians.gaffigan %}{% if comedians.cook %}not {% endif %}be worth seeing{% else %}not be worth seeing{% endif %}");
228 t.is("Show will not be worth seeing", template.render(context));
230 context.comedians.cook = false;
231 t.is("Show will be worth seeing", template.render(context));
233 template = new dd.Template("Show will be {% if comedians.hedberg and comedians.gaffigan and not comedians.cook %}AWESOME{% else %}almost awesome{% endif %}");
234 t.is("Show will be AWESOME", template.render(context));
236 context.comedians.cook = true;
237 t.is("Show will be almost awesome", template.render(context));
239 // Now we test for errors.
242 template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan and not comedians.cook %}worth seeing{% else %}not worth seeing{% endif %}");
245 t.is("'if' tags can't mix 'and' and 'or'", e.message);
249 function test_tag_ifchanged(t){
252 var context = new dd.Context({
255 new Date(2008, 0, 12),
256 new Date(2008, 0, 28),
257 new Date(2008, 1, 1),
258 new Date(2008, 1, 1),
263 var template = new dd.Template("<h1>Archive for {{ year }}</h1>"+
264 "{% for date in days %}"+
265 '{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}'+
266 '<a href="{{ date|date:\'M/d\'|lower }}/">{{ date|date:\'j\' }}</a>'+
268 t.is('<h1>Archive for 2008</h1>'+
270 '<a href="jan/12/">12</a>'+
271 '<a href="jan/28/">28</a>'+
273 '<a href="feb/01/">1</a>'+
274 '<a href="feb/01/">1</a>'+
275 '<a href="feb/01/">1</a>', template.render(context));
277 template = new dd.Template('{% for date in days %}'+
278 '{% ifchanged date.date %} {{ date.date }} {% endifchanged %}'+
279 '{% ifchanged date.hour date.date %}'+
281 '{% endifchanged %}'+
283 t.is(' 2008-01-12 0 2008-01-28 0 2008-02-01 0', template.render(context));
285 function test_tag_ifequal(t){
288 var context = new dd.Context({
297 var template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endifequal %}");
298 t.is("You posted this", template.render(context));
300 context.user.id = 313;
301 t.is("", template.render(context));
306 template = new dd.Template("{% ifequal user.id %}You posted this{% endifequal %}");
309 t.is("ifequal takes two arguments", e.message);
315 template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endif %}");
318 t.is("No tag found for endif", e.message);
322 function test_tag_ifnotequal(t){
325 var context = new dd.Context({
330 var template = new dd.Template("{% ifnotequal favorite comedian %}Not your favorite{% else %}Your favorite{% endifnotequal %}");
331 t.is("Not your favorite", template.render(context));
333 context.comedian = "hedberg";
334 t.is("Your favorite", template.render(context));
336 function test_tag_include(t){
339 var context = new dd.Context({
340 hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"),
342 people: ["Charles", "Ralph", "Julia"]
345 var template = new dd.Template("{% include hello %}");
346 t.is("Hello, <span>Bob</span>", template.render(context));
348 template = new dd.Template('{% include "../../dojox/dtl/tests/templates/hello.html" %}');
349 t.is("Hello, <span>Bob</span>", template.render(context));
351 template = new dd.Template('{% for person in people %}{% include hello %} {% endfor %}');
352 t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context));
354 function test_tag_load(t){
355 t.f(dojox.dtl.tests.text.load);
356 new dojox.dtl.Template("{% load dojox.dtl.tests.text.load %}");
357 t.t(dojox.dtl.tests.text.load);
359 function test_tag_now(t){
362 var template = new dd.Template('It is {% now "jS F Y H:i" %}');
363 t.t(template.render().match(/^It is \d{1,2}[a-z]{2} [A-Z][a-z]+ [0-9]{4,} \d{2}:\d{2}$/));
365 template = new dd.Template('It is the {% now "jS \\o\\f F" %}');
366 t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/));
368 template = new dd.Template("It is the {% now 'jS \\o\\f F' %}");
369 t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/));
373 template = new dd.Template("It is the {% now 'jS \\o\\f F %}");
376 t.is("'now' statement takes one argument", e.message);
382 template = new dd.Template('It is the {% now "jS \\o\\f F %}');
385 t.is("'now' statement takes one argument", e.message);
389 function test_tag_regroup(t){
392 var context = new dd.Context({
394 { firstName: "Bill", lastName: "Clinton", gender: "Male" },
395 { firstName: "Margaret", lastName: "Thatcher", gender: "Female" },
396 { firstName: "Path", lastName: "Smith", gender: "Unkown" },
397 { firstName: "Condoleezza", lastName: "Rice", gender: "Female" },
398 { firstName: "George", lastName: "Bush", gender: "Male" }
402 var template = new dd.Template("{% regroup people|dictsort:'gender' by gender as grouped %}<ul>{% for group in grouped %}<li>{{ group.grouper }}<ul>{% for item in group.list %}<li>{{ item.firstName }} {{ item.lastName }}</li>{% endfor %}</ul></li>{% endfor %}</ul>");
403 t.t(template.render(context).match(new RegExp("^<ul><li>Female<ul><li>(Condoleezza Rice|Margaret Thatcher)</li><li>(Condoleezza Rice|Margaret Thatcher)</li></ul></li><li>Male<ul><li>(Bill Clinton|George Bush)</li><li>(Bill Clinton|George Bush)</li></ul></li><li>Unkown<ul><li>Path Smith</li></ul></li></ul>$")));
405 function test_tag_spaceless(t){
408 var template = new dd.Template("{% spaceless %}<ul> \n <li>Hot</li> \n\n<li>Pocket </li>\n </ul>{% endspaceless %}");
409 t.is("<ul><li>Hot</li><li>Pocket </li></ul>", template.render());
411 function test_tag_ssi(t){
414 var context = new dd.Context({
415 hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"),
417 people: ["Charles", "Ralph", "Julia"]
420 var template = new dd.Template("{% ssi hello parsed %}");
421 t.is("Hello, <span>Bob</span>", template.render(context));
423 template = new dd.Template("{% ssi hello %}");
424 t.is("Hello, <span>{{ person }}</span>", template.render(context));
426 template = new dd.Template('{% ssi "../../dojox/dtl/tests/templates/hello.html" parsed %}');
427 t.is("Hello, <span>Bob</span>", template.render(context));
429 template = new dd.Template('{% for person in people %}{% ssi hello parsed %} {% endfor %}');
430 t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context));
432 function test_tag_templatetag(t){
435 var template = new dd.Template("{% templatetag openblock %}");
436 t.is("{%", template.render());
437 template = new dd.Template("{% templatetag closeblock %}");
438 t.is("%}", template.render());
439 template = new dd.Template("{% templatetag openvariable %}");
440 t.is("{{", template.render());
441 template = new dd.Template("{% templatetag closevariable %}");
442 t.is("}}", template.render());
443 template = new dd.Template("{% templatetag openbrace %}");
444 t.is("{", template.render());
445 template = new dd.Template("{% templatetag closebrace %}");
446 t.is("}", template.render());
447 template = new dd.Template("{% templatetag opencomment %}");
448 t.is("{#", template.render());
449 template = new dd.Template("{% templatetag closecomment %}");
450 t.is("#}", template.render());
452 function test_tag_widthratio(t){
455 var context = new dd.Context({
460 var template = new dd.Template('<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />');
461 t.is('<img src="bar.gif" height="10" width="88" />', template.render(context));
463 function test_tag_with(t){
466 var context = new dd.Context({
468 someSqlMethod: function(){
474 var template = new dd.Template('{% with person.someSqlMethod as total %}{{ total }} object{{ total|pluralize }}{% endwith %}')
475 t.is("4815162342 objects", template.render(context));