egg-view-twig

0.0.1 • Public • Published

egg-view-twig

NPM version build status Test coverage David deps Known Vulnerabilities npm download

egg view plugin for twig.

Install

$ npm i egg-view-twig --save

Usage

// {app_root}/config/plugin.js
exports.twig = {
  enable: true,
  package: 'egg-view-twig',
};
 
// {app_root}/config/config.default.js
exports.view = {
  mapping: {
    '.twig': 'twig',
  },
};
 
// twig config
exports.twig = {};

Create a twig file

// app/view/hello.twig
hello {{ data }}

Render it

// app/controller/render.js
exports.twig = function* () {
  yield ctx.render('hello.twig', {
    data: 'world',
  });
};

The file will be compiled and cached, you can change config.twig.cache = false to disable cache, it's disable in local env by default.

Include

You can include both relative and absolute file.

Relative file is resolve from current file path.

// app/view/a.twig include app/view/b.twig
{% include 'b.twig' %}

Absolute file is resolve from app/view.

// app/view/home.twig include app/view/partial/menu.twig
{% include '/partial/menu.twig' %}

Layout

You can render a view with layout also:

// app/view/layout.twig
 
{% include 'header.twig' %}
{% block content %}{% endblock %}
{% include 'footer.twig' %}
 
// app/view/hello.twig
 
{% extends 'layout.twig' %}
{% block content %}
  Hello {{ data }}, I'm content. 
{% endblock %}
// app/controller/render.js
exports.twig = function* () {
  const locals = {
    data: 'world',
  };
 
  yield ctx.render('hello.twig', locals);
};

Configuration

see config/config.default.js for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.1
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.1
    0

Package Sidebar

Install

npm i egg-view-twig

Weekly Downloads

0

Version

0.0.1

License

MIT

Last publish

Collaborators

  • slevp