I'm using jQueryUI and jQuery-tmpl, but I believe the advice I'm looking for is broad enough it'd apply to any sort of templating library.
The problem I have is that I'll often have some sort of CRUD widget in my template
<script id="some-widget-template">
<div class="some-widget">
<input name="NameField"/>
<button id="some-widget-save">Save</button>
</div>
</script>
That leaves me with a very readable widget I can go back and edit, etc. Except that it is ugly. Using jQueryUI I can make the button very pretty, but it requires some Javascript. So in my corresponding Javascript controller, my code will look like this:
$("#some-widget-template")
.render( arrayOfDataObjects )
.appendTo("body");
$("#some-widget-save").button();
For that very simple example, this works fine. My controller, however, will quickly become cluttered the more elements that need transforming. It also feels like this should be the concern of the view / templating engine, not to mention having magic strings ("#some-widget-save") that degrade the maintainability of the code.
What's the best way to go about this? Surely I can't be the first person to run into this problem. I've hacked together jquery-tmpl so that before returning a jQuery object, it'll scan for a button element, and if it encounters it, to transform it to a proper button element. This seems slow, as it'll have to traverse each element in the template. Any ideas?
Edit: I found that when using CSS3, 90% of my problems were eliminated. When building for non-CSS3 compliant browsers, I make use of the factory method.