idea jade编译成html,11.如何使用 pug (jade) 作为 HTML 的模板

这篇博客介绍了Pug(前身为Jade),一种用于编写更简洁HTML的模板引擎。通过示例展示了如何在Webpack配置中使用Pug,并演示了include功能,允许包含子模板以增强代码复用。博客还提供了文件结构和安装步骤。
摘要由CSDN通过智能技术生成

首先肯定会问什么是 pug,如果是 nodejs 程序员的话,肯定听过 jade 吧,pug 就是 从 jade 改名过来的。

说白了,它就是可以让你以更好的语法来写 html 。

下面看看例子就会清楚的。

现在我们就要代替之前的 src/index.html 改用 pug 语法来写。

首先把 src/index.html 改名叫 src/index.pug

$ rename src/index.html src/index.pug

src/index.pug

doctype html

html(lang="en")

head

title= pageTitle

script(type='text/javascript').

if (foo) bar(1 + 5)

body

h1 Pug - node template engine

#root

#container.col

if youAreUsingPug

p You are amazing

else

p Get on it!

p.

Pug is a terse and simple templating language with a

strong focus on performance and powerful features.

上面的内容是从 pug 官方的示例中抄的,然后稍微改了一下。

webpack.config.js

...

module.exports = {

...

plugins: [

...

new HtmlWebpackPlugin({

template: './src/index.pug',

...

}),

...

],

module: {

rules: [

...

{ test: /\.pug$/, loader: ['raw-loader', 'pug-html-loader'] }

]

}

};

$ npm install --save-dev pug pug-html-loader raw-loader

这样基本没啥问题,来看下结果:

我们来试试 pug 的 include 功能,就是可以包含子模板。

src/index.pug

...

body

include includes/header.pug

...

src/includes/header.pug

h1 from header pug file

目录结构是这样的:

src

├── Root.js

├── app.js

├── app.scss

├── contact.html

├── contact.js

├── includes

│ └── header.pug

└── index.pug

结果:

先这样吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值