📄 tag.js
字号:
template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan and not comedians.cook %}worth seeing{% else %}not worth seeing{% endif %}"); }catch(e){ found = true; t.is("'if' tags can't mix 'and' and 'or'", e.message); } t.t(found); }, function test_tag_ifchanged(t){ var dd = dojox.dtl; var context = new dd.Context({ year: 2008, days: [ new Date(2008, 0, 12), new Date(2008, 0, 28), new Date(2008, 1, 1), new Date(2008, 1, 1), new Date(2008, 1, 1) ] }); var template = new dd.Template("<h1>Archive for {{ year }}</h1>"+"{% for date in days %}"+'{% ifchanged %}<h3>{{ date|date:"F" }}</h3>{% endifchanged %}'+'<a href="{{ date|date:\'M/d\'|lower }}/">{{ date|date:\'j\' }}</a>'+"{% endfor %}"); t.is('<h1>Archive for 2008</h1>'+'<h3>January</h3>'+'<a href="jan/12/">12</a>'+'<a href="jan/28/">28</a>'+'<h3>February</h3>'+'<a href="feb/01/">1</a>'+'<a href="feb/01/">1</a>'+'<a href="feb/01/">1</a>', template.render(context)); template = new dd.Template('{% for date in days %}'+'{% ifchanged date.date %} {{ date.date }} {% endifchanged %}'+'{% ifchanged date.hour date.date %}'+'{{ date.hour }}'+'{% endifchanged %}'+'{% endfor %}'); t.is(' 2008-01-12 0 2008-01-28 0 2008-02-01 0', template.render(context)); }, function test_tag_ifequal(t){ var dd = dojox.dtl; var context = new dd.Context({ user: { id: 314 }, comment: { user_id: 314 } }); var template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endifequal %}"); t.is("You posted this", template.render(context)); context.user.id = 313; t.is("", template.render(context)); // Errors var found = false; try { template = new dd.Template("{% ifequal user.id %}You posted this{% endifequal %}"); }catch(e){ found = true; t.is("ifequal takes two arguments", e.message); } t.t(found); found = false; try { template = new dd.Template("{% ifequal user.id comment.user_id %}You posted this{% endif %}"); }catch(e){ found = true; t.is("No tag found for endif", e.message); } t.t(found); }, function test_tag_ifnotequal(t){ var dd = dojox.dtl; var context = new dd.Context({ favorite: "hedberg", comedian: "cook" }); var template = new dd.Template("{% ifnotequal favorite comedian %}Not your favorite{% else %}Your favorite{% endifnotequal %}"); t.is("Not your favorite", template.render(context)); context.comedian = "hedberg"; t.is("Your favorite", template.render(context)); }, function test_tag_include(t){ var dd = dojox.dtl; var context = new dd.Context({ hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"), person: "Bob", people: ["Charles", "Ralph", "Julia"] }); var template = new dd.Template("{% include hello %}"); t.is("Hello, <span>Bob</span>", template.render(context)); template = new dd.Template('{% include "../../dojox/dtl/tests/templates/hello.html" %}'); t.is("Hello, <span>Bob</span>", template.render(context)); template = new dd.Template('{% for person in people %}{% include hello %} {% endfor %}'); t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context)); }, function test_tag_load(t){ t.f(dojox.dtl.tests.text.load); new dojox.dtl.Template("{% load dojox.dtl.tests.text.load %}"); t.t(dojox.dtl.tests.text.load); }, function test_tag_now(t){ var dd = dojox.dtl; var template = new dd.Template('It is {% now "jS F Y H:i" %}'); t.t(template.render().match(/^It is \d{1,2}[a-z]{2} [A-Z][a-z]+ [0-9]{4,} \d{2}:\d{2}$/)); template = new dd.Template('It is the {% now "jS \\o\\f F" %}'); t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/)); template = new dd.Template("It is the {% now 'jS \\o\\f F' %}"); t.t(template.render().match(/^It is the \d{1,2}[a-z]{2} of [A-Z][a-z]+$/)); var found = false; try{ template = new dd.Template("It is the {% now 'jS \\o\\f F %}"); }catch(e){ found = true; t.is("'now' statement takes one argument", e.message); } t.t(found); found = false; try{ template = new dd.Template('It is the {% now "jS \\o\\f F %}'); }catch(e){ found = true; t.is("'now' statement takes one argument", e.message); } t.t(found); }, function test_tag_regroup(t){ var dd = dojox.dtl; var context = new dd.Context({ people: [ { firstName: "Bill", lastName: "Clinton", gender: "Male" }, { firstName: "Margaret", lastName: "Thatcher", gender: "Female" }, { firstName: "Path", lastName: "Smith", gender: "Unkown" }, { firstName: "Condoleezza", lastName: "Rice", gender: "Female" }, { firstName: "George", lastName: "Bush", gender: "Male" } ] }); 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>"); 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>$"))); }, function test_tag_spaceless(t){ var dd = dojox.dtl; var template = new dd.Template("{% spaceless %}<ul> \n <li>Hot</li> \n\n<li>Pocket </li>\n </ul>{% endspaceless %}"); t.is("<ul><li>Hot</li><li>Pocket </li></ul>", template.render()); }, function test_tag_ssi(t){ var dd = dojox.dtl; var context = new dd.Context({ hello: dojo.moduleUrl("dojox.dtl.tests.templates", "hello.html"), person: "Bob", people: ["Charles", "Ralph", "Julia"] }); var template = new dd.Template("{% ssi hello parsed %}"); t.is("Hello, <span>Bob</span>", template.render(context)); template = new dd.Template("{% ssi hello %}"); t.is("Hello, <span>{{ person }}</span>", template.render(context)); template = new dd.Template('{% ssi "../../dojox/dtl/tests/templates/hello.html" parsed %}'); t.is("Hello, <span>Bob</span>", template.render(context)); template = new dd.Template('{% for person in people %}{% ssi hello parsed %} {% endfor %}'); t.is("Hello, <span>Charles</span> Hello, <span>Ralph</span> Hello, <span>Julia</span> ", template.render(context)); }, function test_tag_templatetag(t){ var dd = dojox.dtl; var template = new dd.Template("{% templatetag openblock %}"); t.is("{%", template.render()); template = new dd.Template("{% templatetag closeblock %}"); t.is("%}", template.render()); template = new dd.Template("{% templatetag openvariable %}"); t.is("{{", template.render()); template = new dd.Template("{% templatetag closevariable %}"); t.is("}}", template.render()); template = new dd.Template("{% templatetag openbrace %}"); t.is("{", template.render()); template = new dd.Template("{% templatetag closebrace %}"); t.is("}", template.render()); template = new dd.Template("{% templatetag opencomment %}"); t.is("{#", template.render()); template = new dd.Template("{% templatetag closecomment %}"); t.is("#}", template.render()); }, function test_tag_widthratio(t){ var dd = dojox.dtl; var context = new dd.Context({ this_value: 175, max_value: 200 }); var template = new dd.Template('<img src="bar.gif" height="10" width="{% widthratio this_value max_value 100 %}" />'); t.is('<img src="bar.gif" height="10" width="88" />', template.render(context)); }, function test_tag_with(t){ var dd = dojox.dtl; var context = new dd.Context({ person: { someSqlMethod: function(){ return 4815162342; } } }); var template = new dd.Template('{% with person.someSqlMethod as total %}{{ total }} object{{ total|pluralize }}{% endwith %}') t.is("4815162342 objects", template.render(context)); } ]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -