简体   繁体   English

创建我自己的自定义JQuery模板引擎?

[英]Creating my own custom JQuery templating engine?

I had a quick search in stackoverflow.. but couldn't find anything quite what I was after. 我在stackoverflow中进行了快速搜索..但是找不到任何我想要的东西。

I am trying to understand/get some pointers on how to build my own VERY simple templating engine for jQuery. 我试图理解/获得一些关于如何为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). 我基本上有一个标准的AJAX调用来获取一些XML(是的......老式的我知道..我需要把它拉进JSON世界)。 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. 因此,对于基于SINGLE的记录,我有一些我称之为'PageDataMagic'(是的,非常戏剧性的......),使用jQuery迭代XML并将xml节点值映射到HTML元素(当然有一些检查)如果一个元素是一个复选框等。) - 这是由XML节点从数据库中调用字段名来完成的,所以只要元素的ID以这种方式命名,它就可以工作。

Ok, on to the point.. if I want to have some sort of 'template' ie preferably an external JS / HTML, how would I go about dynamically mapping the fieldnames and dynamically adding a repeating template? 好的,关键是..如果我想要某种'模板',即最好是外部JS / HTML,我将如何动态映射字段名并动态添加重复模板? I guess there is the append() function in jQuery? 我猜jQuery中有append()函数? 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.. 哦,是的,现在它必须是XML,因为其他一些限制..我知道我可以转换为JSON,并且无疑会使整个过程更容易..但我需要从XML运行,即使它必须解析那 - 慢一点..

使用官方的jQuery模板插件,而不是发明自己的。

I thought that was what XSLT was for? 我认为这就是XSLT的用途? And yes, more coffee. 是的,更多的咖啡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM