{"id":15138712,"url":"https://github.com/pkolt/express-nunjucks","last_synced_at":"2025-04-23T13:33:06.190Z","repository":{"id":36356951,"uuid":"40661732","full_name":"pkolt/express-nunjucks","owner":"pkolt","description":"Is the glue for express and nunjucks.","archived":false,"fork":false,"pushed_at":"2024-09-24T12:35:48.000Z","size":544,"stargazers_count":43,"open_issues_count":6,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T00:07:49.068Z","etag":null,"topics":["express","nunjucks"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pkolt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-13T14:00:20.000Z","updated_at":"2024-08-14T02:27:50.000Z","dependencies_parsed_at":"2024-03-28T18:02:29.984Z","dependency_job_id":"707ccdc5-86c2-41c5-8f2b-805a8caa67ad","html_url":"https://github.com/pkolt/express-nunjucks","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkolt%2Fexpress-nunjucks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkolt%2Fexpress-nunjucks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkolt%2Fexpress-nunjucks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pkolt%2Fexpress-nunjucks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pkolt","download_url":"https://codeload.github.com/pkolt/express-nunjucks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250440212,"owners_count":21430969,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","nunjucks"],"created_at":"2024-09-26T07:43:51.051Z","updated_at":"2025-04-23T13:33:06.161Z","avatar_url":"https://github.com/pkolt.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-nunjucks ![](https://github.com/pkolt/express-nunjucks/workflows/main/badge.svg)\n\nIs the glue for [express](http://expressjs.com/) and [nunjucks](http://mozilla.github.io/nunjucks/).\n\nSupports ESM modules 👍\n\n## Features\n\n- Easy connection.\n- Uses of common templates, filters and extensions.\n- Uses an asynchronous loader templates [nunjucks-async-loader](https://github.com/pkolt/nunjucks-async-loader).\n- Support context processors.\n\n## Installation\n\n```bash\n$ npm i nunjucks express-nunjucks\n```\n\n## Usage\n\n```typescript\nimport express from 'express';\nimport expressNunjucks from 'express-nunjucks';\n// For CommonJS\n// const expressNunjucks = require('express-nunjucks').default;\n\nconst app = express();\nconst isDev = app.get('env') === 'development';\n\napp.set('views', __dirname + '/templates');\n\nconst njk = expressNunjucks(app, {\n  watch: isDev,\n  noCache: isDev,\n});\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\napp.listen(3000);\n```\n\n## API\n\n### expressNunjucks(apps [,config]) -\u003e njk\n\n#### `apps {Object|Array}`\n\n[Express application][exp_app] or an array of applications.\n\n#### `config {Object}`\n\n- `watch=false {Boolean}` - if true, the system will automatically update templates when they are changed on the filesystem.\n- `noCache=false {Boolean}` - if true, the system will avoid using a cache and templates will be recompiled every single time.\n- `autoescape=true {Boolean}` - controls if output with dangerous characters are escaped automatically.\n- `throwOnUndefined=false {Boolean}` - throw errors when outputting a null/undefined value.\n- `trimBlocks=false {Boolean}` - automatically remove trailing newlines from a block/tag.\n- `lstripBlocks=false {Boolean}` - automatically remove leading whitespace from a block/tag.\n- `tags` - defines the syntax for [nunjucks tags][njk_custom_tags].\n- `filters` - defines the syntax for [nunjucks filters][njk_custom_filters].\n- `loader` - defines [loader templates][njk_loader]. The default is the asynchronous loader templates.\n- `globals` - defines [global variables][njk_globals].\n\n### njk.ctxProc(ctxProcessors) -\u003e Middleware\n\nCreates [Express middleware][exp_middleware] to work context processors.\n\n### njk.env -\u003e Environment\n\nReturns [Nunjucks Environment][njk_env].\n\n## Examples\n\n### Use filters\n\nCreate [custom filters][njk_custom_filters] in nunjucks.\n\n```typescript\nimport express from 'express';\nimport expressNunjucks from 'express-nunjucks';\nimport filters from './filters';\n\nconst app = express();\n\napp.set('views', __dirname + '/templates');\n\nconst njk = expressNunjucks(app, {\n  // Add custom filter.\n  filters: filters,\n});\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\napp.listen(3000);\n```\n\n### Use globals\n\nDefines [globals][njk_globals] to use this in templates.\n\n```typescript\nimport express from 'express';\nimport expressNunjucks from 'express-nunjucks';\nimport { asset } from './utils';\n\nconst app = express();\n\napp.set('views', __dirname + '/templates');\n\nconst njk = expressNunjucks(app, {\n  // Defines globals.\n  globals: { asset: asset },\n});\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\napp.listen(3000);\n```\n\n```html\n...\n\u003clink rel=\"stylesheet\" href=\"{{ asset('styles.css') }}\" /\u003e\n...\n```\n\n### Use context processors\n\nContext processors is one great idea from the [django framework][dj_ctx_processors].\n\n```typescript\nimport express from 'express';\nimport expressNunjucks from 'express-nunjucks';\nimport { webpackAssets } from './build/assets';\n\nconst app = express();\n\napp.set('views', __dirname + '/templates');\n\n// Adds information about the request in the context of the template.\nconst reqCtxProcessor = (req, ctx) =\u003e {\n  ctx.req = req;\n};\n// Adds links to statics in the context of the template.\nconst assetsCtxProcessor = (req, ctx) =\u003e {\n  ctx.scripts = webpackAssets.scripts;\n  ctx.styles = webpackAssets.styles;\n};\n\nconst njk = expressNunjucks(app);\n\napp.use(njk.ctxProc([reqCtxProcessor, assetsCtxProcessor]));\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\napp.listen(3000);\n```\n\n**Warning!** Context processors not supported to `app.render()`.\n\n### Use synchronous loader templates\n\n```typescript\nimport express from 'express';\nimport expressNunjucks from 'express-nunjucks';\n\nconst app = express();\n\napp.set('views', __dirname + '/templates');\n\nconst njk = expressNunjucks(app, {\n  loader: nunjucks.FileSystemLoader,\n});\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\napp.listen(3000);\n```\n\n### Use application and sub application\n\n#### General application\n\n```typescript\n// proj/app.js\n\nimport express from 'express';\nimport expressNunjucks from 'express-nunjucks';\nimport subApp from './subapp';\n\nconst app = express();\n\napp.set('views', __dirname + '/templates');\n\nconst njk = expressNunjucks([app, subApp]);\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\napp.use('/subApp', subApp);\n// and more...\n\napp.listen(3000);\n```\n\n#### Sub application\n\n```typescript\n// proj/subapp/index.js\n\nimport express from 'express';\nconst app = express();\n\napp.set('views', __dirname + '/templates');\n\napp.get('/', (req, res) =\u003e {\n  res.render('index');\n});\n\nmodule.exports = app;\n```\n\n#### Template hierarchy\n\n```\nproj\n|\n|- templates\n|   |\n|   |- base.html\n|   |- index.html\n|   |-subapp\n|      |\n|      |-page.html\n|\n|- subapp\n    |\n    |-templates\n       |\n       |-subapp\n          |\n          |-index.html\n          |-page.html\n```\n\nThe templates in the directory `proj/templates/subapp` override templates `proj/subapp/templates/subapp`.\n\n## TypeScript\n\nIf you're having trouble importing a module into TypeScript, try adding settings to `tsconfig.json`:\n\n```json\n{\n    \"compilerOptions\": {\n      \"esModuleInterop\": true,\n      \"allowSyntheticDefaultImports\": true\n    }\n  }\n```\n\n## Tests\n\nTo run the test suite, first install the dependencies, then run `npm test`:\n\n```bash\n$ npm ci\n$ npm test\n```\n\n## License\n\n[MIT](LICENSE.md)\n\n[dj_ctx_processors]: https://docs.djangoproject.com/en/1.9/ref/templates/api/#built-in-template-context-processors\n[njk_custom_filters]: http://mozilla.github.io/nunjucks/api.html#custom-filters\n[njk_custom_tags]: http://mozilla.github.io/nunjucks/api.html#customizing-syntax\n[njk_env]: http://mozilla.github.io/nunjucks/api.html#environment\n[njk_loader]: https://mozilla.github.io/nunjucks/api.html#loader\n[njk_globals]: http://mozilla.github.io/nunjucks/api.html#addglobal\n[exp_engine]: http://expressjs.com/en/api.html#app.engine\n[exp_app]: http://expressjs.com/en/api.html#app\n[exp_middleware]: http://expressjs.com/en/guide/writing-middleware.html\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkolt%2Fexpress-nunjucks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpkolt%2Fexpress-nunjucks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpkolt%2Fexpress-nunjucks/lists"}