简体   繁体   English

使用脚本标签作为模板系统

[英]Using Script Tags As A Template System

** Revised ** ** 修订 **

I made this basic example which I believe is evicence that JavaScript may be useful as it's own template engine: http://jsfiddle.net/julienetienne/u6akrx7j/ 我做了一个基本示例,我相信它可以证明JavaScript可能有用,因为它是自己的模板引擎: http : //jsfiddle.net/julienetienne/u6akrx7j/

<script>talk[0].text('Hello World!');</script>

It's just a simple example but as you can see there are many possibilities eg. 这只是一个简单的示例,但是如您所见,存在许多可能性。

  • It doesn't necessarily have to detect the tag nodes in that manner, it could do it by class, id. 它不一定必须以这种方式检测标记节点,它可以按类,id进行检测。 It is also possible to obtain the script node of the function, 也可以获取函数的脚本节点,
  • you could simply print variables like p('Page Title'); 您可以简单地打印p('Page Title')之类的变量;
  • A self closing list of elements could be similar to eg li('', menu); 元素的自闭列表可能类似于li('',menu);
  • And you could clearly build up other complex data sets as you can with any other common template engine. 您可以像使用任何其他通用模板引擎一样,清晰地建立其他复杂的数据集。

Before this edit I made the mistake of comparing it to PHP. 在进行此编辑之前,我犯了将其与PHP比较的错误。 I'm actually considering it more of an alternative to eg handlebars , underscore , moustache , dust and similar. 我实际上正在考虑将其更多地替代例如车把下划线胡须灰尘和类似物品

But before I get to excited I would really like to know if there are any issues in regards to using in this way. 但是在让我兴奋之前,我真的很想知道以这种方式使用是否存在任何问题。 (I'm not concerned with novice best practices). (我不关心新手最佳实践)。

The benefits of an organic template system seems quite apparent. 有机模板系统的好处似乎很明显。 The biggest advantage is that there is no syntax to learn and it's cleaner than seeing %{{foobar}}% like markings. 最大的优点是,无需学习语法,并且比看到%{{foobar}}%像标记更干净。

Considering my example is just a tiny minimalistic concept, please tell me the drawbacks of a system like this compared to common template engines. 考虑到我的示例只是一个很小的简约概念,请告诉我与普通模板引擎相比,这种系统的缺点。

Thanks 谢谢

It looks like it's a PHP-style templating system, but it is apparently not: In the fiddle the script parts could be written anywhere in the page. 看起来它是一个PHP样式的模板系统,但显然不是:在小提琴中,脚本部分可以编写在页面的任何位置。

The part that defines the position of the texts to be inserted into the DOM is defined by the line var talk = document.getElementsByTagName('div'); 定义要插入DOM中的文本位置的部分由var talk = document.getElementsByTagName('div'); and the structure of the HTML. 以及HTML的结构。

If you'd use more div than the ones already there the texts will appear at a totally different (and supposedly wrong) position. 如果您使用的div大于现有的div ,则文本将出现在完全不同的位置(并且可能是错误的)。

The disavantage will thus be, that you can't use the system independently of the underlying markup. 因此,造成的破坏是您不能独立于基础标记使用系统。 That makes it different from a use case in PHP, where you can echo the variables by defining their position in the markup. 这使其与PHP中的用例不同,在PHP中,您可以通过定义变量在标记中的位置来回显变量。

Have a look at Angular or similar front end frameworks, if you are looking for a way how to implement a templating system. 如果您正在寻找一种实现模板系统的方法,请看看Angular或类似的前端框架。

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

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