How can nunjucks be used?


Basic use

const nunjucks = require('nunjucks')

// nunjucks.configure({ autoescape: true });
// const result = nunjucks.renderString('Hello {{ username }}', { username: 'James' });
// console.log(result)

//The view relative path here is affected by the node execution path
nunjucks.configure('views', { autoescape: true });
// var result = nunjucks.render('index.html', { foo: 'bar' });
var result = nunjucks.render('login.html');


{% extends "layout.html" %}

{% block body %}
This is the landing page
{% endblock %}

{% block script %}

  window.alert('hello login')

{% endblock %}


  {% block style %}
  {% endblock %}

  {% include "header.html" %}
  {% include "sidebar.html" %}
  {% block body %}
  {% endblock %}
  {% include "footer.html" %}
  {% block script %}
  {% endblock %}

There are also header, sidebar and foot, which are public styles in the layout. Block follows the principle of “one radish, one pit”, and writes its own special parts.

OK! Use this template engine to write a page!

Recommended Today

Configure Apache to support PHP in the Apache main configuration file httpd.conf Include custom profile in

In Apache’s main configuration file / conf/ http.conf Add at the bottom Include “D:workspace_phpapache-php.conf” The file path can be any In D: workspace_ Create under PHP file apache- php.conf file Its specific content is [html] view plain copy PHP-Module setup LoadFile “D:/xampp/php/php5ts.dll” LoadModule php5_module “D:/xampp/php/php5apache2_2.dll” <FilesMatch “.php$”> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch “.phps$”> SetHandler application/x-httpd-php-source </FilesMatch> […]