From 19db585c4d3df3d5f96a22ed7cf3d28de8be1b17 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 8 Sep 2024 18:38:27 -0300 Subject: [PATCH] feat: Allow passing element content without using block --- CHANGELOG.md | 14 +++++++++++++ benchmark/main.cr | 20 +++++++++---------- shard.yml | 4 ++-- spec/blueprint/html/custom_elements_spec.cr | 10 ++++++++++ spec/blueprint/html/renderer_spec.cr | 6 +++--- spec/blueprint/html/standard_elements_spec.cr | 8 ++++++++ spec/blueprint/html_spec.cr | 4 ++-- src/blueprint/html/element_registrar.cr | 15 ++++++++++++++ src/blueprint/version.cr | 2 +- 9 files changed, 65 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a5560b..869b16d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,24 @@ All notable changes to this project will be documented on . +## [0.6.0] - 2023-04-25 + +Allows passing content to elements without using blocks, eg. + +```crystal + h1 { "Hello World!" } + # or + h1 "Hello World!" +``` + +Release details: + ## [0.5.1] - 2023-04-25 Fix Crystal version string requirement. +Release details: + ## [0.5.0] - 2023-04-25 Performance improvements: Increased speed execution by 15%. diff --git a/benchmark/main.cr b/benchmark/main.cr index 759d6f5..78fc4ab 100644 --- a/benchmark/main.cr +++ b/benchmark/main.cr @@ -9,7 +9,7 @@ class Example::LayoutComponent def blueprint(&) html do head do - title { @title } + title @title meta name: "viewport", content: "width=device-width,initial-scale=1" link href: "/assets/tailwind.css", rel: "stylesheet" end @@ -17,9 +17,9 @@ class Example::LayoutComponent body class: "bg-zinc-100" do nav class: "p-5", id: "main_nav" do ul do - li(class: "p-5") { a(href: "/") { "Home" } } - li(class: "p-5") { a(href: "/about") { "About" } } - li(class: "p-5") { a(href: "/contact") { "Contact" } } + li(class: "p-5") { a("Home", href: "/") } + li(class: "p-5") { a("About", href: "/about") } + li(class: "p-5") { a("Contact", href: "/contact") } end end @@ -36,28 +36,28 @@ class Example::Page def blueprint render Example::LayoutComponent.new do - h1 { "Hi" } + h1 "Hi" table id: "test", class: "a b c d e f g" do tr do td id: "test", class: "a b c d e f g" do - span { "Hi" } + span "Hi" end td id: "test", class: "a b c d e f g" do - span { "Hi" } + span "Hi" end td id: "test", class: "a b c d e f g" do - span { "Hi" } + span "Hi" end td id: "test", class: "a b c d e f g" do - span { "Hi" } + span "Hi" end td id: "test", class: "a b c d e f g" do - span { "Hi" } + span "Hi" end end end diff --git a/shard.yml b/shard.yml index acfb2d9..996a538 100644 --- a/shard.yml +++ b/shard.yml @@ -2,7 +2,7 @@ name: blueprint description: | Blueprint is a lib for writing reusable and testable HTML templates in plain Crystal, allowing an OOP (Oriented Object Programming) approach when building your views. -version: 0.5.1 +version: 0.6.0 authors: - Stephann V. <3025661+stephannv@users.noreply.github.com> @@ -14,4 +14,4 @@ license: MIT development_dependencies: ameba: github: crystal-ameba/ameba - version: 1.5.0 + version: 1.6.1 diff --git a/spec/blueprint/html/custom_elements_spec.cr b/spec/blueprint/html/custom_elements_spec.cr index a38614a..1da08a2 100644 --- a/spec/blueprint/html/custom_elements_spec.cr +++ b/spec/blueprint/html/custom_elements_spec.cr @@ -9,6 +9,7 @@ private class DummyPage private def blueprint div do v_btn(href: "#home", data: {id: 12, visible: true, disabled: false}) { "Home" } + v_btn("Contact", href: "#contact") card end end @@ -24,6 +25,15 @@ describe "Blueprint::HTML custom elements registration" do page.to_html.should contain expected_html end + it "allows passing content as first argument" do + page = DummyPage.new + expected_html = <<-HTML.strip + Contact + HTML + + page.to_html.should contain expected_html + end + it "allows empty custom elements" do page = DummyPage.new expected_html = <<-HTML.strip diff --git a/spec/blueprint/html/renderer_spec.cr b/spec/blueprint/html/renderer_spec.cr index 152a56c..13759c2 100644 --- a/spec/blueprint/html/renderer_spec.cr +++ b/spec/blueprint/html/renderer_spec.cr @@ -10,9 +10,9 @@ private class DummyPage span { "Passing content to component" } end - render ComplexComponent.new do |c| - c.title { "My card" } - c.body { "Card content" } + render ComplexComponent.new do |card| + card.title { "My card" } + card.body { "Card content" } footer { "Footer tag" } end end diff --git a/spec/blueprint/html/standard_elements_spec.cr b/spec/blueprint/html/standard_elements_spec.cr index ddd12ec..e53c7fc 100644 --- a/spec/blueprint/html/standard_elements_spec.cr +++ b/spec/blueprint/html/standard_elements_spec.cr @@ -18,6 +18,8 @@ private class DummyPage {{element.id}} {{element.id}}(attribute: "test") {{element.id}} { "content" } + {{element.id}}("content") + {{element.id}}("content", attribute: "test") {{element.id}}(attribute: "test") { "content" } {% end %} @@ -34,6 +36,8 @@ private class DummyPage select_tag select_tag(attribute: "test") select_tag { "content" } + select_tag("content") + select_tag("content", attribute: "test") select_tag(attribute: "test") { "content" } end end @@ -46,6 +50,8 @@ describe "Blueprint::HTML standard HTML elements" do io << "<" << tag << ">" << "" io << "<" << tag << " attribute=\"test\">" << "" io << "<" << tag << ">content" << "" + io << "<" << tag << ">content" << "" + io << "<" << tag << " attribute=\"test\">content" << "" io << "<" << tag << " attribute=\"test\">content" << "" end @@ -62,6 +68,8 @@ describe "Blueprint::HTML standard HTML elements" do io << "" io << "" io << "" + io << "" + io << "" io << "" end diff --git a/spec/blueprint/html_spec.cr b/spec/blueprint/html_spec.cr index 95d20a6..3855b50 100644 --- a/spec/blueprint/html_spec.cr +++ b/spec/blueprint/html_spec.cr @@ -78,8 +78,8 @@ private class ExamplePage end private def article(title : String, content : String) - render ArticleComponent.new(title) do |c| - c.body { content } + render ArticleComponent.new(title) do |article| + article.body { content } end end end diff --git a/src/blueprint/html/element_registrar.cr b/src/blueprint/html/element_registrar.cr index 38ed26a..f6d214c 100644 --- a/src/blueprint/html/element_registrar.cr +++ b/src/blueprint/html/element_registrar.cr @@ -9,6 +9,10 @@ module Blueprint::HTML private def {{method_name.id}}(**attributes) : Nil element({{tag}}, **attributes) { "" } end + + private def {{method_name.id}}(__content__ : String, **attributes) : Nil + element({{tag}}, __content__, **attributes) + end end macro register_empty_element(method_name, tag = nil) @@ -38,6 +42,17 @@ module Blueprint::HTML @buffer << ">" end + private def element(_tag_name : String | Symbol, __content__ : String, **attributes) : Nil + @buffer << "<" + @buffer << _tag_name + @buffer << parse_attributes(attributes) + @buffer << ">" + @buffer << __content__ + @buffer << "" + end + private def void_element(_tag_name : String | Symbol, **attributes) : Nil @buffer << "<" @buffer << _tag_name diff --git a/src/blueprint/version.cr b/src/blueprint/version.cr index 49c44a2..21e7006 100644 --- a/src/blueprint/version.cr +++ b/src/blueprint/version.cr @@ -1,3 +1,3 @@ module Blueprint - VERSION = "0.5.1" + VERSION = "0.6.0" end