Skip to content
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
31 changes: 31 additions & 0 deletions spec/instance_template/macro_for_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "../spec_helper"

module ToHtml::InstanceTemplate::MacroForSpec
class MyView
getter a : String
getter b : String

def initialize(@a, @b); end

ToHtml.instance_template do
form do
{% for ivar in @type.instance_vars %}
input type: :text, name: {{ivar.name.stringify}}, value: {{ivar.name.id}}
{% end %}
end
end
end

describe "MyView#to_html" do
it "should return the correct HTML" do
expected = <<-HTML.squish
<form>
<input type="text" name="a" value="foo">
<input type="text" name="b" value="bar">
</form>
HTML

MyView.new(a: "foo", b: "bar").to_html.should eq(expected)
end
end
end
27 changes: 27 additions & 0 deletions spec/instance_template/macro_if_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "../spec_helper"

module ToHtml::InstanceTemplate::MacroIfSpec
class MyView
ToHtml.class_template do
{% if false %}
p do
"Not shown"
end
{% else %}
p do
"Shown"
end
{% end %}
end
end

describe "MyView#to_html" do
it "should return the correct HTML" do
expected = <<-HTML.squish
<p>Shown</p>
HTML

MyView.to_html.should eq(expected)
end
end
end
19 changes: 19 additions & 0 deletions src/instance_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ module ToHtml
{% end %}
{% end %}
end
{% elsif blk.body.is_a?(MacroIf) %}
\{% if {{blk.body.cond}} %}
ToHtml.to_html_eval_exps({{io}}, {{indent_level}}) do
{{blk.body.then}}
end
\{% else %}
ToHtml.to_html_eval_exps({{io}}, {{indent_level}}) do
{{blk.body.else}}
end
\{% end %}
{% elsif blk.body.is_a?(MacroFor) %}
\{% for {{blk.body.vars.splat}} in {{blk.body.exp}} %}
ToHtml.to_html_eval_exps({{io}}, {{indent_level}}) do
# Crystal seems to insert a newline after each macro variable for some reason
{{blk.body.body.stringify.gsub(/\n/, "").id}}
end
\{% end %}
{% elsif blk.body.is_a?(MacroExpression) || blk.body.is_a?(MacroLiteral) %}
{{blk.body}}
{% elsif blk.body.is_a?(Assign) %}
# Don't print this to the IO
{{blk.body}}
Expand Down