Skip to main content
May 17, 2012
Answered

Where is the best place to put cfqueries?

  • May 17, 2012
  • 3 replies
  • 1191 views

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

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    So before the doctype tag? Thanks


    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

    3 replies

    BKBK
    Community Expert
    Community Expert
    May 17, 2012

    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.

    Inspiring
    May 17, 2012

    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-framework

    --

    Adam

    Inspiring
    May 17, 2012

    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.

    May 17, 2012

    So before the doctype tag? Thanks

    adamaas
    Known Participant
    May 17, 2012

    Its best to put it before the html tag.