Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options in opts for consolidate[engine] #36

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

Froguard
Copy link

@Froguard Froguard commented Jul 31, 2019

If I wanna use extend/include, a block, macro or others in nunjuck, config such as follow:

Directory:

   /
   ├─ app.js
   │
   └─ tpl
       │
       ├─ com.html
       │
       └─ error.html

app.js:

app.use(koaError({
    //...
    engine: 'nunjucks',
    template: path.join(__dirname, 'tpl/error.html'),
    options: {
        settings: {
            views: path.join(__dirname, 'tpl') // use other tpl file in tpl
        },
        helpers: {
            stringify(o) {
                return JSON.stringify(o);
            }
        }
    }
}));

tpl/error.html:

<!DOCTYPE html>
<html>
  <head>
    {% include "./com.html" %}
  </head>
  <body>
    <!--  use helpers.stringify(obj) here -->
    <!-- ... -->
  </body>
</htm>

tpl/com.html:

<link rel="stylesheet" href="/css/normalize.css" />

more-about-nunjucks

issue-37

@Froguard Froguard changed the title Add options in opts for consolidate Add options in opts for consolidate[engine] Jul 31, 2019
@Froguard
Copy link
Author

Froguard commented Jul 31, 2019

You can also define some custom filters in the following way:

app.js

const nunjucksEnv = new nunjucks.Environment(
    new nunjucks.FileSystemLoader(path.join(__dirname, 'tpl'))
);

nunjucksEnv.addFilter('stringify', (...args) => {
    return JSON.stringify(...args);
});

app.use(koaError({
    //...
    template: path.join(__dirname, 'tpl/error.html'),
    options: {
        nunjucksEnv // custom nunjucks env
    }
}));

error.html

<!DOCTYPE html>
<html>
  <head>
    {% include "./com.html" %}
  </head>
  <body>
    <p>{{ request | stringify }}</p>
    <!-- ... -->
  </body>
</htm>

index.js Show resolved Hide resolved
Copy link
Author

@Froguard Froguard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readme

Copy link
Member

@3imed-jaberi 3imed-jaberi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@3imed-jaberi 3imed-jaberi mentioned this pull request Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants