Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for views. When you call render() on a CFC that extends…

… Mustache, by default it will use itself as the context and look for a view called ViewName.mustache
  • Loading branch information...
commit 1edb9f31e3cf69d127c97b127a866e0ad162111c 1 parent 282b7a8
@pmcelhaney authored
View
13 Mustache.cfc
@@ -6,8 +6,8 @@
</cffunction>
<cffunction name="render" output="false">
- <cfargument name="template"/>
- <cfargument name="context" />
+ <cfargument name="template" default="#readMustacheFile(getMetaData(this).name)#"/>
+ <cfargument name="context" default="#this#"/>
<cfset template = renderSections(template, context) />
<cfreturn renderTags(template, context)/>
</cffunction>
@@ -111,11 +111,11 @@
</cfif>
</cffunction>
- <cffunction name="readMustacheFile" access="private">
+ <cffunction name="readMustacheFile" access="private" output="false">
<cfargument name="filename" />
<cfset var template="" />
<cffile action="read" file="#getDirectoryFromPath(getCurrentTemplatePath())##filename#.mustache" variable="template"/>
- <cfreturn template/>
+ <cfreturn trim(template)/>
</cffunction>
<cffunction name="get" access="private" output="false">
@@ -138,8 +138,9 @@
<cfargument name="text"/>
<cfargument name="re"/>
<cfset var results = arrayNew(1) />
- <!--- TODO: Pass in the compiled pattern instead of recompiling every call. --->
- <cfset var pattern = CreateObject("java","java.util.regex.Pattern").compile(arguments.re) />
+ <!--- TODO: Pass in the compiled pattern instead of recompiling every call. --->
+ <cfset var DOTALL = 32 />
+ <cfset var pattern = CreateObject("java","java.util.regex.Pattern").compile(arguments.re, DOTALL) />
<cfset var matcher = pattern.matcher(arguments.text)/>
<cfset var i = 0 />
<cfset var nextMatch = "" />
View
1  Tests.cfc
@@ -1,3 +1,4 @@
<cfcomponent output="false" extends="mxunit.framework.TestSuite">
<cfset addAll("RenderTests")>
+ <cfset addAll("WinnerTest") />
</cfcomponent>
View
3  Winner.cfc
@@ -0,0 +1,3 @@
+<cfcomponent extends="Mustache">
+
+</cfcomponent>
View
5 Winner.mustache
@@ -0,0 +1,5 @@
+Hello {{name}}
+You have just won ${{value}}!
+{{#in_ca}}
+Well, ${{taxed_value}}, after taxes.
+{{/in_ca}}
View
17 WinnerTest.cfc
@@ -0,0 +1,17 @@
+<cfcomponent extends="mxunit.framework.TestCase">
+ <cffunction name="canonincalExample">
+ <cfset winner = createObject("component", "Winner").init()/>
+ <cfset winner.name = "Patrick" />
+ <cfset winner.value = 10000 />
+ <cfset winner.taxed_value = 10000 - 10000 * 0.4/>
+ <cfset winner.in_ca = true />
+ <!--- TODO: Technically, there shouldn't be newlines after "$10000!" and "taxes." (going by the ctemplate spec) --->
+ <cfset expected = "Hello Patrick
+You have just won $10000!
+
+Well, $6000, after taxes.
+" />
+ <cfset assertEquals(expected, winner.render()) />
+ </cffunction>
+
+</cfcomponent>
View
2  gracie_allen.mustache
@@ -1 +1 @@
-{{word}}{{! Contrary to popular belief, she didn't say "Goodnight, Gracie." }}
+{{word}}{{! Contrary to popular belief, she didn't say "Goodnight, Gracie."}}
Please sign in to comment.
Something went wrong with that request. Please try again.