Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

laytpl

Package Overview
Dependencies
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

laytpl

Node.js view engine

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13
decreased by-7.14%
Maintainers
1
Weekly downloads
 
Created
Source

介绍 - Introduce

laytpl 是一款轻量的 JavaScript 模板视图引擎,能同时运行在浏览器端及 Node.js 端,且对 Express 框架无缝兼容,模版语法遵循原生 JavaScript。

安装 Installation

$ npm install laytpl

语句 - Grammar

  • 转义输出(即转义 HTML) : {{= d.field }}
  • 原文输出(即渲染 HTML) : {{- d.field }}

兼容性重要提示1.x 版本的 {{ d.field }} 等同于原文输出,因此若升级到 2.x,需按照新版分隔符进行语句适配。

  • 逻辑语句 : {{# JavaScript statement }}
  • 忽略解析 : {{! content !}} 如:
{{! 
  {{= d.key }} 则不会解析这中间的内容,原字符输出
!}} 

使用 - Usage

此处只演示 Express 下的使用方式

第一步

在项目入口指定 view engine,并且定义模版文件扩展名为 .html

var express = require('express');
var app = express();

var laytpl = require('laytpl');
app.engine('.html', laytpl.__express);
app.set('view engine', 'html');

第二步

在视图文件中书写模板,支持 include 导入子模板,文件扩展名可省略。

{{# if (d.user) { }}
  <h2>{{= d.user.name }}</h2>
{{# } >>
<p>{{= d.intro }}</p>

{{- include footer }}

第三步

渲染

app.get('/', function(req, res){
  res.render('index', {
    user: {name: 'sents'},
    intro: 'a developer from China'
  });
});

自定义分隔符 - Custom delimiters

Custom delimiters can also be applied globally:

laytpl.config({
  open: '{{',
  close: '}}'
});

缓存 - cache

laytpl 默认不开启缓存,这在你开发时非常有利。但是当你的模版足够稳定,你如果需要开启该项,只需要按以下设置即可:

laytpl.config({
  cache: true
});

开启缓存后的渲染速度将会得到极大的提升。

压缩 - min

laytpl.config({
  min: true
});

协议 - License

The MIT License

Keywords

FAQs

Package last updated on 10 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc