Publish and Include
Often you come across a problem where an application needs to be able to change it’s behavior without reprogramming and where language limitations make it difficult to implement a solution using dynamic programming.
For example, lets say you would like to be able to allow administrators to add, edit and delete the fields displayed within a contact form. That is pretty straightforward. With enough metadata about each field, you can just loop through the field list dynamically and generate the appropriate HTML to display it. But what happens if you’d like client side Javascript and would like to take advantage of cfform’s ability to generate the code?
If you dynamically build HTML using a string, that works just fine, but you can’t include ColdFusion tags within the string (you can, but they won’t be processed). There are a number of ways of handling this including rewriting the functionality and using cfsavecontent, but sometimes a good solution is to build up the cfml for the form (string concatenation works for simple use cases – an application generator with a template and either XML metadata or a databased metabase works better for more complex implementations). Then publish the generated script to the server hard drive and at the appropriate time, just cfinclude that script and it will be processed just as if you had manually written it.
Generation can be a separate step but I usually build these kind of things into my admin which basically calls the publish method of the object’s service class on save and delete. My base service class has a simple publish method that does nothing, making it easy to overload the method for the few objects that require this kind of publishing with a script that actually implements the publishing. I just did one to allow a client to manage product attributes in an e-commerce software product line on a store by store basis.
These can also be extremely useful for improving performance as you can compile down a lot of processing that only needs to happen occasionally, but as always, beware of premature optimization!


There are no comments for this entry.
[Add Comment]