简体   繁体   English

使用HTML元素作为“模板”还是在javascript中动态生成HTML更好?

[英]Is it better to use an HTML element as a “template” or to generate the HTML dynamically in the javascript?

I'm currently creating a "facebook-like" news feed, and I'm wondering if I'm better off having 1.) anelement within my HTML which I clone in my javascript and append the data I get from my ajax call to in the right places, or 2.) if it would be better to generate the whole element inside the javascript. 我目前正在创建一个“类似facebook”的新闻提要,我想知道是否最好在1.)HTML中添加元素(我将其克隆到javascript中并将从ajax调用中获取的数据附加到其中)在正确的位置,或者2.)如果最好在javascript中生成整个元素。

In terms of best practices, what is more generally acceptable and allow me the best extensibility? 在最佳实践方面,什么是最普遍接受的,并允许我获得最佳扩展性?

Is it possibly a situation based answer, or is there a clear answer in this case I should stick to across my site? 这可能是基于情况的答案,还是在这种情况下我应该在整个网站上坚持使用的明确答案?

A con to option 2 I see is that its generally not a good idea to be writing HTML in your javascript. 我看到的选项2的一个缺点是,用JavaScript编写HTML通常不是一个好主意。

A con to option 1 might be if I try to start manipulating the overall structure of the status so its different on different pages, its going to be hard to keep track and I could mess up things overall. 选项1的一个问题可能是,如果我尝试开始操纵状态的整体结构,以使其在不同页面上有所不同,那么将很难保持跟踪状态,并且我可能会弄乱整体情况。

I think the answer varies quite a lot on the situation. 我认为答案因情况而异。 It varies on the size and complexity of the template, etc. I tend prefer to use HTML templates from my HTML page (so I can use my authoring tools and such) and clone them. 它随模板的大小和复杂性等而变化。我倾向于使用HTML页面中的HTML模板(这样我就可以使用创作工具之类)并克隆它们。 I'm careful to avoid tying the JavaScript code to the specific structure of the HTML (instead preferring class names, name attributes, and/or data-xyz attributes as markers for where things go). 我要小心避免将JavaScript代码与HTML的特定结构绑定在一起(而不是将类名, name属性和/或data-xyz属性作为标记,以表示事情的发展)。 That lets you change the HTML (and use different HTML on different pages, with the same data) without worrying about blowing up your JavaScript code. 这样一来,您就可以更改HTML(并在具有相同数据的不同页面上使用不同的HTML),而不必担心炸毁JavaScript代码。 Just make sure the markers are there (or not, if you want to leave some information off on a particular page.) 只需确保标记在那里(或不,如果您想在特定页面上保留一些信息)。

I'll add a third option to your two: 我将在您的两个选项中添加第三个选项:

3) Have the ajax return an HTML fragment that you simply append to the page. 3)让ajax返回一个HTML片段,您只需将其附加到页面即可。

This last is most useful if you're already using some templating technology on the server-side to generate the initial parts of your feed, because then you can have the code in one place rather than two. 如果您已经在服务器端使用某种模板技术来生成提要的初始部分,则最后一个功能非常有用,因为这样您就可以将代码放在一个地方而不是两个地方。

Basically, you need to generate HTML from JavaScript in cases you have to create interfaces that differ depending on the configuration eg you need to build custom dynamic forms as a input projection of a composite model. 基本上,在必须创建根据配置而有所不同的接口的情况下,您需要从JavaScript生成HTML,例如,您需要构建自定义动态表单作为复合模型的输入投影。

In simple cases like yours HTML is cool. 在像您这样的简单情况下,HTML很酷。 But you may take a look at jQuery Template plugin or something similar for your template (if you are using one). 但是您可以看看jQuery Template插件或类似的模板(如果您正在使用的话)。

What I usually do when I have to create just one template, I echo it to the JavaScript with some json_encode analog, and put there some variables like {{name}}, {{date}} etc. In JavaScript I just do a replace function, but is kinda fast and dirty. 我通常只需要创建一个模板,然后用一些json_encode类似物将其回显到JavaScript,然后在其中放置一些变量,例如{{name}},{{date}}等。在JavaScript中,我只是执行替换操作功能,但有点快又脏。

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

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