Skip to main content
Inspiring
May 26, 2010
Question

placing the <cfquery>

  • May 26, 2010
  • 2 replies
  • 411 views

Can you locate the <cfquery> anywhere in the code ?

I was thinking all my queries at the beginning of the page's code. Am I free to put them anywhere before the associated <cfoutput> tag ?

This topic has been closed for replies.

2 replies

Inspiring
May 26, 2010

Can you locate the <cfquery> anywhere in the code ?

I was thinking all my queries at the beginning of the page's code. Am I free to put them anywhere before the associated <cfoutput> tag ?

The answer to the question you are asking is "yes".  CF executes code line by line (well: tag by tag, or statement my statement), from the top of the template to the bottom.  The exception to this is that UDFs within a template are compiled separately, and will be available to code "earlier" in the template.  Also I think any evaluate() statements will still cause a template to be processed twice, once to work out what the evaluation is, and then once to process what was evaluated (don't quote me on that though).

However... the question you are framing implies a far-less-than-ideal approach to coding.  If you have your queries muddled in with your business logic and your presentation logic, you're reducing your ability to reuse your code.  What if you want to use the same query on a different page?  You'll need to duplicate it.  What happens if you use the same visual layout for a different page (which has a different data requirement)?  You have to duplicate it.  Or less obviously you might have the same business logic being applied to different data, or different views.

So what you should be doing is implementing your code in such a way that a query doesn't rely on business logic (although the business logic will be what calls the query) and your display logic.  In the middle of all that, you should have a controller which decides - for a given situation - which data the business logic will need, and then how the result of giving the business logic some data should be displayed.

What I recommend you do is to read up on MVC (http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), and then have a look at Model-Glue to organise your applications.  M-G looks more complicated than simply charging in and typing code, but it's pretty intuitive once one spends a couple of hours getting up to speed with it, then it greatly simplifies the code you write.  It's worth the upfront cost of learning how to use it.

Even if you don't run with Model Glue (or one of the other frameworks out there, although I could not recommend Fusebox), at least using MVC principles when rolling your own code is really a "must do".

--

Adam

Inspiring
May 26, 2010

thanks guys

really, the reason I asked the question was, I'm using includes in my code. At presetn the includes only contain logic (not the queries), and I was wondering if I could use a cleaner approach by including the query in the include.

I'll definitely read up on MVC and Model Glue

ilssac
Inspiring
May 26, 2010

You are.......

BUT

Most developers would prefer not to do this.  Seperating business logic from display logic, usually makes future modifications of one or the other sepeartly from each other much easier.