egg-view-twig
egg view plugin for twig.
Install
$ npm i egg-view-twig --save
Usage
// {app_root}/config/plugin.jsexportstwig = enable: true package: 'egg-view-twig'; // {app_root}/config/config.default.jsexportsview = mapping: '.twig': 'twig' ; // twig configexportstwig = {};
Create a twig file
// app/view/hello.twighello data
Render it
// app/controller/render.jsexports { ctx;};
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.jsexports { const locals = data: 'world' ; ctx;};
Configuration
see config/config.default.js for more detail.
Questions & Suggestions
Please open an issue here.