node前端开发模板引擎Jade的入门

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

node前端开发模板引擎Jade的⼊门
随着 web 发展,前端应⽤变得越来越复杂,基于后端的 javascript(Node.js) 也开始崭露头⾓,此时 javascript 被寄予了更⼤的期望,与此同时 javascript MVC 思想也开始流⾏起来。

为了使⽤户界⾯与业务数据(内容)分离,就产⽣了『模板引擎』这个概念。

说的简单点,模板引擎就是⼀个字符串中有⼏个变量待定,通过模板引擎函数把数据动态的塞进去。

今天我们就来聊⼀聊 Jade 的使⽤⽅法和语法说明。

Jade官⽹:
Jade 命令⾏⼯具
Jade 的使⽤需要依赖 Node环境,通过 npm 包进⾏安装 Jade 命令⾏⼯具,安装成功之后就可以新建⼀个⽂件,⽂件后缀名为*.jade。

我们就可以尽情的使⽤ jade 的语法咯,写完之后只需要通过命令⾏⼯具进⾏编译即可编译为我们平时使⽤的 html 静态
⽂件。

安装⽅法
1、⾸先确定是否安装有 Node 环境和 npm ⼯具,查看⽅法如下:
在命令⾏⼯具中执⾏如下代码:
node -v
=> v0.10.35
npm -v
=> 1.4.28
// 如果成功返回版本号信息即为已成功安装 Node 环境。

2、通过 npm 全局安装 Jade 命令⾏⼯具
npm install jade -g
// mac⽤户可能需要管理员权限,使⽤如下命令
sudo npm install jade -g
3、创建 *.Jade ⽂件,开始任务。

4、通过使⽤ Jade 命令⾏⼯具将 jade ⽂件编译为 html ⽂件
Jade 命令⾏⼯具使⽤⽅法
我们可以通过 jade --help 查看 Jade 命令⾏⼯具的使⽤参数
jade --help
Usage: jade [options] [dir|file ...]
Options:
-h, --help output usage information / 输出使⽤信息
-V, --version output the version number / 输出版本号信息
-O, --obj <str> javascript options object / 传输到 jade ⽂件中的数据对象
-o, --out <dir> output the compiled html to <dir> / 输出编译后的 HTML 到 <dir>
-p, --path <path> filename used to resolve includes / 在处理 stdio 时,查找包含⽂件时的查找路径
-P, --pretty compile pretty html output / 格式化编译 html ⽂件
-c, --client compile function for client-side runtime.js / 编译浏览器端可⽤的 runtime.js
-n, --name <str> The name of the compiled template (requires --client) / 编译模板的名字
-D, --no-debug compile without debugging (smaller functions) / 关闭编译的调试选项(函数会更⼩)
-w, --watch watch files for changes and automatically re-render / 监听⽂件改变并⾃动刷新编译结果
--name-after-file Name the template after the last section of the file path (requires --client and overriden by --name)
--doctype <str> Specify the doctype on the command line (useful if it is not specified by the template) / 在命令⾏中指定⽂档类型(如果在模板中没有被指定) Examples:
# 编译整个⽬录
$ jade templates
# ⽣成 {foo,bar}.html
$ jade {foo,bar}.jade
# 在标准IO下使⽤jade
$ jade < my.jade > my.html
# 在标准IO下使⽤jade
$ echo 'h1 Jade!' | jade
# foo, bar ⽬录渲染到 /tmp
$ jade foo bar --out /tmp
实例:
// ⽐如说我们需要编译index.jade⽂件,默认编译到同⽂件夹下的同名html ⽂件中
jade index.jade
// 如果我们要格式化输出 index.html ⽂件,只需要添加 -P 参数即可
jade -P index.jade
// 如果我们要实现监听和⾃动编译,需要使⽤ -w 参数
jade -P -w index.jade
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

相关文档
最新文档