{"id":19826880,"url":"https://github.com/estools/estemplate","last_synced_at":"2025-05-01T14:31:49.969Z","repository":{"id":17942863,"uuid":"20922640","full_name":"estools/estemplate","owner":"estools","description":"Proper (AST-based) JavaScript code templating with source maps support.","archived":false,"fork":false,"pushed_at":"2016-12-03T09:47:10.000Z","size":21,"stargazers_count":105,"open_issues_count":2,"forks_count":10,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-11-04T16:07:18.396Z","etag":null,"topics":["ast","ecmascript","estree","javascript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/estools.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-06-17T12:37:27.000Z","updated_at":"2024-02-09T15:25:40.000Z","dependencies_parsed_at":"2022-08-27T20:03:54.528Z","dependency_job_id":null,"html_url":"https://github.com/estools/estemplate","commit_stats":null,"previous_names":["rreverser/estemplate"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estools%2Festemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estools%2Festemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estools%2Festemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/estools%2Festemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/estools","download_url":"https://codeload.github.com/estools/estemplate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224262048,"owners_count":17282267,"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":["ast","ecmascript","estree","javascript"],"created_at":"2024-11-12T11:11:53.070Z","updated_at":"2024-11-12T11:11:53.267Z","avatar_url":"https://github.com/estools.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# estemplate [![Build Status](https://secure.travis-ci.org/estools/estemplate.png?branch=master)](http://travis-ci.org/estools/estemplate)\n\n\u003e Proper JavaScript code templating with source maps support.\n\nThis module allows to generate JavaScript AST from code template and AST nodes as substitutions.\n\nThis is more proper way of code templating since it works on AST not on code string, and thus preserves locations which allow to generate source maps in future.\n\n## Getting Started\nInstall the module with: `npm install estemplate` and require it:\n\n```shell\nnpm i estemplate --save\n```\n\n```javascript\nvar estemplate = require('estemplate');\n```\n\n## API\n\n### estemplate(tmplString, [options], data)\n\nGenerates [SpiderMonkey AST](https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API) from given template string, optional [esprima](http://esprima.org/doc/index.html) options and data.\n\nSupported template substitution markers:\n\n  * Compile-time execution block: `\u003c% var localCounter = 0; %\u003e`\n  * Node substitution: `var x = \u003c%= expr %\u003e + 1;`\n  * Array elements: `var a = [%= elements %];`\n  * Function parameters: `function f(%= params %) {}`\n  * Call arguments: `var x = f(%= args %);`\n  * Block statements: `define(function () {%= body %});`\n  * Literals: `var x = \"%= 'alpha' + 'beta' %\";`\n\nYou can combine list substitutions with inline elements like:\n  * `var a = [0, %= numbers %, Infinity];`\n  * `function f(%= params %, callback) {}`\n  * `define(function () { console.time('Module'); %= body %; console.timeEnd('Module'); });`\n\nFrom template, you can access entire data object via `it` and estemplate itself via `estemplate`.\n\nIf you set `options.fast` to true, then passed data will be available only via `it` variable, but template function in general will be significantly faster.\n\n### estemplate.compile(tmplString, [options])\n\nSame as above but returns function that can be reused for AST generation (just save result and call with `data` as argument whenever needed).\n\n## Examples\n\n### Simple generation\n\n```javascript\nvar ast = estemplate('var \u003c%= varName %\u003e = \u003c%= value %\u003e + 1;', {\n  varName: {type: 'Identifier', name: 'myVar'},\n  value: {type: 'Literal', value: 123}\n});\n\nconsole.log(escodegen.generate(ast));\n// \u003e var myVar = 123 + 1;\n```\n\n### Advanced generation (with source map)\n\n\u003e template.jst\n\n```javascript\ndefine(function (require, exports, module) {% = body %});\n```\n\n\u003e index.js\n\n```javascript\nvar dependency1 = require('dependency1'),\n    dependency2 = require('dependency2');\n\nmodule.exports = function () {\n\treturn dependency1() + dependency2();\n};\n```\n\n\u003e main code\n\n```javascript\nvar templateCode = fs.readFileSync('template.jst', 'utf-8');\nvar template = estemplate.compile(templateCode, {attachComment: true});\n\nvar program = esprima.parse(fs.readFileSync('index.js', 'utf-8'), {\n    loc: true,\n    source: 'index.js'\n});\n\nvar ast = template({body: program.body});\n\nvar output = escodegen.generate(ast, {\n  sourceMap: true,\n  sourceMapWithCode: true\n});\n\nconsole.log(output.code);\n```\n\n\u003e output\n\n```javascript\ndefine(function (require, exports, module) {\n    var dependency1 = require('dependency1'), dependency2 = require('dependency2');\n    module.exports = function () {\n        return dependency1() + dependency2();\n    };\n});\n```\n\n## Contributing\nIn lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).\n\n## License\nCopyright (c) 2014 Ingvar Stepanyan. Licensed under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Festools%2Festemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Festools%2Festemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Festools%2Festemplate/lists"}