HTML 5 template language for CFML applications

  Follow me: Follow Bruce Kirkpatrick by email subscription Bruce Kirkpatrick on Twitter Bruce Kirkpatrick on Facebook
Wed, Apr 17, 2013 at 5:10PM

What should a framework do and what should a language do?   CFML does have a good amount of built-in framework functionality already.  It gets confusing on what is language or not, doesn't it?   It seems that CFML's job is to merge a bunch of cool Java technologies that solve difficult web app problems and give them a simpler API.  

If the Railo CFML Engine choose to implement a simple, secure template language, it might not require them to write all of it or modify existing code very much.  It could be mostly wiring up something else that people use a lot - like just integrating another Java template engine and making it cachable.

ASP.NET has a variety of official view engines built-in, which I think are created using T4 (Text template transformation toolkit).  It might not be a high priority for the Railo project to do this, but I think it is a good feature to have a simpler, more secure way to program so that designers/marketing people can get more involved in making simple changes.  I don't plan on ever letting a designer login and make a change to a cfm or cfc file on my server even if that seems innocent on simpler files.  I have all that kind of access locked down through SSH tunnel and rsa-key protection and I deploy sourceless, so they would still need to deploy changes through the lead developer.  But letting them change html and txt files is safe enough and those could be stored on the server with direct access granted for a few file types.

How could this be implemented into the language?

If there was an easier way to extend Railo's parser/compiler with your own syntax and a limited sub-set of allowed features, that would be interesting.   I wouldn't consider trying to understand all that bytecode Java stuff and modify Railo myself, but if you could have some kind of API for mapping custom syntax to CFML language constructs, then you could get some better performance out of your template language by having the cfml parser / compiler do most of it for you.  
 
You could implement most of your custom template features as CFC based custom tags which carefully limit access to the rest of the system.   I think a view language shouldn't allow variable assignment, and there should be a white list for allowed functions/tags so that the compiler will throw an exception if an a feature is not allowed.  Control flow and operators would be something you could turn on/off and map to different symbols.  Like dateformat or dollarformat is ok, but filedelete or cfquery is not in a view.   Maybe you want to disable logic, but allow loops.  Maybe you don't want to allow multiple conditions in an if statement.   There are a lot of decisions to make when designing a template language.
 

I built my own template language for CFML

 
I set some unique requirements for myself when I made my own template language.  I wanted it to output fast, simple CFML code, but still support a variety of language features.   Currently, it supports loops, logic, expressions, variables, custom widgets to extend the language and automatic CFC application scope caching.  I also want to release it as a separate free open source project eventually.  The source code is around 3500 lines at the moment, which is a lot more involved then simple string replacement.  The parser tediously processes each character in the file 1 at a time to analyze HTML, language tokens, and translates them into a string of equivalent CFML code.  It is secure because it also validates and restricts access to data.  It is fast because the generated CFML code is stored as a cached CFC during start-up so that it is no slower then a function call.  
 
It was not easy for me to make this, but it was a fun challenge.  I want to have it integrated as a key feature of my Jetendo CMS application.  It would probably be 100 times faster if it could go straight to Java bytecode, but the compilation overhead is not that bad in the current implementation.  You can see examples of how it works and read more about my implementation details here:

HTML 5 Template Language for CFML Applications

 
Railo and ColdFusion convert source code directly into bytecode, which is the best way to build a language on top of the JVM.  Railo or another CFML engine can't pre-decide what every simple template language needs to do, but they might be able to accelerate and structure it better for you since they know more about the Java Virtual Machine (JVM).  It would be interesting to see what they could do to support other template syntax someday.
 
It would also be interesting if there was a built in function that could return a stream or an object of parsed CFML tokens that supports the same set of functionality as the current Railo parser.  This would make it easier to translate or validate CFML files.  PHP has this feature: http://php.net/manual/en/book.tokenizer.php
 
I think the best part about having a separate template language is that you can execute it with something else later.   Like I want to be able to render some of my templates in Javascript client-side.  Designers could skin the ajax frontend the same way as the backend.   Rendering client side will save server CPU resources and reduce network usage.  It's should be easier to switch from CFML to Javascript if the views are made compatible with both.

Bookmark & Share



Popular tags on this blog

Performance |