Skip to main content
Inspiring
November 30, 2009
Answered

How do you include the doctype in Application.cfc?

  • November 30, 2009
  • 3 replies
  • 769 views

I'm trying to convert an Application.cfm file to Application.cfc and in the cfm file I had the following:

<!--- Set the content type to ISO-8859-1, otherwise the default is UTF-8 --->
<cfcontent type="text/html; charset=ISO-8859-1">

<!--- Set the doctype for all the documents --->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

I think I set the cfcontent in the onApplicationStart function along with the other application variables.  No matter what I do, nothing is getting the doctype to show up on the pages. Where do you put that in the cfc file?

Thanks!

Holli

This topic has been closed for replies.
Correct answer ilssac

If you want OUTPUT from the OnRequestStart function, you must use OnRequest() and include the generated output in it.

Which really means that you could probably do all you want just in the OnRequest method, but that is up to you.

It is not that OnRequest() itself breaks webservices, ajax calls ect.  It is that OnRequest will be run for EVERY request including webservice request, ajax request ect.

Then, if the OnRequest method adds content, such as your heading content, to all requests no matter what, including webservices and ajax calls.  99.9% of the time, that added content is going to break the format and thus prevent the functionality of those web service and ajax requests.

This is not difficult to work around.

1) have a smarter onRequest method that understands when it is responding to webservice or ajax requests and don't provide content that will break them.

2) make your webservice and ajax resource inside a different directory with a different Application.cfc file with a different (or more likely no) onRequest method.

3) As Adam said, don't put content into your Application.cfc but use any number of other methods to apply this type of standard output to all views.

3 replies

Inspiring
December 1, 2009

Thanks! Yes, this is all starting to make sense now. Found this on another website in case someone stumbles across this post looking for answers.  This is a way to not run the onRequest function for cfc files that might be in the same directory:

<!--- ensure CFC / Web Service / Flex Remoting calls are not intercepted --->

<cfif right(arguments.targetPage,4) is ".cfc">

<cfset doCompile = false />

<cfset structDelete(variables,"onRequest") />

<cfset structDelete(this,"onRequest") />

</cfif>

Thanks again!

Inspiring
December 1, 2009

Thanks for your help, but I'm still missing something. According to this: http://www.coldfusionjedi.com/index.cfm?mode=entry&entry=ED9D4058-E661-02E9-E70A41706CD89724  UsingOnRequest will break flash remoting, web services and AJAX calls, so I don't think I want to go there. I put it in the onRequestStart function, but it's still not showing up on the page? I included it as is and wrapped it in cfoutput tags. Neither of these are working?

    <!--- Run before the request is processed  --->
    <cffunction name="onRequestStart" returnType="boolean" output="false">
        <cfargument name="thePage" type="string" required="true">
      <!--- Set the doctype for all the documents --->
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
        <cfreturn true>
    </cffunction>

    <!--- Run before the request is processed  --->
    <cffunction name="onRequestStart" returnType="boolean" output="false">
        <cfargument name="thePage" type="string" required="true">
      <!--- Set the doctype for all the documents --->
      <cfoutput><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml"></cfoutput>
        <cfreturn true>
    </cffunction>

ilssac
ilssacCorrect answer
Inspiring
December 1, 2009

If you want OUTPUT from the OnRequestStart function, you must use OnRequest() and include the generated output in it.

Which really means that you could probably do all you want just in the OnRequest method, but that is up to you.

It is not that OnRequest() itself breaks webservices, ajax calls ect.  It is that OnRequest will be run for EVERY request including webservice request, ajax request ect.

Then, if the OnRequest method adds content, such as your heading content, to all requests no matter what, including webservices and ajax calls.  99.9% of the time, that added content is going to break the format and thus prevent the functionality of those web service and ajax requests.

This is not difficult to work around.

1) have a smarter onRequest method that understands when it is responding to webservice or ajax requests and don't provide content that will break them.

2) make your webservice and ajax resource inside a different directory with a different Application.cfc file with a different (or more likely no) onRequest method.

3) As Adam said, don't put content into your Application.cfc but use any number of other methods to apply this type of standard output to all views.

ilssac
Inspiring
November 30, 2009

Its part of the request, so it would need to be in one of the request methods.

I would probably put it in OnRequestStart(), but it could also be incorperated into OnRequest() with the rest of the output if one choose to do so.

Inspiring
November 30, 2009

If one was to want to put it in Application.cfc, then onRequest() would be the place to put it, yeah.  I'd limit onRequestStart() to setting variables, not outputting anything.  OnRequest() (of the two) is kinda intended for the output.

However this doesn't sit very well with me. What if one of your requests is an RSS feed?

I'd put this sort of stuff on one of my view templates, and then use that view template for standard HTML requests, and a different template for RSS requests and the like.

--

Adam