A few weeks ago, someone was asking some questions about XUL templates. In the process of answering, I had a revelation and finally understood completely how templates work. I also decided it was finally time to try to understand the template building code. This code is rather complicated, involving about 20 - 30 different data structures. I still don't understand what all of them are for, and I don't think it's possible that anyone ever will. I think the complexity comes from the code needing to be scalable for templates with lots of rules and conditions, and ensuring that only the necessary parts need to be reevaluated. Most templates only have a few consitions so I'm not convinced that the complexity is worth it. But then again, I wouldn't be able to tell for sure without ripping it all out and changing it, which I don't think I want to do.

I am, however, more sure that the template builder actually does work as it's supposed to. Any problems people have are caused by errors either in RDF or template syntax. Common problems include using the wrong RDF content type, using the different parts of the RDF triple incorrectly, or putting the wrong values or variables in the template itself.

Many people would agree that templates need some kind of debugging aid. This would involve two things. First, the simplest, would be to print template syntax errors on the Javascript console. This would catch some types of errors, but wouldn't be able to catch RDF usage errors. The second would be to have a way of enabling a log of what the template builder is doing, allowing one to determine where in the process things are going wrong. Debug builds of Mozilla already have such a feature, but the output is very cryptic and is intended more for someone who is debugging the template builder code itself. I like the idea of a template builder log, but I'm not sure where it would be stored. One possibility is to add an attribute 'debugtemplate' or somesuch and, if true, store a log in a string field in the template builder, accessible via script.

That said, I'm in the process of creating some patches for the template builder, one patch to print template syntax errors to the JS console, and another nice patch for the enhancement bug 86435.