{"id":19607594,"url":"https://github.com/nishants/jeyson","last_synced_at":"2025-08-01T10:04:28.817Z","repository":{"id":57151683,"uuid":"60058500","full_name":"nishants/jeyson","owner":"nishants","description":"A JSON template system.","archived":false,"fork":false,"pushed_at":"2018-12-08T08:46:53.000Z","size":81,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-18T23:05:41.427Z","etag":null,"topics":["inbuilt-directives","javascript","json","json-template","npm","template-engine","template-language"],"latest_commit_sha":null,"homepage":null,"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/nishants.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":"2016-05-31T04:36:41.000Z","updated_at":"2023-06-12T06:30:46.000Z","dependencies_parsed_at":"2022-09-17T02:14:43.983Z","dependency_job_id":null,"html_url":"https://github.com/nishants/jeyson","commit_stats":null,"previous_names":["nishants/jso-ng"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nishants%2Fjeyson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nishants%2Fjeyson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nishants%2Fjeyson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nishants%2Fjeyson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nishants","download_url":"https://codeload.github.com/nishants/jeyson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251204548,"owners_count":21552239,"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":["inbuilt-directives","javascript","json","json-template","npm","template-engine","template-language"],"created_at":"2024-11-11T10:11:29.217Z","updated_at":"2025-04-27T20:32:25.759Z","avatar_url":"https://github.com/nishants.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Jeyson Templates\n  - Every json is a valid template in itself.\n  - The templates can have expressions, which can execute javascript.\n  - These templates are compiled in context of a scope.\n  - Directives can pass instructions to compiler.\n  - Inbuilt directives for iterating, conditional contents, including other templates etc.\n  - Custom directives can replace/modify template body, execute expressions, read other templates.\n\n### Compiling Templates\n```javascript\nvar jeyson        = require('jeyson').create(),\n    scope         = {message: 'Hello!'},\n    templateJson  = {\"message\": \"{{message}}\"},\n    compiled      = jeyson.compile(scope, templateJson);\n```\n\n### Expressions\nAn expression is defined as '__{{expr}}__'\n\nFollowing __template.json__  :\n```javascript\n{\n  \"age\": \"{{21 + 33}}\"\n}\n ```\n is compiled to\n ```javascript\n {\n  \"age\": 54\n }\n ```\n\n### Javascript in Expressions.\nAny valid javascript snippet is a valid expression, e.g following __template.json__  :\n```javascript\n{\n  \"list\"      : \"{{'one,two,three,four,five'.split(',')}}\",\n}\n ```\nis compiled to\n ```javascript\n{\n  \"list\"    : [\"one\", \"two\", \"three\", \"four\", \"five\"],\n}\n ```\n### Scopes\nAny field on scope object, is available as local variable in expressions.\n\ne.g.  Given scope defined as :\n ```javascript\nvar scope    = {message: \"Hello !\"},\n```\nthen following __template.json__  :\n```javascript\n{\n  \"message\": \"{{message}}\"\n}\n```\nis compiled to\n```javascript\n{\n \"message\": \"Hello !\"\n}\n```\n\n#### Scope can have methods\n\ne.g.  Given scope defined as :\n ```javascript\nvar scope = {\n    message: function(){\n      return \"Hello !\";\n    }\n};\n```\nthen following __template.json__  :\n```javascript\n{\n  \"message\": \"{{message()}}\"\n}\n```\nis compiled to\n```javascript\n{\n \"message\": \"Hello !\"\n}\n```\n\n### Directives\n - Directive is a field with name starting with __\"@\"__\n - Directive __body__ is the parent subtree of directive field\n - Directive argument is the value of directive field.\n\n E.g.\n ```javascript\n {\n    \"list\"  : {\n      \"@repeat\" : \"name in ['one','two','three','four','five']\",\n      \"id\"      : \"{{$index + 1}}\",\n      \"name\"    : \"{{name}}\",\n    }\n }\n```\n - __\"list\"__  value is directive body (has a directive child)\n - __@repeat__    is  directive name\n - __count in [1,2,3,4,5]__ is directive argument\n\n It will compile to following json :\n\n```javascript\n{\n  \"list\": [ {\"id\": \"1\",\"name\" : \"one\"},\n            {\"id\": \"2\",\"name\" : \"two\"},\n            {\"id\": \"3\",\"name\" : \"three\"},\n            {\"id\": \"4\",\"name\" : \"four\"},\n            {\"id\": \"5\",\"name\" : \"five\"}];\n}\n ```\n\n# Conditional Blocks \u0026 System Directives\nFollowing are the conditional directives\n 1. switch\n 2. if\n 3. ignore-if (a system directive)\n\n### @switch\n#### Will replace the body with \"@[value]\", \"@default\" or null based on choice.\n\n```javascript\nscope    = {role : {name: \"admin\"}}\ntemplate = {\n\"user\"    : {\n  \"role\"    : {\n     \"@switch\"    : \"role.name\",\n     \"@admin\"     : {\"name\" : \"admin\"},\n     \"@visitor\"   : {\"name\" : \"visitor\"},\n     \"@default\"   : {\"name\" : \"guest\"}\n  }\n}\n```\nWill compile to\n```javascript\ntemplate = {\n   \"user\"   : {\n   \"role\"  : {\"name\" : \"admin\"}\n }\n```\n### @if, @then, @else\n#### Will set field value to null, \"@then\"  or \"@else\", by condition\n\n```javascript\n{\n  \"author\"  : \"ABC\",\n  \"book\"    : {\n    \"@if\"   : \"1 == 2\",\n    \"@then\" : {\"title\" : \"Harry Potter\"}\n    \"@else\" : \"unknown\",\n  }\n}\n```\n\nwill result in\n\n```javascript\n{\n \"author\"  : \"ABC\",\n \"book\"    : \"unknown\"\n}\n```\n\n```javascript\n{\n  \"author\"  : \"ABC\",\n  \"book\"    : {\n    \"@if\"   : \"1 == 1\",\n    \"@then\" : {\"title\" : \"Harry Potter\"}\n}\n```\nwill result in\n```javascript\n{\n \"author\"  : \"ABC\",\n \"book\"    : {\"title\" : \"Harry Potter\"}\n}\n```\n\n```javascript\n{\n  \"author\"  : \"ABC\",\n  \"book\"    : {\n    \"@if\"   : \"1 == 2\",\n    \"@then\" : {\"title\" : \"Harry Potter\"}\n}\n```\n\nwill result in\n```javascript\n{\n \"author\"  : \"ABC\",\n \"book\"    : null\n}\n```\n### @ignore-if\n#### Will not render the field itself, if the condition is false.\ne.g\n```javascript\n{\n  \"author\"  : \"ABC\",\n  \"book\"    : {\n    \"@ignore-if\": \"1 == 1\",\n    \"title\"     : \"Harry Potter\"\n  }\n}\n```\n\nwill result in\n```javascript\n{\n \"author\"  : \"ABC\"\n}\n```\n\n\n### Inbuilt Directives\nInbuilt directives for repeating, including other json etc.\n\n### @repeat\n - $index\n - array\n - result\n\n### Custom Directives\n  - Reading scope\n  - Executing expressions\n  - Modifying body\n  - Replacing body\n  - Reading other templates by relative path\n\n### Source code of inbuilt directives\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnishants%2Fjeyson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnishants%2Fjeyson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnishants%2Fjeyson/lists"}