{"id":16419331,"url":"https://github.com/sviperll/static-mustache","last_synced_at":"2025-03-21T03:32:47.866Z","repository":{"id":24207858,"uuid":"27599469","full_name":"sviperll/static-mustache","owner":"sviperll","description":"Template engine for java with statically checked and compiled templates. Compilation is performed alone with java sources.","archived":false,"fork":false,"pushed_at":"2020-10-13T09:05:47.000Z","size":154,"stargazers_count":28,"open_issues_count":8,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-12T07:16:47.195Z","etag":null,"topics":["java","mustache","template-engine","type-safe"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sviperll.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":"2014-12-05T16:17:03.000Z","updated_at":"2023-12-28T13:40:17.000Z","dependencies_parsed_at":"2022-08-22T05:00:38.655Z","dependency_job_id":null,"html_url":"https://github.com/sviperll/static-mustache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sviperll%2Fstatic-mustache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sviperll%2Fstatic-mustache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sviperll%2Fstatic-mustache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sviperll%2Fstatic-mustache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sviperll","download_url":"https://codeload.github.com/sviperll/static-mustache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221811374,"owners_count":16884305,"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":["java","mustache","template-engine","type-safe"],"created_at":"2024-10-11T07:16:43.397Z","updated_at":"2024-10-28T09:16:29.390Z","avatar_url":"https://github.com/sviperll.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"Static mustache\n===============\n\nLogicless text templating engine.\nTemplates are compiled alone with Java-sources.\nValue bindings are statically checked.\n\nFeatures\n--------\n\n * Logicless templating language.\n\n * [Mustache](http://mustache.github.io/) syntax.\n\n * Templates are compiled into effective code\n\n * Value bindings are statically checked.\n\n * Methods, fields and getter-methods can be referenced in templates.\n\n * Friendly error messages with context.\n\n * Zero configuration. No plugins or tweaks are required.\n   Everything is done with standard javac with any IDE and/or build-system.\n\n * Non-HTML templates are supported. Set of supported formats is extensible.\n\n * Layouts are supported, i. e. generation of header and footer from one template.\n\nInstallation\n------------\n\nUse maven dependency:\n\n```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003ecom.github.sviperll\u003c/groupId\u003e\n        \u003cartifactId\u003estatic-mustache\u003c/artifactId\u003e\n        \u003cversion\u003e0.4\u003c/version\u003e\n    \u003c/dependency\u003e\n```\n\n\n\n\n\nChangelog\n---------\n\n * Since 0.3\n\n   Switch to metachicory version 0.24. Replace text-formats dependency with chicory-text 0.24.\n   Add support for layouts, see `Html5Layout` example.\n\n * Since 0.2\n\n   Introduce metachicory 0.17 and text-formats 0.17 dependencies\n\nExample\n-------\n\n### user.mustache ###\n\n```\n{{#name}}\n\u003cp\u003eName: {{.}}, Name Length is {{length}}\u003c/p\u003e\n{{/name}}\n\n\u003cp\u003eAge: {{  age  }}\u003c/p\u003e\n\n\u003cp\u003eAchievements:\u003c/p\u003e\n\n\u003cul\u003e\n{{#array}}\n  \u003cli\u003e{{.}}\u003c/li\u003e\n{{/array}}\n\u003c/ul\u003e\n\n{{^array}}\n\u003cp\u003eNo achievements\u003c/p\u003e\n{{/array}}\n\n\u003cp\u003eItems:\u003c/p\u003e\n\n\u003col\u003e\n{{#list1}}\n  \u003cli\u003e{{value}}\u003c/li\u003e\n{{/list1}}\n\u003c/ol\u003e\n```\n\n### User.java ###\n\nFollowing class can be used to provide actual data to fill into above template.\n\n```java\n@GenerateRenderableAdapter(\n    // points to src/main/resources/user.mustache file\n    template = \"user.mustache\",\n\n    // adapterName can be omitted. \"Renderable{{className}}Adapter\" name is used by default\n    adapterName = \"RenderableHtmlUserAdapter\")\npublic class User {\n    final String name;\n    final int age;\n    final String[] array;\n    final List\u003cItem\u003cString\u003e\u003e list1;\n\n    public User(String name, int age, String[] array, List\u003cItem\u003cString\u003e\u003e list1) {\n        this.name = name;\n        this.age = age;\n        this.array = array;\n        this.list1 = list1;\n    }\n\n    public static class Item\u003cT\u003e {\n        private final T value;\n        public Item(T value) {\n            this.value = value;\n        }\n        T value() {\n            return value;\n        }\n    }\n}\n```\n\n\n### Rendering ###\n\nNew class `RenderableHtmlUserAdapter` will be mechanically generated with the above code. This class can be used to render template filled with actual data. To render template following code can be used:\n\n```java\nclass Main {\n    public static void main(String[] args) throws IOException {\n        User user = new User(\"John Doe\", 21, new String[] {\"Knowns nothing\"}, list);\n        Renderable\u003cHtml\u003e renderable = new RenderableHtmlUserAdapter(user);\n\n        // Any appendable will do: StringBuilder, Writer, OutputStream\n        Renderer renderer = renderable.createRenderer(System.out);\n\n        // Write rendered template\n        renderer.render();\n    }\n}\n```\n\nThe result of running this code will be\n\n```\n\u003cp\u003eName: John Doe, Name Length is 8\u003c/p\u003e\n\n\n\u003cp\u003eAge: 21\u003c/p\u003e\n\n\u003cp\u003eAchievements:\u003c/p\u003e\n\n\u003cul\u003e\n\n  \u003cli\u003eKnowns nothing\u003c/li\u003e\n\n\u003c/ul\u003e\n\n\n\n\u003cp\u003eItems:\u003c/p\u003e\n\n\u003col\u003e\n\n  \u003cli\u003ehelmet\u003c/li\u003e\n\n  \u003cli\u003eshower\u003c/li\u003e\n\n\u003c/ol\u003e\n```\n\nReferencing non existent fields, or fields with non renderable type, all result in compile-time errors. These errors are reported at your project's compile-time alone with other possible errors in java sources.\n\n```\ntarget/classes/user.mustache:5: error: Field not found in current context: 'age1'\n  \u003cp\u003eAge: {{  age1  }} ({{birthdate}}) \u003c/p\u003e\n                  ^\n  symbol: mustache directive\n  location: mustache template\n```\n\n```\ntarget/classes/user.mustache:5: error: Unable to render field: type error: Can't render data.birthdate expression of java.util.Date type\n  \u003cp\u003eAge: {{  age  }} ({{birthdate}}) \u003c/p\u003e\n                                    ^\n  symbol: mustache directive\n  location: mustache template\n```\n\nSee `static-mustache-examples` project for more examples.\n\nDesign\n------\n\nThe idea is to create templating engine combining [mustache](http://mustache.github.io/) logicless philosophy\nwith Java's single responsibility and static-typing.\nFull compile-time check of syntax and data-binding is the main requirement.\n\nCurrently Java-code is generated for templates. Generated Java-code should never fail to compile.\nIf it is impossible to generate valid Java-code from some template,\nfriendly compile-time error pointing to template file should be generated.\nUsers should never be exposed to generated Java-code.\n\nOriginal mustache uses Javascript-objects to define rendering context.\nFields of selected Javascript-objects are binded with template fields.\n\nStatic mustache uses Java-objects to define rendering context.\nBinding of template fields is defined and checked at compile-time.\nMissing fields are compile-time error.\n\nCurrent differences from mustache\n---------------------------------\n\n * Lambdas are not supported\n * Partials are not supported\n * Delimiter redefinition is not supported\n\nInterpretation of Java-types and values\n---------------------------------------\n\nSee [original mustache manual](http://mustache.github.io/mustache.5.html).\n\nWhen some value is null nothing is rendered for this mustache-variable or mustache-section anyway.\n\nBoxed and unboxed booleans can be used for mustache-sections. Section is only rendered if value is true.\n\nArrays and Iterables can be used in mustache-sections and are treated like Javascript-arrays in original mustache.\n\nAny non-null object can be used for mustache-section.\nThis object serves like a data-binding context for given section.\n\nData-binding contexts are nested.\nNames are looked up in innermost context first.\nIf name is not found in current context, parent context is inspected.\nThis process continues up to root context.\n\nIn each rendering context name lookup is performed as following.\nLookup is performed in Java-class serving as a rendering context.\n\n 1. Method with requested name is looked up.\n    Method should have no arguments and should throw no checked exceptions.\n    If there is such method it is used to fetch actual data to render.\n    Compile-time error is raised if there is method with given name, but\n    it is not accessible, has parameters or throws checked exceptions.\n\n 2. Method with getter-name for requested name is looked up.\n    (For example, if 'age' is requested, 'getAge' method is looked up.)\n    Method should have no arguments and should throw no checked exceptions.\n    If there is such method it is used to fetch actual data to render.\n    Compile-time error is raised if there is method with such name, but\n    it is not accessible, has parameters or throws checked exceptions.\n\n 3. Field with requested name is looked up.\n    Compile-time error is raised if there is field with such name but it's not accessible.\n\n 4. Search continues in parent-context if all of the above failed.\n\nPrimitive types and strings can be used in mustache-variables.\n\nEscaping is always performed for mustache-variables.\n\nUnescaped variables are supported as in original mustache.\n\nAny boxed or unboxed primitive type is rendered with toString method.\nStrings are rendered as is.\n\nRendering of other Java-types as mustache-variable is currently compile-time error.\n\nLicense\n-------\n\nStatic mustache is under BSD 3-clause license.\n\nFlattr\n------\n\n[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=sviperll\u0026url=https%3A%2F%2Fgithub.com%2Fsviperll%2Fstatic-mustache\u0026title=static-mustache\u0026language=Java\u0026tags=github\u0026category=software)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsviperll%2Fstatic-mustache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsviperll%2Fstatic-mustache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsviperll%2Fstatic-mustache/lists"}