{"id":13636212,"url":"https://github.com/leafo/etlua","last_synced_at":"2025-10-27T11:05:40.356Z","repository":{"id":11406008,"uuid":"13854020","full_name":"leafo/etlua","owner":"leafo","description":"Embedded Lua templates","archived":false,"fork":false,"pushed_at":"2023-10-02T15:30:26.000Z","size":27,"stargazers_count":228,"open_issues_count":4,"forks_count":18,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-31T18:20:06.610Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Lua","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/leafo.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2013-10-25T07:18:39.000Z","updated_at":"2025-03-27T16:43:59.000Z","dependencies_parsed_at":"2024-01-08T20:19:18.423Z","dependency_job_id":null,"html_url":"https://github.com/leafo/etlua","commit_stats":{"total_commits":46,"total_committers":3,"mean_commits":"15.333333333333334","dds":0.04347826086956519,"last_synced_commit":"8dda2e5aeb4413446172a562a9a374b700054836"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leafo%2Fetlua","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leafo%2Fetlua/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leafo%2Fetlua/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leafo%2Fetlua/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leafo","download_url":"https://codeload.github.com/leafo/etlua/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247767236,"owners_count":20992548,"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":[],"created_at":"2024-08-02T00:00:58.607Z","updated_at":"2025-10-27T11:05:35.318Z","avatar_url":"https://github.com/leafo.png","language":"Lua","readme":"# etlua\n\nEmbedded Lua templating\n\n## Install\n\n```bash\n$ luarocks install etlua\n```\n\n## Tutorial\n\n```lua\nlocal etlua = require \"etlua\"\nlocal template = etlua.compile([[\n  Hello \u003c%= name %\u003e,\n  Here are your items:\n  \u003c% for i, item in pairs(items) do %\u003e\n   * \u003c%= item -%\u003e\n  \u003c% end %\u003e\n]])\n\nprint(template({\n  name = \"leafo\",\n  items = { \"Shoe\", \"Reflector\", \"Scarf\" }\n}))\n\n```\n\n## Reference\n\nThe following tags are supported\n\n* `\u003c% lua_code %\u003e` runs lua code verbatim\n* `\u003c%= lua_expression %\u003e` writes result of expression to output, HTML escaped\n* `\u003c%- lua_expression %\u003e` same as above but with no HTML escaping\n\nAny of the embedded Lua tags can use the `-%\u003e` closing tag to suppress a\nfollowing newline if there is one, for example: `\u003c%= 'hello' -%\u003e`.\n\nThe module can be loaded by doing:\n\n```lua\nlocal etlua = require \"etlua\"\n```\n\n### Methods\n\n#### `func = etlua.compile(template_string)`\n\nCompiles the template into a function, the returned function can be called to\nrender the template. The function takes one argument: a table to use as the\nenvironment within the template. `_G` is used to look up a variable if it can't\nbe found in the environment.\n\n#### `result = etlua.render(template_string, env)`\n\nCompiles and renders the template in a single call. If you are concerned about\nhigh performance this should be avoided in favor of `compile` if it's possible\nto cache the compiled template.\n\n### Errors\n\nIf any of the methods fail they will return `nil`, followed by the error\nmessage.\n\n### How it works\n\n* Templates are transparently translated into Lua code and then loaded as a\n  function. Rendering a compiled template is very fast.\n* Any compile time errors are rewritten to show the original source position in\n  the template.\n* The parser is aware of strings so you can put closing tags inside of a string\n  literal without any problems.\n\n## Raw API\n\nThe raw API is a bit more complicated but it lets you insert code between the\ncompile stages in addition to exposing the internal buffer of the template.\n\nAll methods require a parser object:\n\n```lua\nlocal parser = etlua.Parser()\n```\n\n#### `lua_code, err = parser.compile_to_lua(etlua_code)`\n\nParses a string of etlua code, returns the compiled Lua version as a\nstring.\n\nHere's an example of the generated Lua code:\n\n```lua\nlocal parser = etlua.Parser()\nprint(parser:compile_to_lua(\"hello\u003c%= world %\u003e\"))\n```\n\n```lua\nlocal _b, _b_i, _tostring, _concat, _escape = ...\n_b_i = _b_i + 1\n_b[_b_i] = \"hello\"\n_b_i = _b_i + 1\n--[[9]] _b[_b_i] = _escape(_tostring( world ))\n_b_i = _b_i + 1\n_b[_b_i] = \"\"\nreturn _b\n```\n\nThere are a few interesting things: there are no global variable references,\nall required values are passed in as arguments, and comments are inserted to\nannotate the positions of where code originated from. `_b` is expected to be a\nregular Lua table that is the buffer where chunks of the template are inserted\nas it's executed.\n\n#### `fn, err = parser.load(lua_code)`\n\nConverts the Lua code returned by `parser.compile_to_lua` into an actual\nfunction object. If there are any syntax errors then `nil` is returned along\nwith the error message. At this stage syntax errors are rewritten to point to\nthe original location in the etlua code and not the generated code.\n\n#### `result = parser.run(fn, env={}, buffer={})`\n\nExecutes a loaded function returned by `parser.load` with the specified buffer\nand environment. Returns the result of fn, which is typically the buffer. The\nenvironment is applied to `fn` with `setfenv` (a version is included for Lua\n5.2).\n\n### Example\n\nFor example we can render multiple templates into the same buffer:\n\n```lua\nparser = etlua.Parser()\n\nfirst_fn = parser:load(parser:compile_to_lua(\"Hello \"))\nsecond_fn = parser:load(parser:compile_to_lua(\"World\"))\n\nbuffer = {}\nparser:run(first_fn, nil, buffer, #buffer)\nparser:run(second_fn, nil, buffer, #buffer)\n\nprint(table.concat(buffer)) -- print 'Hello World'\n```\n\n## Custom compiler\n\nIf you need to customize the Lua code that is generated by etlua to integrate\nwith your own output buffers then you can provide a custom compiler.\n\nYou can extend `etlua.Compiler` and override it's methods to control the\noutput. See \u003chttps://github.com/leafo/etlua/blob/master/etlua.moon#L42\u003e for the\nimplementation of the default compiler\n\nFor an example we'll create a debug compiler that prints whenever a template is\nexecuted.\n\n```moonscript\n-- create a custom compiler\nimport Compiler from require \"etlua\"\n\nclass DebugCompiler extends Compiler\n  header: =\u003e\n    @push 'print(\"Running template\")\\n'\n    super!\n\n-- try it out\nimport Parser from require \"etlua\"\n\nprint Parser!\\compile_to_lua \"hello\", DebugCompiler\n```\n\n`compile_to_lua` takes an optional second argument of the compiler class to\nuse.\n\n## Editor Support\n\n* [Vim](https://github.com/VaiN474/vim-etlua)\n* [Visual Studio Code](https://github.com/commita/vscode-lua-templates)\n* [Sublime/Textmate](https://github.com/VaiN474/etlua-tmLanguage)\n* [Atom](https://github.com/VaiN474/language-etlua)\n\n## License\n\nMIT, Copyright (C) 2014 by Leaf Corcoran\n\n","funding_links":[],"categories":["Libraries","Lua","资源","Resources"],"sub_categories":["Templating"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleafo%2Fetlua","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleafo%2Fetlua","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleafo%2Fetlua/lists"}