3

I had a quick search in stackoverflow.. but couldn't find anything quite what I was after.

I am trying to understand/get some pointers on how to build my own VERY simple templating engine for jQuery.

I basically have a standard AJAX call to get some XML (yes.. old fashioned I know.. and I will need to pull this into the JSON world perhaps). So I end up with say something like the following (just a cut down idea):

<rows>
    <myFieldname1>some data</myFieldname1>
    <myFieldname2>some data too</myFieldname2>
    .. and so on ...
</rows>

So, for SINGLE based records, I have something I call 'PageDataMagic' (yes, very dramatic..) which, using jQuery, iterates through the XML and maps the xml node values to HTML elements (there is a bit of checking of course if an element is a checkbox etc..) - this is done by the XML nodes being called the fieldnames from the database / so as long as the ID's of the elements are named that way, it works.

Ok, on to the point.. if I want to have some sort of 'template' i.e. preferably an external JS / HTML, how would I go about dynamically mapping the fieldnames and dynamically adding a repeating template? I guess there is the append() function in jQuery? just I guess trying to read a template file and create row instances of that???

Does any of that make sense??? Maybe more coffee is needed...

Any help though very appreciated...

David.

--- EDIT ---

Oh yes, and right now it has to be XML because of some other constraints.. I know I could convert to JSON, and would make the whole process easier no doubt.. but I need to run from XML even if it has to parse that - bit slower..

Dav.id
  • 2,757
  • 3
  • 45
  • 57
  • Ok, so I found http://stackoverflow.com/questions/2669787/how-do-templating-engines-in-javascript-work just to get a start.. but any further help anyone can add? – Dav.id Dec 15 '10 at 21:59
  • I'm confused on two accounts: You've got a list of resources on the linked question that could help you already without reinventing the wheel, and your question is a tad confusing. What exactly would you like help on? How do you append a new template line to the recordset? – jcolebrand Dec 15 '10 at 22:17
  • Yes it was quite early this morning... given the response, I didn't make myself all that clear... anyway, I know about XSLT and the official JQuery plugin that Microsoft helped with, however I was really wanted to understand 'how' a templating engine would work - so mainly a learning exercise.. – Dav.id Dec 16 '10 at 04:55
  • Well I think the resources from Resig would be good enough on how one would set about doing something like this. – jcolebrand Dec 16 '10 at 05:20

2 Answers2

2

Use the official jQuery template plugin instead of inventing your own.

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
  • Thanks. Again, yes wasn't all that clear. My need is 1) as a learning exercise on how to build such a function and 2) The official JQuery plugin doesn't seem to run from XML only JSON?? probably wrong, but that was my understanding? – Dav.id Dec 16 '10 at 04:58
1

I thought that was what XSLT was for? And yes, more coffee.

Nathan
  • 3,842
  • 1
  • 26
  • 31
  • Darn right.. early start, little coffee = not good. Anyway I realise using XSLT would be a definite way to go, and I do have access to a nice client side XSLT processor from dhtmlx.com, however as mentioned above, I didn't express that I wanted to use this as a learning exercise too - to understand how one would build such a function. – Dav.id Dec 16 '10 at 05:00