Friday, May 23, 2008

Smart XSL Snippets: Mini Code Generators

Today we released dbQwikSite By most counts, this is a simple maintenance release but hiding in the list of updates there appears one rather mundane looking item entitled “xsl snippet support”. Looks pretty small and simple on the surface, but this single new feature infers some pretty powerful implications. It means that you can make mini-code generators inside the dbQwikSite generation framework.

Let’s take a closer look at this feature. We call it XSL code snippets. What it means that in any of the 150-plus custom code insert points found in Developer Edition, you can write an XSL(T) template. But this is not a template transforming xml data to format HTML in the browser. These XSL templates work a code generation time and they work against the XML of your project. This is really cool, because it means that you can access all the design information stored in your project model to generate code snippets. It takes a bit of time to wrap your mind around the concept but once you do the implications are quite interesting.

Let’s step back a bit. Let’s say we are working with Developer edition, which in itself is very powerful. What we can do is add in new script code to enhance our generated pages. We can do all kinds of neat things by typing in “static” script syntax into insert points / events. Ok, so we can understand adding code snippets to our pages. But what about a “smart snippet”? One that can write the code snippet for you. One that can know about other pages in your project, one that can react to the design setting contained in your project model. That’s exactly what the XSL snippets offer. And when you think about it, that’s exactly what the dbQwikSite code generation engines does, translates your design setting to code. But what’s extra cool about the XSL snippets is that it is you who defines what is to be generated, rather than the code generation engine itself. Now, that’s pretty advanced flexibility, and you won’t find this type of power in any competing tool. If you are lucky you may get “events” and then only a handful at best. With dbQwikSite you get over 150 “events” and you get smart snippets that can actually generate code themselves.

So why would we ever write a smart snippet. There are a number of situations that make smart snippets indispensible. The first one that comes to mind is to be able to have code snippets that is “project aware”, for example you may want to create code that adds new page flows, but without the names of the other pages, you could not so this, smart snippets can gather information from the project XML. Another situation is to make a snippet that is settings aware, for example you want it to create code differently if the page is secured or not, or if the group has a shopping cart. These are examples where smart snippets can outperform their “static” counterparts. You can write snippets that are not project specific, they are generic and self adjusting between projects. Another example could be a multi-scripting language snippet. For example rather than writing two snippets, one in ASP and one PHP and maintaining, managing and distributing both snippets, you have only one smart snippet, that automatically detects the generation language and inserts the correct language syntax.

Granted, writing smart snippets may not be for everyone. You can get along quite well inserting ordinary “static” script code into your insert points. To write a smart snippet, you need an understanding of the project XML and XSL as well as the code that you want to generate. But if you are into these technologies, you may be interested in a few of the details of the mechanics of XSL smart snippets. To create a smart snippet, you do as you would for any other kind of code snippet. But instead of typing in script syntax you type in an XSL template, and check the box that says this is a XSL snippet. During code generation, your XSL is executed and the output is placed into the insert point that invokes the snippet. Your snippet is passed the entire DOM of project XML, as well as two parameters. The two parameters are the Page ID and the Item ID (when applicable), giving you the context of the call to your XSL. You can easily work your way through the DOM to access Groups, Pages and other project objects to produce the code syntax you need.

That’s it, one small item in a maintenance release, the gives you an extremely powerful capability, a capability that you won’t find elsewhere. And even if you are not up to writing your own smarts snippets others will write and share smart snippets and you can benefit. This is yet another way that we are providing ways for the user community to contribute to the development of dbQwikSite. With our first step about a year ago providing an XML project model, to support for user defined project reports, addition of a plug-in architecture, user definable payment processing page generation, code snippets and now smart snippets. You can look forward to dbQwikSite becoming more powerful and more flexible with every release.

No comments: