Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow passing element content without using block #53

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

All notable changes to this project will be documented on <https://stephannv.github.io/blueprint-docs/>.

## [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: <https://stephannv.github.io/blueprint-docs/changelogs/v0.6.0/>

## [0.5.1] - 2023-04-25

Fix Crystal version string requirement.

Release details: <https://stephannv.github.io/blueprint-docs/changelogs/v0.5.1/>

## [0.5.0] - 2023-04-25

Performance improvements: Increased speed execution by 15%.
Expand Down
20 changes: 10 additions & 10 deletions benchmark/main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ 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

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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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. <[email protected]>
Expand All @@ -14,4 +14,4 @@ license: MIT
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: 1.5.0
version: 1.6.1
10 changes: 10 additions & 0 deletions spec/blueprint/html/custom_elements_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
<v-btn href="#contact">Contact</v-btn>
HTML

page.to_html.should contain expected_html
end

it "allows empty custom elements" do
page = DummyPage.new
expected_html = <<-HTML.strip
Expand Down
6 changes: 3 additions & 3 deletions spec/blueprint/html/renderer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/blueprint/html/standard_elements_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}

Expand All @@ -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
Expand All @@ -46,6 +50,8 @@ describe "Blueprint::HTML standard HTML elements" do
io << "<" << tag << ">" << "</" << tag << ">"
io << "<" << tag << " attribute=\"test\">" << "</" << tag << ">"
io << "<" << tag << ">content" << "</" << tag << ">"
io << "<" << tag << ">content" << "</" << tag << ">"
io << "<" << tag << " attribute=\"test\">content" << "</" << tag << ">"
io << "<" << tag << " attribute=\"test\">content" << "</" << tag << ">"
end

Expand All @@ -62,6 +68,8 @@ describe "Blueprint::HTML standard HTML elements" do
io << "<select></select>"
io << "<select attribute=\"test\"></select>"
io << "<select>content</select>"
io << "<select>content</select>"
io << "<select attribute=\"test\">content</select>"
io << "<select attribute=\"test\">content</select>"
end

Expand Down
4 changes: 2 additions & 2 deletions spec/blueprint/html_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/blueprint/html/element_registrar.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 << "</"
@buffer << _tag_name
@buffer << ">"
end

private def void_element(_tag_name : String | Symbol, **attributes) : Nil
@buffer << "<"
@buffer << _tag_name
Expand Down
2 changes: 1 addition & 1 deletion src/blueprint/version.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Blueprint
VERSION = "0.5.1"
VERSION = "0.6.0"
end
Loading