Skip to content

Commit

Permalink
feat: Add before_render, around_render and after_render (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Oct 20, 2024
1 parent 483eb21 commit c6259e6
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 70 deletions.
63 changes: 0 additions & 63 deletions spec/blueprint/html/enveloping_spec.cr

This file was deleted.

77 changes: 77 additions & 0 deletions spec/blueprint/html/hooks_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
require "../../spec_helper"

private class ExampleWithBlock
include Blueprint::HTML

private def blueprint(&)
h1 { yield }
end

private def before_render(&)
span { "Before render" }
end

private def around_render(&)
span { "Around start" }
yield
span { "Around end" }
end

private def after_render(&)
span { "After render" }
end
end

private class ExampleWithoutBlock
include Blueprint::HTML

private def blueprint
h1 "Without block"
end

private def before_render(&)
span { "Before render" }
end

private def around_render(&)
span { "Around start" }
yield
span { "Around end" }
end

private def after_render(&)
span { "After render" }
end
end

describe "hooks" do
context "with block" do
it "allows defining hooks before_render, around_render, after_render" do
actual_html = ExampleWithBlock.new.to_s { "With block" }
expected_html = normalize_html <<-HTML
<span>Before render</span>
<span>Around start</span>
<h1>With block</h1>
<span>Around end</span>
<span>After render</span>
HTML

actual_html.should eq expected_html
end
end

context "without block" do
it "allows defining hooks before_render, around_render, after_render" do
actual_html = ExampleWithoutBlock.new.to_s
expected_html = normalize_html <<-HTML
<span>Before render</span>
<span>Around start</span>
<h1>Without block</h1>
<span>Around end</span>
<span>After render</span>
HTML

actual_html.should eq expected_html
end
end
end
34 changes: 27 additions & 7 deletions src/blueprint/html.cr
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,44 @@ module Blueprint::HTML

@buffer = buffer

envelope { blueprint }
{% if @type.has_method?(:before_render) %}
before_render { }
{% end %}

{% if @type.has_method?(:around_render) %}
around_render { blueprint }
{% else %}
blueprint
{% end %}

{% if @type.has_method?(:after_render) %}
after_render { }
{% end %}
end

def to_s(buffer : String::Builder, &) : Nil
return unless render?

@buffer = buffer

envelope do
{% if @type.has_method?(:before_render) %}
before_render { yield }
{% end %}

{% if @type.has_method?(:around_render) %}
around_render do
blueprint { capture_content { yield } }
end
{% else %}
blueprint { capture_content { yield } }
end
{% end %}

{% if @type.has_method?(:after_render) %}
after_render { yield }
{% end %}
end

private def render? : Bool
true
end

private def envelope(&) : Nil
yield
end
end

0 comments on commit c6259e6

Please sign in to comment.