Permalink
Browse files

Added support for arrays as sections.

  • Loading branch information...
1 parent ba6cdcf commit f53dc9397b39a054fc435abdc45e00ccc8dccd56 @pmcelhaney committed May 30, 2010
Showing with 19 additions and 2 deletions.
  1. +8 −2 Mustache.cfc
  2. +11 −0 Tests.cfc
View
@@ -41,13 +41,19 @@
<cfargument name="context"/>
<cfset var ctx = get(tagName, context) />
<cfset var result = "" />
+ <cfset var item = "" />
<cfif isStruct(ctx)>
<cfreturn render(inner, ctx)>
<cfelseif isQuery(ctx)>
<cfloop query="ctx">
<cfset result &= render(inner, ctx) /> <!--- TODO: should probably use StringBuilder for performance --->
</cfloop>
- <cfreturn result/>
+ <cfreturn result/>
+ <cfelseif isArray(ctx)>
+ <cfloop array="#ctx#" index="item">
+ <cfset result &= render(inner, item) /> <!--- TODO: should probably use StringBuilder for performance --->
+ </cfloop>
+ <cfreturn result />
<cfelseif isFunction(context[tagName])>
<cfreturn evaluate("context.#tagName#(inner)") />
<cfelseif ctx xor type eq "^">
@@ -130,7 +136,7 @@
<!--- TODO: Find a good way to determine whether an object is a function --->
<cffunction name="isFunction" access="private" output="false">
<cfargument name="object" />
- <cfreturn not (isSimpleValue(object) or isStruct(object) or isQuery(object)) />
+ <cfreturn not (isSimpleValue(object) or isStruct(object) or isQuery(object) or isArray(object)) />
</cffunction>
</cfcomponent>
View
@@ -89,6 +89,17 @@
<cfset context = {contacts = contacts} />
<cfset template = "{{##contacts}}({{name}}'s number is {{phone}}){{/contacts}}">
<cfset expected = "(Jenny's number is 867-5309)(Tom's number is 555-1234)" />
+ </cffunction>
+
+ <cffunction name="arrayAsSection">
+ <cfset context = {
+ contacts = [
+ { name = 'Jenny', phone = '867-5309'}
+ , { name = 'Tom', phone = '555-1234'}
+ ]
+ } />
+ <cfset template = "{{##contacts}}({{name}}'s number is {{phone}}){{/contacts}}">
+ <cfset expected = "(Jenny's number is 867-5309)(Tom's number is 555-1234)" />
</cffunction>
<cffunction name="escape">

0 comments on commit f53dc93

Please sign in to comment.