{"id":35012200,"url":"https://github.com/forax/html-component","last_synced_at":"2025-12-27T05:00:21.669Z","repository":{"id":229073468,"uuid":"775550367","full_name":"forax/html-component","owner":"forax","description":"A very small library to define unmodifiable HTML/XML components","archived":false,"fork":false,"pushed_at":"2024-05-03T22:01:55.000Z","size":526,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-03T23:21:56.236Z","etag":null,"topics":["components","htmx","java","string-template"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/forax.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-21T15:43:38.000Z","updated_at":"2024-05-03T23:21:57.109Z","dependencies_parsed_at":"2024-03-27T10:25:40.717Z","dependency_job_id":"ea72546d-b622-4fb2-aed2-e984dd0bf9c1","html_url":"https://github.com/forax/html-component","commit_stats":null,"previous_names":["forax/html-component"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/forax/html-component","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fhtml-component","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fhtml-component/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fhtml-component/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fhtml-component/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forax","download_url":"https://codeload.github.com/forax/html-component/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forax%2Fhtml-component/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28072674,"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","status":"online","status_checked_at":"2025-12-27T02:00:05.897Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["components","htmx","java","string-template"],"created_at":"2025-12-27T05:00:05.465Z","updated_at":"2025-12-27T05:00:21.662Z","avatar_url":"https://github.com/forax.png","language":"Java","readme":"# html-component\nA very small library to define unmodifiable HTML/XML components in Java\n\nHtml components are\n- safe: the template is not string based and checked by an XML parser\n- composable: a component can reference another component statically or dynamically\n- lightweight: the whole jar is less than 20kb\n- defined in Java: no need to learn another template language, and it works with all Java IDEs.\n\nA html component is a record that implements the interface `Component` thus provides a `render` method.\n\n```java\nrecord HelloWorld(String name) implements Component {\n  public Renderer render() {\n    return $.\"\"\"\n      \u003cdiv\u003eHello \\{name} !\u003c/div\u003e\n      \"\"\";\n  } \n}\n```\n\nIt uses the mechanism of StringTemplate Processor in preview in Java 21/22 ([JEP 459](https://openjdk.org/jeps/459))\nto generate XML events from a template. This makes it impossible to generate invalid fragment of an XML document.\n\nBecause it's a plain record, it can be instantiated like any Java classes using `new`\n```java\nvar hello = new HelloWorld(\"html component\");\nSystem.out.println(hello.render());\n```\n\nA html component can reference statically another html component if they are both declared in the same class\nThe name of the component has to start with a letter in uppercase to differentiate it from a regular XML element.\n```java\nrecord TwoHello(String name1, String name2) implements Component {\n  public Renderer render() {\n    return $.\"\"\"\n      \u003cdiv\u003e\n        \u003cHelloWord name=\"\\{name1}\"/\u003e\n        \u003cHelloWord name=\"\\{name2}\"/\u003e\n      \u003c/div\u003e\n      \"\"\";\n  }\n}\n```\n\nA html component can reference dynamically any other components, using the method `Renderer.from(stream)`\n```java\nrecord HelloList(List\u003cHelloWorld\u003e hellos) implements Component {\n  public Renderer render() {\n    return $.\"\"\"\n      \u003cdiv\u003e\n        \\{ Renderer.from(hellos.stream()) }\n      \u003c/div\u003e\n      \"\"\";\n  }\n}\n```\n\nHere are 3 more demos ([Demo.java](src/test/java/Demo.java), [Demo2.java](src/test/java/Demo2.java) and\n[DemoList.java](src/test/java/DemoList.java)).\nAnd that's all for the spec part.\n\n## Using html components with htmx\n\nHtml components are a good fit for [htmx](https://htmx.org/) because it's an easy way to create XML fragments\nthat can be downloaded and patched by the JavaScript library htmx.\n\n[HTMXDemo](src/test/java/HTMXDemo.java) shows how html components can be used\nin the context of htmx. The demo using a Java port of the Express.js library\n([JExpress.java](src/test/java/JExpress.java))\nbut this is not a requirement, it makes just the demo code simpler than using Spring Boot.\n\nTo run the demo with Java 22, after calling `mvn package` :\n```java\n  cd src/test/java\n  java --enable-preview --source 22 --class-path ../../../target/*.jar HTMXDemo.java\n```\nand in the browser `http://localhost:8080/university`\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforax%2Fhtml-component","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforax%2Fhtml-component","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforax%2Fhtml-component/lists"}