当前位置:Gxlcms > JavaScript > nodejs前端模板引擎swig使用教程

nodejs前端模板引擎swig使用教程

时间:2021-07-01 10:21:17 帮助过:10人阅读

这次给大家带来nodejs前端模板引擎swig使用教程,nodejs前端模板引擎swig使用的注意事项有哪些,下面就是实战案例,一起来看一下。

相对于jade,我还是更喜欢swig前端模板引擎,jade虽然语法简练高效了不少,但是在我这最大的问题是

他没有一个html该有的样子。。。

所以我还是决定使用swig,页面结构,样子都是熟悉的样子,使用起来顺手了许多。

很多朋友也在纠结二者的优缺点,这个根据需求因人而异吧

下面我们一起学习下swig这个前端模板引擎

swig的简单介绍

swig是JS模板引擎,它有如下特点:

  1. 支持大多数主流浏览器。

  2. 表达式兼容性好。

  3. 面向对象的模板继承。

  4. 将过滤器和转换应用到模板中的输出。

  5. 可根据路劲渲染页面。

  6. 支持页面复用。

  7. 支持动态页面。

  8. 可扩展、可定制。

一. swig的安装

  1. npm install swig --save

二.和express框架集成

app.js

  1. var express = require('express');
  2. var swig = require('swig');
  3. var path = require('path')
  4. var app = express();
  5. var port = process.env.PORT || 4000
  6. //设置swig页面不缓存
  7. swig.setDefaults({
  8. cache: false
  9. })
  10. app.set('view cache', false);
  11. app.set('views','./views/pages/');
  12. app.set('view engine','html');
  13. app.engine('html', swig.renderFile);
  14. app.listen(port);
  15. console.log('server is started at http://localhost:'+port);
  16. //index page
  17. app.get('/',function(req, res){
  18. res.render('index',{
  19. title:'首页 ',
  20. content: 'hello swig'
  21. })
  22. })

index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>{{ title }}</title>
  6. </head>
  7. <body>
  8. {{ content }}
  9. </body>
  10. </html>

然后测试运行

  1. node app.js

在浏览器输入:http://localhost:4000, 执行效果如下

浏览器运行.png

三.基本用法

1.变量

  1. {{ name }}

这里需要注意的是前后两端都要有空格,这样{{name}}写就会报错

2.属性

  1. {{ student.name }}

3.模板继承

Swig 使用 extends 和 block 来实现模板继承 layout.html

首先我们定义一个模板

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>{% block title %}{% endblock %}</title>
  6. {% block head %}{% endblock %}
  7. </head>
  8. <body>
  9. {% block content %}{% endblock %}
  10. </body>
  11. </html>

这个模板里面我们定义了三个block块,子模板可以对这三个block继承

然后我们写一个index.html继承这个模板

  1. {% extends './layout.html' %}
  2. {% block title %} index {% endblock %}
  3. {% block content %}
  4. <p>
  5. <h1>hello swig</h1>
  6. <p>
  7. {% endblock %}

注意我这里并没有复写{% block head %}{% endblock %}这个块

也就是说我们可以在layout.html模板页面里面定义很多block,而子页面可以有选择性的实现。

4.include模板

包含一个模板到当前位置,这个模板将使用当前上下文

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>{% block title %}{% endblock %}</title>
  6. {% include "./includes/head.html" %}
  7. {% block head %}{% endblock %}
  8. </head>
  9. <body>
  10. {% include "./includes/header.html" %}
  11. {% block content %}{% endblock %}
  12. </body>
  13. </html>

5.if判断

  1. { % if name === '郭靖' % }
  2. hello 靖哥哥
  3. { % endif % }

6.if-else判断

  1. { % if name === '郭靖' % }
  2. hello 靖哥哥
  3. { % elseif name === '黄蓉' % }
  4. hello 蓉妹妹
  5. { % else % }
  6. hello 欧阳峰
  7. { % endif % }

7.for循环

先上栗子:

  1. // arr = [1, 2, 3]
  2. { % for key, val in arr % }
  3. <p>{ { key } } -- { { val } }</p>
  4. { % endfor % }

for循环内置变量:

  1. loop.index:当前循环的索引(1开始)

  2. loop.index0:当前循环的索引(0开始)

  3. loop.revindex:当前循环从结尾开始的索引(1开始)

  4. loop.revindex0:当前循环从结尾开始的索引(0开始)

  5. loop.key:如果迭代是对象,是当前循环的键,否则同 loop.index

  6. loop.first:如果是第一个值返回 true

  7. loop.last:如果是最后一个值返回 true

  8. loop.cycle:一个帮助函数,以指定的参数作为周期

使用方法:

  1. // arr = [1, 2, 3]
  2. { % for key, val in arr % }
  3. <p>{{ loop.index }} -- {{ key }} -- {{ val }}</p>
  4. { % endfor % }

8.强大的内置过滤器

  1. add(value):使变量与value相加,可以转换为数值字符串会自动转换为数值。

  2. addslashes:用 \ 转义字符串

  3. capitalize:大写首字母

  4. date(format[, tzOffset]):转换日期为指定格式

  5. format:格式

  6. tzOffset:时区

  7. default(value):默认值(如果变量为undefined,null,false)

  8. escape([type]):转义字符

  9. 默认: &, <, >, ", '

  10. js: &, <, >, ", ', =, -, ;

  11. first:返回数组第一个值

  12. join(glue):同[].join

  13. json_encode([indent]):类似JSON.stringify, indent为缩进空格数

  14. last:返回数组最后一个值

  15. length:返回变量的length,如果是object,返回key的数量

  16. lower:同''.toLowerCase()

  17. raw:指定输入不会被转义

  18. replace(search, replace[, flags]):同''.replace

  19. reverse:翻转数组

  20. striptags:去除html/xml标签

  21. title:大写首字母

  22. uniq:数组去重

  23. upper:同''.toUpperCase

  24. url_encode:同encodeURIComponent

  25. url_decode:同decodeURIComponemt

使用方法:

例如我们要格式化一个时间,使用方法和linux上的管道命令非常像

  1. {{ birthday|date('Y-m-d') }}

大写首字母

  1. {{ name|title }}

9.set命令

用来设置一个变量,在当前上下文中复用

  1. {% set foo = [0, 1, 2, 3, 4, 5] %}
  2. {% for num in foo %}
  3. <li>{{ num }}</li>
  4. {% endfor %}

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

php生成随机数字、字母或数字字母混合的字符串

php图片裁剪与缩略图使用实例讲解

以上就是nodejs前端模板引擎swig使用教程的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行