在页面上存储 HTML 模板的最佳实践?
我正在为我正在构建的网站开发一个相当重的 JavaScript 界面,并且我决定使用(最近正式发布的)jQuery 模板插件,用于从我的查询到 JSON API 生成我的元素。现在,我目前遇到的问题是:
我周围很容易有一堆这样的模板。一个用于每种类型的对象,一些用于列表,一些用于页面的各个部分,等等。是否有存储这些模板的首选方式?我读过有关使用模板名称定义带有 id
的 标记,然后从那里检索文本(如 John Resig 在“JavaScript 微模板"),但是有一堆
那么,问题是:对于这种情况有没有“最佳实践”?
I'm developing a rather heavy JavaScript interface to a site I'm building, and I've decided to use the (recently made official) jQuery templates plugin to generate my elements from my queries to a JSON API. Now, the problem I'm currently having is:
I'm prone to have a bunch of these templates around. One for each kind of object, some for lists, some for sections of the page, etc. Is there any preferred way of storing these templates? I've read about defining a <script>
tag with an id
using the template name, and then retrieving the text from there (as John Resig describes in "JavaScript Micro-Templating"), but having a bunch of those <script>
tags in every single page looks a bit hackish.
So, the question is: is there any 'best practice' for this kind of scenario?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么不直接将模板加载到隐藏元素中,然后使用
$(template).clone()
呢?这可以避免很多令人头痛的事情。我无法谈论.clone()
与方法的性能,但我可以告诉您,额外的脚本标记会降低性能,而且一般情况下让代码变得混乱。
为什么?
即使你有一个时髦的转义实用程序,错误仍然会发生,而且它们会更难阅读。另外,如果您必须在团队环境中工作,特别是当人们滚动/滚动项目时,这可能不容易理解。
您可以创建一个指针对象,例如
templates = {}
,并使用指向可克隆元素的 jquery 对象指针加载它,如下所示:<前><代码>模板= {};
templates.message = $("#templates.message");
templates.dialog = $("#templates.dialog");
现在您拥有的一切创建新模板的方法是:
您可以创建一个包装函数来接受参数,然后使用 newDialog.find("title").html(title)、newDialog.find("message") 添加这些参数。 html(message), 等。
同样,从性能角度来看,我不知道哪个更快,因为我现在没有时间测试。但我知道,当处理足够大、需要模板的代码库时,使用认可的方法通常会更好……最终不可避免的是其他人会参与其中,而且你需要解释的事情越多,你的时间就越少做你自己的糟糕编码。
性能的另一个重要方面是计时...如果您遇到性能延迟,您应该确保使用 setTimeout 来分解冗长的可执行代码块。它创建一个新堆栈,并让浏览器绘制在创建新堆栈之前已经处理过的内容。当处理作为性能问题一部分的视觉延迟时(通常是最常见的性能问题,尤其是非编码人员),这非常有帮助。如果您想了解更多信息,请给我发消息,我会收集一些资源。现在时间有限,正在扑灭工作中的火灾。 :)
Why not just load your templates in a hidden element and then use
$(template).clone()
? That saves a lot of escaping headaches. I can't speak to performance on.clone()
vs the<script>
approach, but I can tell you that extra script tags are a performance penalty and just generally make code muddy.Why?
Even though you have a snazzy escaping utility, mistakes will still be made, and they'll be that much harder to read through. Plus if you have to work in a team situation, especially if people roll on/roll off the project, it may not be easily understandable.
you could create a pointer object, say,
templates = {}
, and load it up with jquery object pointers to your clone-able elements like so:now all you have to do to create a new template is:
You could make a wrapper function to accept params, and then add these with
newDialog.find("title").html(title), newDialog.find("message").html(message),
etc.Again, from a performance perspective, I don't know which is faster, as i don't have time to test right now. But I know that using sanctioned methods is generally better when working on a codebase large enough to require templates... its inevitable that other people will be involved eventually, and the more you have to be around to explain things, the less time you have to do your own badass coding.
Another important aspect of performance is timing... if you are experiencing performance delays, you should make sure to break up your chunks of lengthy executable code by using setTimeout. It creates a new stack and lets the browser paint what it's already processed up to the point just before the new stack is created. This helps immensely when dealing with visual delays as part of a performance issue (typically the most common performance issue noted, especially by non-coders). If you want some more info on that, send me a message and I'll gather up some resources. Limited time right now, putting out fires at work. :)
我最近做了这个练习,虽然
标签方法看起来有点黑客,但我接受了这个作为很好的解决方案并选择了我自己的版本(John Resigs 版本显然非常好,但我 中的
我的版本是一个基本模板,并使用
form
##VALUE##
TEMPLATE 可替换值进行替换: (单个项目模板或行模板)
内容构造函数:
用法
内容将从函数返回和/或者您可以设置appendTo参数设置
elementID
自动附加
内容。 (将null
设置为不append
)可替换值仅作为动态对象添加,其中
对象
名称是可替换项。注意:
我已经注释掉了
//initializePageElements();
,这是用来初始化模板上的jQuery
插件、样式的。将内容插入 DOM 时似乎只有内联样式才有效
I have recently done this excercise and although the
<script>
tag approach seems a bit hackish, I accepted this as good solution and opted for my own version (John Resigs version is obviosly really good, but I went for my own easily understandable for my own brain version)My version is a basic template and replacement using replacable values in the
form
##VALUE##
TEMPLATE: (single item template or row template)
CONTENT CONSTRUCTION FUNCTION:
USAGE
The content will be returned from the function and/or you can set the appendTo parameter set the
elementID
to automaticallyappend
the content. (setnull
to notappend
)The replacable values are just added as a dynamic object where the
object
names are the replaceable items.note:
I have commented out
//initializePageElements();
, this is used to initializejQuery
plugins, styles on the template.Only inline styles seem to work when inserting the content into the DOM
看看 https://github.com/inspiraller/STQuery
STQuery 使用与jquery,但它不是在 dom 上操作元素,而是直接在字符串上工作:
这使逻辑与模板分开,即不引人注目的 html 模板。
相对于 jquery 的优点是 STQuery 不必首先写入 DOM,这意味着您可以使用 Web Worker 或在服务器上执行所有模板操作。注意:截至此消息,stquery 仅用 JavaScript 编写,因此需要转换为所选的服务器端语言。
look at https://github.com/inspiraller/STQuery
STQuery uses all of the same method names as jquery but instead of manipulating elements on the dom, it works directly on the string:
This keeps logic seperate from the template, ie unobtrusive html templates.
The advantages over jquery is that STQuery doesn't have to write to the DOM first, which means you could use web workers or do all your template manipulation on the server. Note: as of this message, stquery has only been written in javascript so would need to be converted to the server side language of choice.
在过去的一年里,我为此尝试了许多不同的技术,我同意
script
技巧感觉有点不对,但我认为这是我们拥有的最好的技术。页面加载时需要出现的任何模板都必须是 DOM 的一部分。任何将它们存储在其他地方并加载它们的尝试都会产生明显的滞后。
我编写了一些模板帮助程序,允许我在页面上插入必要的模板,同时仍然可以在需要时通过 AJAX 延迟加载它们。我在页面加载时将页面上的所有模板加载到变量中,这使得引用更容易一些。
我当然想听听其他人是如何解决这个问题的。
I've tried many different techniques for this over the past year, and I agree that the
script
trick feels a bit wrong but I think it's the best we have.Any template that needs to be present when the page loads has to be part of the DOM, period. Any attempt at storing them elewhere and loading them creates a noticeable lag.
I've written some template helpers that allow me to insert the necessary templates on the page, while still making it possible to lazy-load them via AJAX when needed. I load all of the templates on the page into a variable on page load, which makes it a little easier to reference.
I would certainly like to hear how others have tackled this issue.
我在我的框架中成功使用了脚本 jquery 纯 html 模板
关于最佳实践,请看一下 Twitter 的新界面,所有内容都使用一个页面,并且仅通过“#!/paths”进行内部路由。
使用单页网站接缝是 Web 应用程序的下一个重大步骤,我个人喜欢将其称为 web 3.0 :) 它还将消除每个页面上拥有所有相同模板的问题 - 您将拥有只有一页。
I used script successfully with my framework jquery pure html templates
Regarding of the best practices, please have a look on the new interface of twitter, using one page for everything and only routing internally via "#!/paths".
Using single page website seams to be the next big step for web applications, personally I like to call it web 3.0 :) It will also eliminate problem with having all the same templates on each page - you will have only one page.
在 asp.net mvc 中,我经常将常用模板打包在部分视图中,我可以将其包含在需要的地方。这样我就不需要到处重复我的模板。
另一种方法是将模板放在单独的文件中。就像 newslist.tmpl.html 一样,它允许您使用 ajax 加载模板。考虑到这可以与数据加载并行完成,它不会减慢应用程序的速度,因为无论如何它很可能比数据调用更快。
In asp.net mvc I often pack my common templates in a partial view which I can include where I need it. That way I do not need to repeat my templates all over the place.
Another approach is to have your templates in a separate files. Like newslist.tmpl.html which allows you to load the template with ajax. Considering this can be done in parallell with the loading of data it shouldn't slow down you application, as it most likely is faster than the data call anyway.