{"id":22488706,"url":"https://github.com/benchpressjs/benchpressjs","last_synced_at":"2025-04-16T15:49:46.505Z","repository":{"id":14963472,"uuid":"17688301","full_name":"benchpressjs/benchpressjs","owner":"benchpressjs","description":"ultralight javascript templating framework","archived":false,"fork":false,"pushed_at":"2025-03-31T21:28:07.000Z","size":4511,"stargazers_count":90,"open_issues_count":3,"forks_count":17,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-04-13T19:13:15.261Z","etag":null,"topics":["javascript","templates"],"latest_commit_sha":null,"homepage":"","language":"Rust","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/benchpressjs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2014-03-12T23:50:00.000Z","updated_at":"2025-03-31T21:28:11.000Z","dependencies_parsed_at":"2024-06-18T16:52:52.751Z","dependency_job_id":"7b305a79-9cf5-4a22-8a5a-c352873a7461","html_url":"https://github.com/benchpressjs/benchpressjs","commit_stats":{"total_commits":535,"total_committers":9,"mean_commits":59.44444444444444,"dds":0.5663551401869159,"last_synced_commit":"df6d6afe8ad3ba9d5795f764ed0cf7f98dfd5678"},"previous_names":["psychobunny/templates.js"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benchpressjs%2Fbenchpressjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benchpressjs%2Fbenchpressjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benchpressjs%2Fbenchpressjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benchpressjs%2Fbenchpressjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benchpressjs","download_url":"https://codeload.github.com/benchpressjs/benchpressjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249256919,"owners_count":21239079,"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":["javascript","templates"],"created_at":"2024-12-06T17:18:30.352Z","updated_at":"2025-04-16T15:49:46.486Z","avatar_url":"https://github.com/benchpressjs.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# \u003cimg alt=\"benchpress\" src=\"https://cdn.rawgit.com/benchpressjs/benchpressjs/master/benchpress.svg\" /\u003e\n\n![CI Status](https://github.com/benchpressjs/benchpressjs/workflows/Lint%20and%20test/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/benchpressjs/benchpressjs/badge.svg?branch=master)](https://coveralls.io/github/benchpressjs/benchpressjs?branch=master)\n[![Code Climate](https://codeclimate.com/github/benchpressjs/benchpressjs.png)](https://codeclimate.com/github/benchpressjs/benchpressjs)\n\nBenchpress is an ultralight (1.3kb minified + gzipped) and super fast templating framework for Javascript and node.js.\n\nIt has [express](http://expressjs.com/) support out-of-the-box, and requires zero client-side dependencies.\n\n## Installation\nBenchpress is available as an npm module:\n\n    npm i benchpressjs\n\n## API\nThe following is a quick rundown of the API. [See the full docs](docs/readme.md)\n\nBenchpress uses an ahead of time (AOT) compilation model. It requires that you precompile templates into Javascript modules before using them.\n\n### `.precompile(source, { filename }): Promise\u003cstring\u003e`\nThis method compiles a template source into Javascript code.\n\n```js\nconst fs = require('fs').promises;\nconst benchpress = require('benchpressjs');\n\nconst template = await fs.readFile('path/to/source/templates/favorite.tpl', 'utf8');\nconst compiled = await benchpress.precompile(template, { filename: 'favorite.tpl' });\nawait fs.writeFile('path/to/compiled/templates/favorite.jst', compiled);\n```\n\n`path/to/source/templates/favorite.tpl`\n```html\nMy favourite forum software is {forum}. This templating engine is written in {language}.\n```\n\n`path/to/compiled/templates/favorite.jst`\n```js\n(function (factory) {\n  if (typeof module === 'object' \u0026\u0026 module.exports) {\n    module.exports = factory();\n  } else if (typeof define === 'function' \u0026\u0026 define.amd) {\n    define(factory);\n  }\n})(function () {\n  function compiled(helpers, context, get, iter, helper) {\n    return 'My favourite forum software is ' + get(context \u0026\u0026 context['forum']) + '. This templating engine is written in ' + get(context \u0026\u0026 context['language']) + '.';\n  }\n\n  return compiled;\n});\n```\n\n### `.__express`\n\nThis method provides an express engine API.\n\n```js\nconst express = require('express');\nconst app = express();\nconst benchpress = require('benchpressjs');\n\nconst data = {\n  foo: 'bar',\n};\n\napp.configure(function() {\n  app.engine('jst', benchpress.__express);\n  app.set('view engine', 'jst');\n  app.set('views', 'path/to/compiled/templates');\n});\n\napp.render('myview', data, function(err, html) {\n  console.log(html);\n});\n\napp.get('/myroute', function(req, res, next) {\n  res.render('myview', data);\n});\n```\n\n### `.render(template, data): Promise\u003cstring\u003e` (alias: `.parse(template, data, callback(string))`)\n\nThis method is used mainly to parse templates on the client-side.\nTo use it, `.registerLoader(loader)` must be used to set the callback for fetching compiled template modules.\n\n```js\nrequire(['benchpress'], (benchpress) =\u003e {\n  benchpress.registerLoader((name, callback) =\u003e {\n    // fetch `name` template module\n  });\n\n  benchpress.render('basic', {\n    forum: 'NodeBB',\n    language: 'Javascript',\n  }).then((output) =\u003e {\n    // do something with output\n  });\n});\n```\n\n## Template Syntax\nSample data, see test cases for more:\n\n```json\n{\n  \"animals\": [\n    {\n      \"name\": \"Cat\",\n      \"species\": \"Felis silvestris catus\",\n      \"isHuman\": false,\n    },\n    {\n      \"name\": \"Dog\",\n      \"species\": \"Canis lupus familiaris\",\n      \"isHuman\": false,\n    },\n    {\n      \"name\": \"Human\",\n      \"species\": \"Homo sapiens\",\n      \"isHuman\": true\n    }\n  ],\n  \"package\": {\n    \"name\": \"benchpressjs\",\n    \"author\": \"psychobunny\",\n    \"url\": \"http://www.github.com/benchpressjs/benchpress\"\n  },\n  \"website\": \"http://burnaftercompiling.com\",\n  \"sayHello\": true\n}\n```\n\n### Simple key/value\n```\nMy blog URL is {website}. The URL for this library is {{package.url}}\n```\n\n### Conditionals\n```html\n{{{ if sayHello }}}\n  Hello world!\n{{{ end }}}\n\n{{{ if !somethingFalse }}}\n  somethingFalse doesn't exist\n{{{ end }}}\n```\nBenchpress supports several syntaxes for conditionals in order to be backwards compatible with **templates.js**.\n`\u003c!-- ENDIF abcd --\u003e`, `\u003c!-- END abcd --\u003e`, `\u003c!-- ENDIF !foobar --\u003e`, and `\u003c!-- END --\u003e` are all equivalent tokens as far as Benchpress is concerned.\n\n### Iteration\nRepeat blocks of HTML. The two special keys `@first` and `@last` are available as booleans, and the `@index`, `@key`, and `@value` special keys are also available. Benchpress supports iterating over objects, in which case `@index` will be the current loop number and `@key` will be the key of the current item. For normal arrays, `@key == @index`.\n\n```html\n{{{ each animals }}}\n  {animals.name} is from the species {animals.species}.\n  {{{ if !animals.isHuman }}}\n    - This could be a pet.\n  {{{ end }}}\n{{{ end }}}\n\nprints out:\n\nCat is from the species Felis silvestris catus.\n- This could be a pet.\nDog is from the Canis lupus familiaris.\n- This could be a pet.\nHuman is from the species Homo sapiens.\n```\n\nBenchpress supports several syntaxes for iteration in order to be backwards compatible with **templates.js**:\n - `\u003c!-- END abcd --\u003e` == `\u003c!-- END foo --\u003e` == `\u003c!-- END --\u003e`\n - `\u003c!-- BEGIN abc --\u003e {abc.def} \u003c!-- END --\u003e` == `\u003c!-- BEGIN abc --\u003e {../def} \u003c!-- END --\u003e` which will print the `def` key of every item in `abc`.\n\nThere is a grey zone where if you wish to print a field of the object you are iterating over, you can't directly. This is a breaking change from **templates.js**. To fix this, change `{abc.def}` to `{../../abc.def}`.\n\n### Helpers\n\nHelpers are JavaScript methods for advanced logic in templates. This example shows a really simple example of a function called `print_is_human` which will render text depending on the current block's data.\n\n```js\nbenchpress.registerHelper('print_is_human', function (data) {\n  return (data.isHuman) ? \"Is human\" : \"Isn't human\";\n});\n```\n\n```html\n{{{ each animals }}}\n{print_is_human(@value)}\n{{{ end }}}\n\nprints out:\n\nIsn't human\nIsn't human\nIs human\n```\n\n## Testing\n\n    npm install\n    npm test\n\n## Projects using Benchpress\n\n[NodeBB](http://www.nodebb.org)\n\nAdd yours here by submitting a PR :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenchpressjs%2Fbenchpressjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenchpressjs%2Fbenchpressjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenchpressjs%2Fbenchpressjs/lists"}