I’m trying to convert a rails app I wrote to CFML.
Rails allows you to have ‘helper’ methods that return back html. You can pass parameters or collections to said methods.
Is it bad practice to use a cfc method in this way? To return html? Should I only be returning queries and other non-public(not correct term, but…) facing entities?
Thanks for any advice.
I’ll give you the stereotypical answer first… it depends
Until recently I was under the same belief, and this comes from the “horses mouth” at Adobe, that CFCs should be pure data unless you’re dealing with things like Email. In that case I guess you really can’t avoid using it.
They’ve always pushed the idea that you should use Custom Tags for populating and presenting HTML. That way you can wrap up your “views” into modular pieces of code and reuse these as and when you need.
Custom Tags aren’t bullet-proof though and breaking encapsulation on them is far too easy at times.
So I would say yeah you’ve got a good approach if you want to go down the route you’re thinking and Custom Tags but it’s not the “golden” answer.
Personally I’m now starting to look at creating “UI” CFCs. So for example things like jQuery tools like datagrids I’ve got a set of reusable CFC that I can call on, pass in an array, query etc. and it gives me back populated HTML / JS code for it.
For general HTML stuff like templates I tend to use an MVC framework nowadays. Tools like ColdBox have a nice way of separating all this stuff out so they’re worth looking at.
Oh and BTW ColdBox has helpers as well so it’s interesting to see Rails influences in it, thanks
Just found this article from ben nadel
http://www.bennadel.com/blog/552-ColdFusion-Query-Maintains-Current-Row-Even-When-Passed-By-Reference.htm, and this is exactly what i’m wanting to do… but i can’t get this to work if the function isn’t on the same page (in a external cfc).
Instead of just calling the method like a traditional CF function, wouldn’t you do something like