Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
If a field is missing, just skip it.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Jul 8, 2010
1 parent 87b4748 commit 9d69765
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
18 changes: 11 additions & 7 deletions mustache/Mustache.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
<cfargument name="inner"/>
<cfargument name="context"/>
<cfset var ctx = get(tagName, context) />
<cfif isStruct(ctx)>
<cfif isStruct(ctx)>
<cfreturn render(inner, ctx)>
<cfelseif isQuery(ctx)>
<cfreturn renderQuerySection(inner, ctx) />
<cfelseif isArray(ctx)>
<cfreturn renderArraySection(inner, ctx) />
<cfelseif isCustomFunction(context[tagName])>
<cfreturn renderArraySection(inner, ctx) />
<cfelseif structKeyExists(context, tagName) and isCustomFunction(context[tagName])>
<cfreturn evaluate("context.#tagName#(inner)") />
<cfelseif convertToBoolean(ctx) xor type eq "^">
<cfreturn inner />
Expand Down Expand Up @@ -134,11 +134,15 @@
<cfif isCustomFunction(context[key])>
<cfreturn evaluate("context.#key#('')")>
<cfelse>
<cfreturn context[key]/>
<cfreturn context[key]/>
</cfif>
<cfelseif isQuery(context)>
<cfreturn context[key][context.currentrow] />
<cfelse>
<cfelseif isQuery(context)>
<cfif listContainsNoCase(context.columnList, key)>
<cfreturn context[key][context.currentrow] />
<cfelse>
<cfreturn "" />
</cfif>
<cfelse>
<cfreturn "" />
</cfif>
</cffunction>
Expand Down
26 changes: 26 additions & 0 deletions tests/RenderTests.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
<cfset template = "Ready {{##set}}set {{/set}}go!" />
<cfset expected = "Ready set go!" />
</cffunction>

<cffunction name="skipMissingField">
<cfset context = structNew() />
<cfset template = "There's something {{##foo}}missing{{/foo}}!" />
<cfset expected = "There's something !" />
</cffunction>

<cffunction name="structAsSection">
<cfset context = {
Expand All @@ -101,6 +107,15 @@
<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="missingQueryColumnIsSkipped">
<cfset contacts = queryNew("name")/>
<cfset queryAddRow(contacts)>
<cfset querySetCell(contacts, "name", "Jenny") />
<cfset context = {contacts = contacts} />
<cfset template = "{{##contacts}}({{name}}'s number is {{phone}}){{/contacts}}">
<cfset expected = "(Jenny's number is )" />
</cffunction>

<cffunction name="arrayAsSection">
Expand All @@ -112,6 +127,17 @@
} />
<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="missingStructKeyIsSkipped">
<cfset context = {
contacts = [
{ name = 'Jenny', phone = '867-5309'}
, { name = 'Tom'}
]
} />
<cfset template = "{{##contacts}}({{name}}'s number is {{^phone}}unlisted{{/phone}}{{phone}}){{/contacts}}">
<cfset expected = "(Jenny's number is 867-5309)(Tom's number is unlisted)" />
</cffunction>

<cffunction name="escape">
Expand Down

0 comments on commit 9d69765

Please sign in to comment.