gregs

helping me remember what I figure out

getPageContext

| 2 Comments

I have been meaning to try this out for ages. Rather than hack a <cflocation> and <cfinclude> function together to use within <cfscript>, I decided to give getPageContext a go to carry out these tasks. I read about
this ages ago (I think it may have even been an article by Charlie Arehart over at CFDJ) and it struck me as a neat way to handle <cflocation> and <cfinclude>.

Include

<cfscript>
getPageContext().include(“pathto/yourfile”);
</cfscript>

The flipside, this literally includes the template, so say you have a text file that you wish to include, then this will be spat out to the screen at this point. And you can’t save it in a variable so it can’t act as a
<cfsavecontent> substitute. It acts exactly like a <cfinclude>.

Re-direct

<cfscript>
getPageContext().forward(“/pathto/template”);
</cfscript>

This acts as a server re-direct, which means that it requires one less trip to the browser. <cflocation> simply informs the browser that it needs to load another page, which the browse dutifully requests. However the URL
doesn’t change and I haven’t managed to forward to a full qualified domain (i.e. no http://gregs.teacupinastorm.com/someFolder/).

If you do a <cfdump var=”#getPageContext()#” />, you can see all the other methods provided and delve deeper by exploring the other methods using for example: <cfdump var=”#getPageContext().getServletContext().getServletNames()#” />

2 Comments

  1. This is an old posting, but it helped me today! Thanks.

  2. Glad you found it useful. For a real in-depth look at getPageContext() have a look at this website, just in case you didn’t come across it.

Leave a Reply

Required fields are marked *.

*