Narnia's Psychedelic Mushrooms

    egg-view-twig-x

    0.0.2 • 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

    Install

    npm i egg-view-twig-x

    DownloadsWeekly Downloads

    2

    Version

    0.0.2

    License

    MIT

    Unpacked Size

    7.39 kB

    Total Files

    8

    Last publish

    Collaborators

    • bizbin