Skip to main content
WolfShade
Legend
Nov 12, 2015 8:35 PM UTC
Answered

onMissingTemplate function in Application.cfc

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 is closed to new replies. Start a new post to keep the conversation going.
    Steve SommersCorrect answer
    Legend
    Nov 30, 2015 6:02 PM UTC

    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.

    6 replies

    Carl Von Stetten
    Legend
    Nov 28, 2015 3:38 AM UTC

    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.

    Translate
    WolfShade
    WolfShadeAuthor
    Legend
    Nov 30, 2015 3:46 PM UTC

    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,

    ^_^

    Translate
    Steve SommersCorrect answer
    Legend
    Nov 30, 2015 6:02 PM UTC

    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.

    Translate
    WolfShade
    WolfShadeAuthor
    Legend
    Nov 16, 2015 3:30 PM UTC

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

    Translate