I'm building a small cms system. Within a page, there can be
placeholders which will be substituted with their content at runtime.
For example: pageContent = 'Time now is @@showTime@@.'. Placeholders;
"Modules-Names" are within @@. The output of this page will be "Time
now is 17:05".
I did the following coding:
Model that gets page content:
<cfcomponent output="false">
<cfproperty name="renderer" inject="coldbox:plugin:renderer"
scope="instance" />
<cfproperty name="moduleManager" inject="model:modules"
scope="instance" />
<!--- get Page --->
<cffunction name="getPage" access="public" returntype="query"
output="false">
<cfset var qGetPage = QueryNew("page_id,page_content") />
<cfset var thisPageContent = "" />
<cfset var moduleArray = "" />
<cfset var thisModule = "" /><cfset var thisModuleContent = "" />
<cfset var i="" />
<cfset QuerySetCell(qGetPage,"page_content","This is a dummy page
content with w modules: module1=@@module1@@ and module2=@@module2@@") /
<!--- check, whether there are modules (@@....@@) within the page.
If yes: Substitute them --->
<cfset moduleArray = REMatch('@@[\S]*@@',qGetPage.page_content) />
<cfif ArrayLen(moduleArray)>
<cfset thisPageContent = qGetPage.page_content />
<!--- Loop over all modules found --->
<cfloop from="1" to="#ArrayLen(moduleArray)#" index="i">
<cfset thisModule = Replace(moduleArray[i],"@@","","all") />
<cfset thisModuleContent = getModuleContent(thisModule) />
<cfset thisPageContent =
Replace(thisPageContent,"#moduleArray[i]#", thisModuleContent, "all") /
</cfloop>
<!--- substitute content --->
<cfset QuerySetCell(qGetPage, "page_content", thisPageContent) />
</cfif>
<cfreturn qGetPage />
</cffunction>
<!--- get content for module --->
<cffunction name="getModuleContent" access="private"
returntype="string" output="false">
<cfargument name="moduleName" required="true" type="string" />
<cfset var ret = "" />
<cfset var moduleData = "" />
<cfset var thisViewTemplate = ExpandPath('.') & "\" &
instance.renderer.getController().getSetting("viewsConvention",true) &
'\pageModules\#arguments.moduleName#.cfm' />
<cfset var
rc=instance.renderer.getController().getRequestService().getContext().getCollection() /
<!--- is there a model method within moduleManager? Yes => call it!
--->
<cfif structKeyExists(instance.moduleManager,arguments.moduleName)>
<cfset rc.moduleData =
Evaluate("instance.moduleManager.#arguments.moduleName#()") />
<cfelse>
<cfset rc.moduleData = StructNew() />
</cfif>
<!--- is there a view defined for the module? Yes => render it! --->
<cfif FileExists(thisViewTemplate)>
<cfset ret = instance.renderer.renderView('pageModules/
#arguments.moduleName#') />
<cfelse>
<cfset ret = '[ no view for module #arguments.moduleName#]' />
</cfif>
<cfreturn ret />
</cffunction>
</cfcomponent>
modules.cfc: