Skip to main content
WolfShade
Legend
November 12, 2015
Answered

onMissingTemplate function in Application.cfc

  • November 12, 2015
  • 2 replies
  • 1202 views

Hello, all,

I'm trying to implement a process for missing templates, so that the user will never see a CF error message for missing pages.

The problem is that even though it will trigger my onError function, it's still outputting CF errors at the bottom of the page.

Here is my onMissingTemplate() function:

<cffunction name="onMissingTemplate" returntype="boolean" output="false" >

    <cfargument name="template" type="string" required="true" />

    <cfset this.onRequestStart(arguments.template) />

    <cfset this.onRequest(arguments.template) />

    <cfreturn true />

</cffunction>

How can I get this to stop processing after displaying the onError page, so that it won't show the CF error mesages?

V/r,

^_^

    This topic has been closed for replies.
    Correct answer Steve Sommers

    From what I can tell, your onMissingTemplate trap is probably working but your code within the event is retriggering the same error. I'm not sure your goal. I usually create an error.cfm template and simply include that module:

    <cffunction name="onMissingTemplate" returntype="boolean" output="false" >

        <cfargument name="template" type="string" required="true" />

        <cfinclude template="error.cfm" />

        <cfabort />

    </cffunction>

    I'm not 100% certain if the cfabort call is required but it does not seem to hurt. <cfreturn false /> probably does the same thing.

    2 replies

    Carl Von Stetten
    Legend
    November 28, 2015

    WolfShade‌, what is in your onRequest() method?  If you pass the nonexistent template as an argument of onRequest(), but don't force navigation to a different page, it would still throw an error.  Do you have a FileExists() check in onRequest() to verify the template exists prior to including it?

    -Carl V.

    WolfShade
    WolfShadeAuthor
    Legend
    November 30, 2015

    Hi, Carl Von Stetten‌!

    My onRequest() method is bare-bones:

    <cffunction name="onRequest" returntype="void">

      <cfargument name="thePage" type="string" required="true" />

      <cfinclude template="#arguments.thePage#" />

    </cffunction>

    The onError() method is triggered, which redirects to my errors.cfm page, and it _should_ stop processing after displaying errors.cfm.

    I've seen several examples online for onRequest() and onError(), and I've never seen one include a FileExists() conditional.  Is that common??

    V/r,

    ^_^

    Steve SommersCorrect answer
    Legend
    November 30, 2015

    From what I can tell, your onMissingTemplate trap is probably working but your code within the event is retriggering the same error. I'm not sure your goal. I usually create an error.cfm template and simply include that module:

    <cffunction name="onMissingTemplate" returntype="boolean" output="false" >

        <cfargument name="template" type="string" required="true" />

        <cfinclude template="error.cfm" />

        <cfabort />

    </cffunction>

    I'm not 100% certain if the cfabort call is required but it does not seem to hurt. <cfreturn false /> probably does the same thing.

    WolfShade
    WolfShadeAuthor
    Legend
    November 16, 2015

    I'll add a screencap of what I'm referring to.  Everything below the logo should not be displaying.