if(!dojo._hasResource["dojox.dtl.tests.text.tag"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.dtl.tests.text.tag"] = true;
dojo.provide("dojox.dtl.tests.text.tag");
dojo.require("dojox.dtl");
dojo.require("dojox.dtl.Context");
doh.register("dojox.dtl.text.tag",
[
function test_tag_block_and_extends(t){
var dd = dojox.dtl;
// Simple (messy) string-based extension
var template = new dd.Template('{% extends "../../dojox/dtl/tests/templates/pocket.html" %}{% block pocket %}Simple{% endblock %}');
t.is("Simple Pocket", template.render());
// Variable replacement
var context = new dd.Context({
parent: "../../dojox/dtl/tests/templates/pocket.html"
})
template = new dd.Template('{% extends parent %}{% block pocket %}Variabled{% endblock %}');
t.is("Variabled Pocket", template.render(context));
// Nicer dojo.moduleUrl and variable based extension
context.parent = dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html");
template = new dd.Template('{% extends parent %}{% block pocket %}Slightly More Advanced{% endblock %}');
t.is("Slightly More Advanced Pocket", template.render(context));
// dojo.moduleUrl with support for more variables.
// This is important for HTML templates where the "shared" flag will be important.
context.parent = {
url: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket.html")
}
template = new dd.Template('{% extends parent %}{% block pocket %}Super{% endblock %}');
t.is("Super Pocket", template.render(context));
},
function test_tag_block(t){
var dd = dojox.dtl;
var context = new dd.Context({
parent: dojo.moduleUrl("dojox.dtl.tests.templates", "pocket2.html"),
items: ["apple", "banana", "lemon" ]
});
var template = new dd.Template("{% extends parent %}{% block pocket %}My {{ item }}{% endblock %}");
t.is("(My apple) (My banana) (My lemon) Pocket", template.render(context));
},
function test_tag_comment(t){
var dd = dojox.dtl;
var template = new dd.Template('Hot{% comment %}Make me disappear{% endcomment %} Pocket');
t.is("Hot Pocket", template.render());
var found = false;
try{
template = new dd.Template('Hot{% comment %}Make me disappear Pocket');
}catch(e){
t.is("Unclosed tag found when looking for endcomment", e.message);
found = true;
}
t.t(found);
},
function test_tag_cycle(t){
var dd = dojox.dtl;
var context = new dd.Context({
items: ["apple", "banana", "lemon"],
unplugged: "Torrey"
});
var template = new dd.Template("{% for item in items %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
// Make sure that it doesn't break on re-render
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
// Test repeating the loop
context.items.push("guava", "mango", "pineapple");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));
// Repeat the above tests for the old style
// ========================================
context.items = context.items.slice(0, 3);
template = new dd.Template("{% for item in items %}{% cycle Hot,Diarrhea,Torrey,Extra %} Pocket. {% endfor %}");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
// Make sure that it doesn't break on re-render
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
// Test repeating the loop
context.items.push("guava", "mango", "pineapple");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket. ", template.render(context));
// Now test outside of the for loop
// ================================
context = new dojox.dtl.Context({ unplugged: "Torrey" });
template = new dd.Template("{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' as steakum %} Pocket. {% cycle steakum %} Pocket. {% cycle steakum %} Pocket.");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket.", template.render(context));
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.");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. Extra Pocket. Hot Pocket. Diarrhea Pocket.", template.render(context));
//t.t(false)
// Test for nested objects
context.items = {
list: ["apple", "banana", "lemon"]
};
template = new dd.Template("{% for item in items.list %}{% cycle 'Hot' 'Diarrhea' unplugged 'Extra' %} Pocket. {% endfor %}");
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
// Make sure that it doesn't break on re-render
t.is("Hot Pocket. Diarrhea Pocket. Torrey Pocket. ", template.render(context));
},
function test_tag_debug(t){
var dd = dojox.dtl;
var context = new dd.Context({
items: ["apple", "banana", "lemon"],
unplugged: "Torrey"
});
var template = new dd.Template("{% debug %}");
t.is('items: ["apple","banana","lemon"]\n\nunplugged: "Torrey"\n\n', template.render(context));
},
function test_tag_filter(t){
var dd = dojox.dtl;
var template = new dd.Template('{% filter lower|center:"15" %}Hot Pocket{% endfilter %}');
t.is(" hot pocket ", template.render());
},
function test_tag_firstof(t){
var dd = dojox.dtl;
var context = new dd.Context({
found: "unicorn"
});
var template = new dd.Template("{% firstof one two three four found %}");
t.is("unicorn", template.render(context));
context.four = null;
t.is("null", template.render(context));
context.three = false;
t.is("false", template.render(context));
},
function test_tag_for(t){
var dd = dojox.dtl;
var context = new dd.Context({
items: ["apple", "banana", "lemon"]
});
var template = new dd.Template("{% for item in items %}
{{ item }}{% endfor %}");
t.is("applebananalemon", template.render(context));
template = new dd.Template("{% for item in items reversed %}{{ item }}{% endfor %}");
t.is("lemonbananaapple", template.render(context));
context.items = {
apple: "Red Delicious",
banana: "Cavendish",
lemon: "Citrus"
};
template = new dd.Template("{% for key, value in items.items %}{{ value }} {{ key|title }}{% endfor %}");
t.is("Red Delicious AppleCavendish BananaCitrus Lemon", template.render(context));
// The same thing above, but using "zipped" sets
context.items = [
["apple", "Red Delicious", 1.99],
["banana", "Cavendish", 0.49],
["lemon", "Citrus", 0.29]
];
template = new dd.Template("{% for fruit, type, price in items %}{{ type }} {{ fruit|title }} costs ${{ price}}{% endfor %}");
t.is("Red Delicious Apple costs $1.99Cavendish Banana costs $0.49Citrus Lemon costs $0.29", template.render(context));
template = new dd.Template("{% for fruit, type, price in items reversed %}{{ type }} {{ fruit|title }} costs ${{ price}}{% endfor %}");
t.is("Citrus Lemon costs $0.29Cavendish Banana costs $0.49Red Delicious Apple costs $1.99", template.render(context));
// Now to create some errors
var found = false;
try {
template = new dd.Template("{% for item initems %}{{ item }}{% endfor %}");
}catch(e){
found = true;
t.is("'for' statements should have at least four words: for item initems", e.message);
}
t.t(found);
found = false;
try {
template = new dd.Template("{% for item ni items %}{{ item }}{% endfor %}");
}catch(e){
found = true;
t.is("'for' tag received an invalid argument: for item ni items", e.message);
}
t.t(found);
found = false;
try {
template = new dd.Template("{% for my item in items %}{{ item }}{% endfor %}");
}catch(e){
found = true;
t.is("'for' tag received an invalid argument: for my item in items", e.message);
}
t.t(found);
},
function test_tag_if(t){
var dd = dojox.dtl;
var context = new dd.Context({
jokes: {
hot_pockets: true,
unicycles: true,
bacon: true
}
});
var template = new dd.Template("Comedian is {% if jokes.hot_pockets and jokes.unicycles and jokes.bacon %}funny{% else %}not funny{% endif %}");
t.is("Comedian is funny", template.render(context));
context.jokes.unicycles = false;
t.is("Comedian is not funny", template.render(context));
context.comedians = {
hedberg: true,
gaffigan: true,
cook: true
};
template = new dd.Template("Show will be {% if comedians.hedberg or comedians.gaffigan %}worth seeing{% else %}not worth seeing{% endif %}");
t.is("Show will be worth seeing", template.render(context));
// NOTE: "and" is implied by nesting. eg {% if sunny %}{% if windy %}It's Sunny and Windy{% endif %}{% endif %}
// Not mixing ands and ors allows for MUCH faster rendering
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 %}");
t.is("Show will not be worth seeing", template.render(context));
context.comedians.cook = false;
t.is("Show will be worth seeing", template.render(context));
template = new dd.Template("Show will be {% if comedians.hedberg and comedians.gaffigan and not comedians.cook %}AWESOME{% else %}almost awesome{% endif %}");
t.is("Show will be AWESOME", template.render(context));
context.comedians.cook = true;
t.is("Show will be almost awesome", template.render(context));
// Now we test for errors.
var found = false;
try {
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("Archive for {{ year }}
"+
"{% for date in days %}"+
'{% ifchanged %}{{ date|date:"F" }}
{% endifchanged %}'+
'{{ date|date:\'j\' }}'+
"{% endfor %}");
t.is('Archive for 2008
'+
'January
'+
'12'+
'28'+
'February
'+
'1'+
'1'+
'1', 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, Bob", template.render(context));
template = new dd.Template('{% include "../../dojox/dtl/tests/templates/hello.html" %}');
t.is("Hello, Bob", template.render(context));
template = new dd.Template('{% for person in people %}{% include hello %} {% endfor %}');
t.is("Hello, Charles Hello, Ralph Hello, Julia ", 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 %}{% for group in grouped %}- {{ group.grouper }}
{% for item in group.list %}- {{ item.firstName }} {{ item.lastName }}
{% endfor %}
{% endfor %}
");
t.t(template.render(context).match(new RegExp("^- Female
- (Condoleezza Rice|Margaret Thatcher)
- (Condoleezza Rice|Margaret Thatcher)
- Male
- (Bill Clinton|George Bush)
- (Bill Clinton|George Bush)
- Unkown
$")));
},
function test_tag_spaceless(t){
var dd = dojox.dtl;
var template = new dd.Template("{% spaceless %}{% endspaceless %}");
t.is("", 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, Bob", template.render(context));
template = new dd.Template("{% ssi hello %}");
t.is("Hello, {{ person }}", template.render(context));
template = new dd.Template('{% ssi "../../dojox/dtl/tests/templates/hello.html" parsed %}');
t.is("Hello, Bob", template.render(context));
template = new dd.Template('{% for person in people %}{% ssi hello parsed %} {% endfor %}');
t.is("Hello, Charles Hello, Ralph Hello, Julia ", 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('
');
t.is('
', 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));
}
]
);
}