template

template

模块介绍

template模块中包含了模板相关功能。

名称类型参数返回值说明
htmlEscape方法html:stringstring将html字符串进行转义
create方法Template创建模板引擎对象
registerFilter方法name:string,call:(in:any,param:any)=>anyvoid注册过滤器

htmlEscape

htmlEscape函数的作用是进行 HTML 转义,以确保特殊字符不会在浏览器中被解释为 HTML 代码。这是防止 XSS(跨站点攻击)的常见措施之一。

HTML转义是指将HTML中特殊含义的字符(如,,,< 等)转换为它们的实体,以防止它们被浏览器解释为HTML代码。常见的HTML实体包括:>,&,"。如果不进行转义,这些字符就会被浏览器解释为 HTML 代码,从而导致复杂的 XSS 漏洞。

  • <转义为&lt;
  • >转义为&gt;
  • &转义为&amp;
  • "转义为&quot;
  • '转义为&#39;

语法

template.htmlEscape(html:string)

参数

  • html:HTML代码的字符串。

示例

import template from "template"
let html = `<script>alert('XSS');</script><b>This is bold text</b>`
let text = template.htmlEscape(html)
console.log(text)
//结果为:&lt;script&gt;alert(&#39;XSS&#39;);&lt;/script&gt;&lt;b&gt;This is bold text&lt;/b&gt; 

create

创建一个模板引擎对象

语法

template.create()

示例

import template from "template"
let textTmpl = template.create()

registerFilter

向模板引擎中注册过滤器

语法

template.registerFilter(name:string,call:(in:any,param:any)=>any)

参数

  • name:过滤器名称
  • call:回调函数,用于执行处理逻辑,并返回处理结果
    • in : 输入值
    • param : 携带的参数

示例

import template from "template"
let tmpl = `
<!DOCTYPE html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    <h1>{{title}}</h1>
    <p>{{body|demo:12}}</p>
</body>
</html>
`
//注册过滤器demo
template.registerFilter("demo",(i,p)=>{
    //i为body的值,p为参数12
    console.log(i,p)
    //返回值为13
    return i+p
})

let user ={
    title:"xx",
    body:1
}

let t = template.create()
t.fromString(tmpl)
console.log(t.execute(user))

Template

Template表示模板引擎对象,该对象包含以下方法:

名称类型参数必须返回值说明
fromString方法content:stringvoid加载字符串模板
fromFile方法file:stringvoid从本地文件系统加载模板文件
executeWriter方法w:Writer,data?:objectvoid生成最终结果,并且将结果输出到一个Writer中
execute方法data?:objectstring生成最终结果并返回

fromString

加载字符串模板

语法

tmpl.fromString(content:string)

参数

  • content:模板文件内容。

示例

import template from "template"
let tmpl = template.create()
let content = `
<!DOCTYPE html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    <h1>{{title}}</h1>
    <p>{{body}}</p>
</body>
</html>
`
tmpl.fromString(content)

fromFile

从文件系统加载模板文件

语法

tmpl.fromFile(file:string)

参数

示例

import template from "template"
let tmpl = template.create()
tmpl.fromFile("./index.html")

executeWriter

生成最终结果,并且将结果输出到一个Writer中

语法

tmpl.executeWriter(w:Writer,data?:object)

参数

  • w:输出流
  • data:填充模板的数据对象

示例

import template from "template"
import {fs} from "core"
let tmpl = template.create()
let content = `
<!DOCTYPE html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    <h1>{{title}}</h1>
    <p>{{body}}</p>
</body>
</html>
`
tmpl.fromString(content)
let data={
    title:"标题",
    body:"内容"
}
//创建一个本地文件
let file = fs.openFile("./xx.html",["create","readWrite"])
//将模板生成结果写入文件
tmpl.executeWriter(file,data)

execute

生成最终结果并返回字符串

语法

tmpl.execute(data?:object)

参数

  • data:填充模板的数据对象

示例

import template from "template"
import {fs} from "core"
let tmpl = template.create()
let content = `
<!DOCTYPE html>
<html>
<head>
    <title>{{title}}</title>
</head>
<body>
    <h1>{{title}}</h1>
    <p>{{body}}</p>
</body>
</html>
`
tmpl.fromString(content)
let data={
    title:"标题",
    body:"内容"
}
//生成结果
let result = tmpl.execute(data)
console.log(result)

模板语法

变量

你可以使用 {{ variable }} 语法来输出变量的值:

Hello, {{ name }}!

示例

import template from "template"

let tmpl = template.create()
let content = `<p>{{title}}</p>`
tmpl.fromString(content)
//生成结果
let result = tmpl.execute({title: "hello"})
console.log(result)
//<p>hello</p>

过滤器

过滤器用于修改变量的显示方式。过滤器可以通过符号 | 链接,并且可以链式调用:

{{ "hello"|capitalize|safe }}

内置过滤器包括:

字符串过滤器

  • capitalize - 将值的首个字母转换为大写。
  • lower - 将字符串转换为小写。
  • upper - 将字符串转换为大写。
  • title - 将字符串中每个单词的首字母转换为大写。
  • trim - 去除字符串首尾的空白字符。
  • striptags - 移除字符串中的所有HTML标签。

数值过滤器

  • add - 给原始值加上一个指定的增量。
  • sub (subtract) - 从原始值中减去一个指定的减量。
  • mul (multiply) - 将原始值乘以一个指定的乘数。
  • div (divide) - 将原始值除以一个指定的除数。

列表和字典过滤器

  • join - 使用指定的分隔符连接数组或列表中的元素。
  • length - 返回数组、列表或字符串的长度。
  • default - 如果值为空,则返回一个默认值。
  • first - 返回数组或列表的第一个元素。
  • last - 返回数组或列表的最后一个元素。
  • random - 从列表中随机选择一个元素。

日期和时间过滤器

  • date - 格式化日期对象。

JSON过滤器

  • json_encode - 将对象编码为JSON字符串。
  • json_decode - 将JSON字符串解码为对象。

其他实用过滤器

  • default - 如果值为假,则返回一个默认值。
  • slice - 从列表或字符串中提取一段子集。

条件语句

{% if user.is_active %}
Hello, {{ user.name }}!
{% endif %}

循环

{% for item in items %}
{{ item }}
{% endfor %}

模板继承

支持模板继承,这意味着你可以定义一个基础模板,其他模板可以继承并覆盖特定部分。

基础模板

基础模板定义了网站的通用结构。

<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
	<title>{% block title %}默认标题{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

子模板

子模板继承基础模板并覆盖块。

{% extends "base.html" %}

{% block title %}子页面标题{% endblock %}

{% block content %}
<p>这是子页面的内容。</p>
{% endblock %}

示例

import template from "template"

let tmpl = template.create()
tmpl.fromFile("./child.html")
//生成结果
let result = tmpl.execute()
console.log(result)
更新时间 9/28/2024, 8:21:49 AM