{"id":22218561,"url":"https://github.com/megahertz/twigjs-loader","last_synced_at":"2025-07-27T14:32:47.634Z","repository":{"id":57382699,"uuid":"130244786","full_name":"megahertz/twigjs-loader","owner":"megahertz","description":"twig.js loader for Webpack","archived":false,"fork":false,"pushed_at":"2022-08-20T17:01:46.000Z","size":93,"stargazers_count":14,"open_issues_count":3,"forks_count":10,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-19T11:02:04.538Z","etag":null,"topics":["template","twig","twig-loader","webpack"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/megahertz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-19T16:40:09.000Z","updated_at":"2023-08-03T10:15:17.000Z","dependencies_parsed_at":"2022-09-01T04:41:22.217Z","dependency_job_id":null,"html_url":"https://github.com/megahertz/twigjs-loader","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/megahertz/twigjs-loader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Ftwigjs-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Ftwigjs-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Ftwigjs-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Ftwigjs-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/megahertz","download_url":"https://codeload.github.com/megahertz/twigjs-loader/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/megahertz%2Ftwigjs-loader/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267370642,"owners_count":24076461,"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","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["template","twig","twig-loader","webpack"],"created_at":"2024-12-02T22:27:08.834Z","updated_at":"2025-07-27T14:32:47.305Z","avatar_url":"https://github.com/megahertz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# twigjs-loader\n[![Build Status](https://travis-ci.org/megahertz/twigjs-loader.svg?branch=master)](https://travis-ci.org/megahertz/twigjs-loader)\n[![npm version](https://badge.fury.io/js/twigjs-loader.svg)](https://badge.fury.io/js/twigjs-loader)\n[![Dependencies status](https://img.shields.io/david/megahertz/twigjs-loader)](https://david-dm.org/megahertz/twigjs-loader)\n\n## Description\n\ntwig.js loader for Webpack\n\n\n## Installation\n\nThis package requires node.js 8 at least.\n\nInstall with [npm](https://npmjs.org/package/twigjs-loader):\n\n    $ npm install -D twigjs-loader\n\n## Usage\n\n```js\nconst indexView = require('./index.twig');\nconsole.log(indexView({ variable1: 'value' }));\n```\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  //...\n  module: {\n    rules: [\n      {\n        test: /\\.twig$/,\n        use: 'twigjs-loader',\n      },\n      //...\n    ],\n  },\n  //...\n}\n\n```\n\n### With Express\n\n - [example](examples/express)\n - [typescript example](examples/typescript)\n\n`$ npm install twigjs-loader`\n\n**index.js:**\n```js\nimport * as express from 'express';\nimport { ExpressView } from 'twigjs-loader';\nimport indexView from './views/index.twig';\n\nconst app = express();\napp.set('view', ExpressView);\n\napp.get('/', (req, res) =\u003e {\n  res.render(indexView, {\n    url: req.originalUrl,\n  })\n});\n\napp.listen(8080);\n```\n\n### On frontend\n\n - [example](examples/frontend)\n\n```js\nimport indexView from './views/index.twig';\n\ndocument.body.innerHTML = indexView({\n  url: location.href,\n})\n```\n\n## Configure\n\nYou can configure how a template is compiled by webpack using the\n`renderTemplate` option. For example:\n\n**webpack.config.js**\n\n```js\nmodule.exports = {\n  //...\n  module: {\n    rules: [\n      {\n        test: /\\.twig$/,\n        use:  {\n          loader: 'twigjs-loader',\n          options: {\n            /**\n             * @param {object}  twigData        Data passed to the Twig.twig function\n             * @param {string}  twigData.id     Template id (relative path)\n             * @param {object}  twigData.tokens Parsed AST of a template\n             * @param {string}  dependencies    Code which requires related templates\n             * @param {boolean} isHot           Is Hot Module Replacement enabled\n             * @return {string}\n             */\n            renderTemplate(twigData, dependencies, isHot) {\n              return `\n                ${dependencies}\n                var twig = require(\"twig\").twig;\n                var tpl = twig(${JSON.stringify(twigData)});\n                module.exports = function(context) { return tpl.render(context); };\n              `;\n            },\n          },\n        },\n      },\n      //...\n    ],\n  },\n  //...\n}\n\n```\n\n## Path resolving\n\nThe module uses webpack for resolving template path, so it doesn't resolve\npath by itself. If you need custom file path resolution (eg namespaces),\ncheck [the example](examples/namespaces).\n\n## Credits\n\nBased on [zimmo-be/twig-loader](https://github.com/zimmo-be/twig-loader)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegahertz%2Ftwigjs-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmegahertz%2Ftwigjs-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmegahertz%2Ftwigjs-loader/lists"}