PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Javascript Templating, AngularJS and Roo.XTemplate

Note: This article was originally published at Planet PHP on 9 April 2012.
Planet PHP
Article originally from rooJSolutions blog
AOk, as I said earlier, a nice Easter break, meant I could get back to coding for fun. The other issue I had to deal with over the weekend was how to do the templating for the Javascript application.A
Having written a template engine for PHP, I can pretty much say that not using a template engine (which automatically escapes output) is essential for outputing html. It makes everything more maintainable, and reduces the risks that as a error prone human being, you are likely to open your application up to exploits.
Since most of the display logic on my applications is moving away from PHP generating html to PHP generating JSON, and the Javascript UI rendering this data and displaying it to the user. There has been one area I've slacked off on, that being sorting out a solution for using this JSON data from the PHP backend and rendering a elegant front end in html to show the end user.
Obviously this is not going to be indexed via google (Somehow JSON / Javascript interfaces are not very google friendly). But for applications that require authentication this not only helps scale the application under load, but also make it far easier to maintain.
My first effort to solve this issue was based on the original Template code in Roo (Roo.Template) however initially I decided to do the implementation in PHP. The concept was to take a group of 'html' template files, and use the PHP to convert those files into 'Javascript functions'. so calling the generated javascript function with 'data' as the argument, it would return the html to be rendered.
This basic implementation supported things like conditional inclusion, embedding Javascript, nested functions, looping and much more. It was however horribly crude and rather fragile if the template syntax was invalid. I used this for a few files relating to the ticket rendering in web.mtrack, however, I was never really happy with the solution.
Read on for my opinion / review of Anjular and the introduction to the updated Roo.XTemplate