Skip to content

Commit

Permalink
Fix for rendering falsey values: Issue #31:
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisMoore committed Mar 11, 2012
1 parent 21da66a commit 34f9aff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
5 changes: 2 additions & 3 deletions jsrender.js
Expand Up @@ -171,7 +171,7 @@ function View( context, path, parentView, data, template, index ) {
tmpl: template,
path: path,
parent: parentView,
data : data||{},
data: data,
ctx: context,
views: $.isArray( data ) ? [] : {}
};
Expand Down Expand Up @@ -282,7 +282,6 @@ function renderContent( data, context, parentView, path, index ) {
path = path || self.path;
index = index || self.index;
props = self.props;
// data = data === undefined ? parentView.data : data;
} else {
tmpl = self.jquery && self[0] // This is a call $.fn.render
|| self; // This is a call from tmpl.render
Expand Down Expand Up @@ -323,7 +322,7 @@ function renderContent( data, context, parentView, path, index ) {
for ( i = 0, l = data.length; i < l; i++ ) {
// Create a view for each data item.
dataItem = data[ i ];
itemResult = tmpl.fn( dataItem || {}, View( context, path, newView, dataItem, tmpl, (index||0) + i ));
itemResult = tmpl.fn( dataItem, View( context, path, newView, dataItem, tmpl, (index||0) + i ), jsv );
result += itemWrap ? itemWrap( itemResult, props ) : itemResult;
}
} else {
Expand Down
13 changes: 8 additions & 5 deletions test/unit/jsrender-tests-no-jquery.js
Expand Up @@ -33,7 +33,6 @@ var person = { name: "Jo" },
var tmplString = "A_{{:name}}_B";
jsviews.tags({ sort: sort });

//jsviews.allowCode = true;
module( "tagParser" );
test("{{if}} {{else}}", function() {
expect(3);
Expand Down Expand Up @@ -179,15 +178,16 @@ test("expressions", function() {

module( "{{for}}" );
test("{{for}}", function() {
expect(8);
expect(9);
jsviews.templates( {
forTmpl: "header_{{for people}}{{:name}}{{/for}}_footer",
layoutTmpl: {
markup: "header_{{for}}{{:name}}{{/for}}_footer",
layout: true
},
pageTmpl: '{{for people tmpl="layoutTmpl"/}}',
simpleFor: "a{{for people}}Content{{/for}}b"
simpleFor: "a{{for people}}Content{{/for}}b",
forPrimitiveDataTypes: "a{{for people}}{{:#data}}{{/for}}b"
});

equal( jsviews.render.forTmpl({ people: people }), "header_JoBill_footer", '{{for people}}...{{/for}}' );
Expand All @@ -199,6 +199,7 @@ test("{{for}}", function() {
equal( jsviews.render.simpleFor({people:[null,undefined,1]}), "aContentContentContentb", 'null or undefined members of array are also rendered' );
equal( jsviews.render.simpleFor({people:null}), "ab", 'null is rendered as empty string' );
equal( jsviews.render.simpleFor({}), "ab", 'undefined is rendered as empty string' );
equal( jsviews.render.forPrimitiveDataTypes({people:[0,1,"abc","",,true,false]}), "a01abctruefalseb", 'Primitive types render correctly, even if falsey' );
});

module( "api" );
Expand Down Expand Up @@ -255,14 +256,15 @@ test("templates", function() {
});

test("render", function() {
expect(13);
expect(14);
var tmpl1 = jsviews.templates( "myTmpl8", tmplString );
jsviews.templates( {
simple: "Content",
simpleLayout: {
markup: "Content",
layout: true
}
},
primitiveDataTypes: "{{:#data}}"
});

equal( tmpl1.render( person ), "A_Jo_B", 'tmpl1.render( data );' );
Expand All @@ -282,6 +284,7 @@ test("render", function() {
equal( jsviews.render.simpleLayout([]), "Content", 'Layout renders once, for empty array' );
equal( jsviews.render.simpleLayout(null), "Content", 'Layout renders once, for null' );
equal( jsviews.render.simpleLayout(), "Content", 'Layout renders once, for undefined' );
equal( jsviews.render.primitiveDataTypes([0,1,"abc","",,true,false]), "01abctruefalse", 'Primitive types render correctly, even if falsey' );
});

test("converters", function() {
Expand Down

0 comments on commit 34f9aff

Please sign in to comment.