Ask a Jedi: Multiple questions on Application startup and templating

Just a quick note before I dive into this question. I’ve noticed lately I’ve been getting a few questions that cover stuff that I’ve answered before. I have no problems answering them again. Sometimes they cover things that really need repeating. I’m just commenting here as it seems like it has occurred a bit often lately, and if my readers are getting a bit miffed, I wanted to explain why. (As another FYI, I plan on running a survey like Todd is doing so you will have the opportunity to tell me exactly what you think of my blog.) Ok, on with the questions!

Tony asks:

Question 1 of 2: I am now transitioning into using Application.cfc (I know...I know...but better late than never) and am not terribly sure what to do with my CFC instances. To be totally honest, I'm trying to pretty much update ALL of my CF bad habits into any and all best practices that I can. So, up til now I've been invoking components where needed and that, of course, is not terribly great practice. What I want to do is create them in Application.cfc so I can just refer to them when needed sitewide. This, I know, is no new concept. I just need assistance with getting started with it. Which function within Application.cfc would be the best place to add createObject() reference? Can you clear this up for me? You have many posts that deal with Application.cfc issues, but I haven't tracked down the answer to my question so I thought I'd contact you directly.

Congratulations on moving to Application.cfc. You won’t regret it. What you want to use is the onApplicationStart method. It is run when your application starts and is the proper place for initialization code. Please see this specific blog post for more information: Application.cfc Methods and Example Uses

I normally just use cfinclude for master header and footer files (and additional function libraries). If I'm not using Model-Glue (or other framework) specifically, what is considered a good practice as far master templates are concerned? Or is it still ok to use cfinclude for master container files as long as CFCs are being leveraged correctly?

I think you have two questions here. Should I use cfincludes for layout and should I use a cfinclude for ‘master libraries’ (of UDFs I assume). Let’s cover layout. I don’t like using cfinclude for layout. Mainly because variables will leak in between them and for sites with complex layout, it is better to have some separation. Normally I recommend custom tags. See this article: ColdFusion custom tag for layout example.

As for UDF libraries, I don’t have a firm idea yet of what I consider best. You can easily do a cfinclude in your Application.cfc onRequestStart function. However, this will not place the UDFs in your variables scope. You can do this in onRequest, but onRequest has negative side effects. What I do sometimes is this - I include the library in onRequestStart, but the UDF declarations are copied to the request scope. So my UDF file may look like so:

<cfscript> function parisIQ() { return 0; } request.parisIQ = parisIQ;

function britneyIQ() { return -10; } request.britneyIQ = britneyIQ; </cfscript> </code>

Basically I just copy the UDFs into the request scope and then reference them as request.parisIQ() or request.britneyIQ(). This also had the side effect of making them easily available in custom tags as well.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments