I am in the process of building an isomorphic JavaScript framework and I need some help deciding between two options for our templates.
I have two different strategies I am trying to choose between. I have both working and functionally either will be fine, but thing in particular I am trying to think about now is which one would be easier to grok and more accepted by developers/contractors we hire or community members we get to help out on open source pieces.
Note: both options using our jeff.js templating lib which is essentially JavaScript/JSON instead of HTML.
The basic idea here is to have Node functions that are used to either bind data on the server side or generate HTML for the client side. For ex:
repeat(model.questions, function (question) {
return div({ class: 'blah'}, question.title);
})
Which would generate this on the server:
<div class="blah">something 1</div>
<div class="blah">something else 2</div>
<div class="blah">one more thing 3</div>
Or this on the client:
<div class="blah" ng-repeat="question in questions">{{ question.title }}</div>
The way this works is that essentially the server side runtime and client side build process have different implementations of the repeat() function.
We can actually use the Angular Style witin our jeff.js templates like this:
div({ 'ng-repeat': 'question in questions' }, '{{ question.title }}')
which will generate the exact same HTML on the server and client as Option 1 above. For the client generation, the code is very simple since the markup is a match to the final client HTML. For the server, the jeff.js template engine has rendering handlers for all Angular tags that are binding in nature (i.e. ng-repeat, ng-bind, ng-show, etc.) and the server values are dynamically injected through a series of JavaScript evals. So, sort of cool...but sort of evil as well ;)
#2 is nice because it matches angular code closely. But, #1 seems to be testable, and be able to handle better sortBy / filter extensions.