美文网首页前端
pug模版学习(二)

pug模版学习(二)

作者: 幸福镰刀 | 来源:发表于2016-11-29 15:04 被阅读114次

pug.compile(source, ?options)

返回一个编译函数,可把数据传入

const pug = require("pug")

var fn = pug.compile("p this is context")
console.log(fn())

var fn2 = pug.compile("p this is #{name}")
console.log(fn2({name: "pug"}))

编译结果:

<p>this is context</p>
<p>this is pug</p>

pug.compileFile(path, ?options)

同上,不过传入的是文件

// temp.pug
p #{name}'s pug code!
const pug = require("pug")
var fn = pug.compileFile("temp.pug")
var html = fn({
  name: "Tom"
})
console.log(html)

编译结果:

<p>Tom's pug code!</p>

pug.render(source, ?options, ?callback)

返回html字符串,这个回调是同步的

var html = pug.render("p.foo bar")
console.log(html)

var html2 = pug.render("p.bar My name is #{name}", {name: "Tom"})
console.log(html2)

编译结果:

<p class="foo">bar</p>

<p class="bar">My name is Tom</p>

pug.renderFile(path, ?options, ?callback)

同上,不过传入的是文件路径

// temp.pug
p #{name}'s pug code!
const pug = require("pug")

var html = pug.renderFile("temp.pug", {name: "Tom"})
console.log(html)

编译结果:

<p>Tom's pug code!</p>

相关文章

  • pug模版学习(二)

    pug.compile(source, ?options) 返回一个编译函数,可把数据传入 编译结果: pug.c...

  • pug模版学习(一)

    标签 按照html的缩进格式 编译结果: 文本 编译结果: 属性 设置class名跟id名(默认是div) 编译结...

  • koa2 pug引擎markdown动态渲染

    问题描述 koa2默认用pug模版,可是pug的filter是在预编译的时候渲染的,不能动态渲染 在pug页面直接...

  • pug中使用coffee-script

    在使用express默认模版 pug(jade) ,想要在内使用coffee需要在 package.json中安装...

  • pug在Vue中的使用

    安装pug pug :安装pugpug-loader:pug的loaderpug-cli:pug 编译工具pug-...

  • Pug学习

    1. 理解 Pug是一款健壮、灵活、功能丰富的HTML模板引擎,专门为 Node.js 平台开发。Pug是由Jad...

  • vue中使用pug 和 stylus

    pug的使用 先安装node环境 (1)安装支持pug依赖:npm install pug pug-loader ...

  • pug 文档

    yarn add pugyarn add pug-cli pug tem.pug -w //watch...

  • jade 升级为 pug

    一、pug原为jade,后来更名了。 二、pug的安装 1、首先在node.js中下载安装node和npm,然后要...

  • webstorm jade模板中的字符串模板无法解析

    jade因版权问题已改名为pug 需要安装一下 npm install pug npm install pug-cli

网友评论

    本文标题:pug模版学习(二)

    本文链接:https://www.haomeiwen.com/subject/bdwapttx.html