Jade模板引擎让你飞
写在前面:现在jade改名成pug了
一.安装
npm install jade
二.基本使用
1.简单使用
p hello jade!
渲染后:
<p>hello jade!</p>
jade安装成功后,进入node命令使用。
2.jade.compile:编译字符窜
> var jade = require('jade')
undefined
> jade.compile('p hello jade!')()
'<p>hello jade!</p>'
3.jade.compileFile:编译jade文件
> var jade = require('jade')
undefined
> jade.compileFile('hello.jade')()
'<p>hello jade!</p>'
>
4.jade.render:渲染html
> jade.render('p hello jade!')
'<p>hello jade!</p>'
5.jade.renderFile:渲染jade文件
> jade.renderFile('hello.jade')
'<p>hello jade!</p>'
>
当jade全局安装后也可以直接使用jade命令。
6.jade filename
C:\Users\Administrator>jade hello.jade rendered hello.html C:\Users\Administrator>
7.jade -P filename 使html文件变得可读
修改hello.jade文件为:
doctype html
html
head
title hello jade!
body
p hello jade
运行:
jade hello.jade
jade.html文件变成这样:
<!DOCTYPE html><html><head><title>hello jade!</title></head><body><p>hello jade</p></body></html>
这样的可读性太差了,不过没事我们有P(pretty)参数
运行:
jade -P hello.jade
hello.html文件变成这样:
<!DOCTYPE html>
<html>
<head>
<title>hello jade!</title>
</head>
<body>
<p>hello jade</p>
</body>
</html>
这样看起来顺眼多了。
8.jade -w filename 监控文件
执行:
C:\Users\Administrator>jade -w hello.jade watching hello.jade
rendered hello.html
一旦我们修改jade文件,html文件会实时修改。此乃神技也,有点像supervisor。
三.常规用法
1.选择器的使用
p.bt.cn#dn
编译后
<p id="dn" class="bt cn"></p>
2.如果省略标签元素,默认是div
.bt.cn#dn
编译后
<div id="dn" class="bt cn"></div>
3.属性的使用
一般属性
div(color='red',font-size='1.5rem')
编译后
<div color="red" font-size="1.5rem"></div>
多个属性如果写一行觉得拥挤的话,可以分开写
div(color='red'
font-size='1.5rem')
style属性
a(style={color:'red'})
编译后:
<a style="color:red;"></a>
带有杠的CSS属性写法
a(style={'z-index':'11000'})
4.字符转义
使用=赋值会进行转义
div(href="https://www.baidu.com/s?wd=jade&ws=jades")
编译后:
<div href="https://www.baidu.com/s?wd=jade&ws=jades"></div>
& 发生了转义 &
使用!=不会转义
div(href!="https://www.baidu.com/s?wd=jade&ws=jades")
编译后:
<div href="https://www.baidu.com/s?wd=jade&ws=jades"></div>
数据库中的字符串这样:萱公子&青橙
很明显被转义了。
显示到前端页面如果继续使用 #{}这样的形式的话,输出的会是萱公子&青橙。肯定是不行的。
这时候,我们可以使用:!{}这样的形式
5.变量的使用
单个变量
- var code = 1;
p.bt #{code}
编译后:
<p class="bt">1</p>
对象
- var code = {z:1,q:2};
p.bt #{code.q}
编译后:
<p class="bt">2 </p>
字符串拼接
- var code = {z:1,q:2};
p(class='bt'+code.z) #{code.q}
编译后:
<p class="bt1">2</p>
6.流程控制语句
Case
- var i=0;
case i
when 0
div 变量为#{i}
when 1
div 变量为1
default
div 没有匹配项
编译后:
<div>变量为0</div>
For
- for(var i=0;i<2;i++)
div #{i} //注意缩进
编译后:
<div>0</div>
<div>1</div>
If...else
- var ifv = true;
if(ifv)
div 为真
else
div 为假
编译后:
<div>为真</div>
7.注释
html可见注释
//html可见注释
div.bt
编译后:
<!--html可见注释-->
<div class="bt"></div>
html不可见注释
//-html不可见注释
div.bt
编译后:
<div class="bt"></div>
多行注释(注意缩进)
//
div.bt
编译后:
<!--div.bt-->
条件注释
<!--[if IE 8]>
<html lang="en" class="ie8">
<![endif]-->
<!--[if IE 8]><!-->
<html lang="en">
<!--<![endif]-->
编译后:
<html lang="en" class="ie8">
<![endif]-->
<!--[if IE 8]><!-->
<html lang="en">
<!--<![endif]-->
8.include
doctype html
html
head
style
include style.css
body
script
include script.js
编译后:(一定要有这两个文件,不然jade会报错)
<!DOCTYPE html>
<html>
<head>
<style>p{
color:red;
}
</style>
</head>
<body>
<script>console.log(1)</script>
</body>
</html>
9.extends与block
layout.jade
doctype html
html
head
title hello jade!
body
block content block foot
business.jade
extends ./layout.jade block content
h1 content主体部分 block foot
h1 foot脚注部分
编译后:
busuness.html
<!DOCTYPE html>
<html>
<head>
<title>hello jade!</title>
</head>
<body>
<h1>content主体部分</h1> <h1>foot脚注部分</h1>
</body>
</html>
10.jade中写行内js或css
doctype html
html
head
style.
p{color:red}
body
script.
console.log(OK)
编译后:
<!DOCTYPE html>
<html>
<head>
<style>p{
color:red;
}
</style>
</head>
<body>
<script>console.log(OK)</script>
</body>
</html>
11.强大的Mixins
mixin templ_li(value)
li #{value}
ul
+templ_li('香蕉')
+templ_li('橘子')
编译后:
<ul>
<li>香蕉</li>
<li>橘子</li>
</ul>
这个特性让我们能自定义一些模板函数。特别是当我们的html结构有相似的时候。
其实跟less中的公共类,react中的公共函数也都是共通的。
less中:
.temp_color(@color:red){
color:@color;
}
//使用
p{
.temp_color(blank);
}
react中:
var temp_prop = {
getDefaultProps:function(){
return {name:'共有属性'};
}
}
//使用
var ComponentDib = React.createClass({
mixins:p[temp_prop ],
render:function(){
return <h1>{this.props.name}</h1>
}
})
Jade模板引擎让你飞的更多相关文章
- Jade 模板引擎使用
在 Express 中调用 jade 模板引擎 jade 变量调用 if 判断 循环 Case 选择 在模板中调用其他语言 可重用的 jade 块 (Mixins) 模板包含 (Includes) 模 ...
- jade模板引擎学习笔记(WebsStorm9.0.3+ nodejs+express+jade)
jade环境搭建 jade标签写法 jade注释 jade添加类名.id.属性 jade添加脚本,css jade变量 jade多行文本显示 jade流程代码:for,each,while jade流 ...
- Express框架之Jade模板引擎使用
日期:2018-7-8 十月梦想 node.js 浏览:2952次 评论:0条 前段时间讲说了ejs模板引擎,提到了jade的效率等等问题!今天在这里简单提一下jade的使用方式!结合expr ...
- Jade模板引擎使用详解
在 Express 中调用 jade 模板引擎 jade 变量调用 if 判断 循环 Case 选择 在模板中调用其他语言 可重用的 jade 块 (Mixins) 模板包含 (Includes) 模 ...
- Express全系列教程之(十):jade模板引擎
一.前言 随着前端业务的不断发展,页面交互逻辑的不断提高,让数据和界面实现分离渐渐被提了出来.JavaScript的MVC思想也流行了起来,在这种背景下,基于node.js的模板引擎也随之出现. 什么 ...
- nodejs jade 模板 引擎的使用方法
1.新建项目 2.使用模板引擎 app.set('view engine','jade'); 3.使用render渲染一个视图 res.render(用于指定需要被渲染的视图(必选),本地变量(可选) ...
- jade模板引擎
最近用jade写了点东西,觉得挺有趣的,是一个有意思的模板引擎. 比如说,像这样的结构的html <span> <i class="icon-edit">& ...
- Express开发实例(2) —— Jade模板引擎
前一篇通过helloworld,简单介绍了Express中的开发,本篇继续深入的学习express的模板. 关于Jade的用法,网上有很多,本篇参考:Jade语法 安装相关模块 在实验代码前,应该先安 ...
- 初次入坑jade模板引擎(一)
最近由于工作需要全栈开发,nodejs做后端,在写一个后台管理系统的时候,我一直在考虑用怎样的方式去写,尝试过依然采用前后端分离的结构.使用json数据进行数据交互的模式,但是尝试过才知道,真的很花时 ...
随机推荐
- SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...
- python自动化测试(2)-自动化基本技术原理
python自动化测试(2) 自动化基本技术原理 1 概述 在之前的文章里面提到过:做自动化的首要本领就是要会 透过现象看本质 ,落实到实际的IT工作中就是 透过界面看数据. 掌握上面的这样的本领 ...
- 算法笔记_013:汉诺塔问题(Java递归法和非递归法)
目录 1 问题描述 2 解决方案 2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...
- hadoop2.7之Mapper/reducer源码分析
一切从示例程序开始: 示例程序 Hadoop2.7 提供的示例程序WordCount.java package org.apache.hadoop.examples; import java.io.I ...
- Jvm 内存浅析 及 GC个人学习总结
从诞生至今,20多年过去,Java至今仍是使用最为广泛的语言.这仰赖于Java提供的各种技术和特性,让开发人员能优雅的编写高效的程序.今天我们就来说说Java的一项基本但非常重要的技术内存管理 了解C ...
- Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- 移动应用App测试与质量管理一
测试工程师 基于Html的WebApp测试, 现在一些移动App混Html5 HTML5性能测试 兼容性 整理后的脑图 测试招聘 弱化大量技术考察 看重看问题的高度 看重潜力 测试经验 质量管理 专项 ...
- “此网页上的某个 Web 部件或 Web 表单控件无法显示或导入。找不到该类型,或该类型未注册为安全类型。”
自从vs装了Resharper,看见提示总是手贱的想去改掉它.于是乎手一抖,把一个 可视web部件的命名空间给改了. 喏,从LibrarySharePoint.WebPart.LibraryAddEd ...
- VS2015墙内创建ionic2 【利用nrm更换源,完美!】
STEP 1 设置cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 一句话建立cnpm STEP 2 安装nr ...
- Android中Activity运行时屏幕方向与显示方式详解
现在我们的手机一般都内置有方向感应器,手机屏幕会根据所处位置自动进行横竖屏切换(前提是未锁定屏幕方向).但有时我们的应用程序仅限在横屏或者竖屏状态下才可以运行,此时我们需要锁定该程序Activity运 ...