Skip navigation
Currently Being Moderated

Where is the best place to put cfqueries?

May 16, 2012 8:09 PM

Where is the best place to put cfqueries, in the <head> </head> or before the html tag? Thanks

 
Replies
  • Currently Being Moderated
    May 16, 2012 10:22 PM   in reply to goodychurro1

    Its best to put it before the html tag.

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 12:20 AM   in reply to goodychurro1

    The "best" thing to do is to not mix your display logic with your business logic in the same file: fetching stuff from the DBs hould not be coupled to outputing something based on that fetched data.

     

    Read this lot:

    http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

    http://docs.model-glue.com/

    https://github.com/seancorfield/fw1/wiki

    http://stackoverflow.com/questions/3361951/picking-a-coldfusion-mvc-fr amework

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 5:17 AM   in reply to Adam Cameron.

    The best thing to do depends on the situation at hand.  There are times when Adam's suggestion makes sense.  There are others where it is overengineering.

     

    If you are going to have your query, processing, and display code all in one file, it is important that you run your query before you attempt to process the results and that you do any processing before you attempt to display it.  However, I don't think it really matters where your coldfusion code is with respect to any html tags.

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 7:09 AM   in reply to goodychurro1

    So before the doctype tag? Thanks

     

    I just doesn't really matter.  the doctype, head, body, etc only matter to the browser; the CF tags only matter to CF.  The browser doesn't see the CFML, and CF doesn't care about the mark-up one bit: it just ignores it.

     

    The best place - as I said - is not in the same file at all.  But if you insist on slapping your business logic in with your display logic, then the only consideration is that you need to set your variables before you use them.  Other than that: it simply doesn't matter.

     

    One consideration you might make is - even if it's all thrown into the same one file - at least separate the file itself into sections: have all your CFML code that gets your data at the top, and try to limit the CFML you have mixed in with the mark-up, eg: limit it to stuff like loops and conditionals.

     

    But, seriously, separate your concerns.  Dan's pretty much just wrong when he said it's over-engineering. It's just sloppy to not to write tidy, well-maintained, well-organised code.

     

    --

    Adam

     
    |
    Mark as:
  • Currently Being Moderated
    May 17, 2012 11:00 AM   in reply to goodychurro1

    goodychurro1 wrote:

     

    Where is the best place to put cfqueries, in the <head> </head> or before the html tag? Thanks

    In a separate CFM page or CFC then use, respectively, a cfinclude or cfobject tag to access the queries.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points