创建我自己的自定义 JQuery 模板引擎?
我在 stackoverflow 上进行了快速搜索..但找不到我想要的东西。
我正在尝试了解/获取一些关于如何为 jQuery 构建我自己的非常简单的模板引擎的指导。
我基本上有一个标准的 AJAX 调用来获取一些 XML(是的..我知道老式的..并且我可能需要将其引入 JSON 世界)。因此,我最终会说如下内容(只是一个简化的想法):
<rows>
<myFieldname1>some data</myFieldname1>
<myFieldname2>some data too</myFieldname2>
.. and so on ...
</rows>
因此,对于基于 SINGLE 的记录,我有一个称为“PageDataMagic”的东西(是的,非常戏剧性..),它使用 jQuery 迭代 XML并将 xml 节点值映射到 HTML 元素(当然,如果元素是复选框等,则需要进行一些检查。) - 这是通过将 XML 节点称为数据库中的字段名来完成的/因此只要 ID 的的元素都是这样命名的,它是有效的。
好的,说到重点..如果我想要某种“模板”,即最好是外部 JS / HTML,我将如何动态映射字段名并动态添加重复模板?我猜jQuery中有append()函数?只是我想尝试读取模板文件并创建该文件的行实例???
这些有道理吗???也许需要更多的咖啡...
非常感谢任何帮助...
大卫。
--- 编辑 ---
哦,是的,由于其他一些限制,现在它必须是 XML.. 我知道我可以转换为 JSON,并且毫无疑问会使整个过程更容易.. 但我需要从XML,即使它必须解析 - 有点慢..
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 官方 jQuery 模板插件,而不是自己发明。
Use the official jQuery template plugin instead of inventing your own.
我认为这就是 XSLT 的用途?是的,更多咖啡。
I thought that was what XSLT was for? And yes, more coffee.