什么是模板引擎? [英] What is a template engine?

查看:183
本文介绍了什么是模板引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Javascript模板引擎?

我一直在寻找javascript模板引擎和我不知道他们的目的是什么。有谁能解释我,就像我五岁那些是什么?前端开发人员可以使用这些吗?

I keep on stumbling upon javascript templating engines and I have no idea what is their purpose. Could anyone explain me like I were five what are those? Can front-end developer make use of these?

推荐答案

编写胖JS客户端时经常使用JavaScript模板引擎,或者使用像Node.js这样的服务器端js时的正常模板引擎

JavaScript template engines are often used when writing thick JS-clients or as "normal" template engines when using server-side js like Node.js.

不是通过使用字符串连接等生成HTML来混淆代码,而是使用模板并在其中插入变量。

Instead of cluttering the code by generating HTML using string concatenating etc., you instead use a template and interpolate the variable in them.

例如在下面的示例中,您将数组 foo 传递给模板引擎,然后模板引擎插入(用占位符$ {data}替换项目<来自数组foo的em> i :

For instance in the example bellow you pass in an array foo to the template engine which then interpolates (replaces the placeholder ${data}) with item i from the array foo:

//pseudo code
<table>
 <tr>
   for each variable data in foo
    <td>${data}</td>
   end
 <tr>
</table>

在backbone.js等框架中,您通常使用模板来实现MVC模式的视图部分。

In frameworks like backbone.js you typically use a template to implement the view part of the MVC pattern.

JS中有许多不同类型的模板引擎。有些是服务器端框架的直接端口,如ERB,HAML等。其他是专门用于JavaScript的开发,即 Pure

There are many different types of template engines in JS. Some are direct ports of server-side frameworks like ERB, HAML etc. Others are developed specially to work with JavaScript i.e Pure

关于模板引擎应该给你多少表现力的争论,因为在模板中放置逻辑通常是件坏事。因此,一些模板引擎专门设计为不允许您将逻辑放入其中,即 Moustache

There is a debate of how much expressiveness the template engine should give you because putting logic in your templates is usually a bad thing. Because of this some template engines are specifically designed to not let you put logic in them i.e Mustache

这篇关于什么是模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆