egg-view-twig
v0.0.1
Published
egg view plugin for twig
Downloads
2
Maintainers
Readme
egg-view-twig
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.