diff --git a/spec/instance_template/macro_for_spec.cr b/spec/instance_template/macro_for_spec.cr new file mode 100644 index 0000000..314085c --- /dev/null +++ b/spec/instance_template/macro_for_spec.cr @@ -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 +
+ + +
+ HTML + + MyView.new(a: "foo", b: "bar").to_html.should eq(expected) + end + end +end diff --git a/spec/instance_template/macro_if_spec.cr b/spec/instance_template/macro_if_spec.cr new file mode 100644 index 0000000..ff42eff --- /dev/null +++ b/spec/instance_template/macro_if_spec.cr @@ -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 +

Shown

+ HTML + + MyView.to_html.should eq(expected) + end + end +end diff --git a/src/instance_template.cr b/src/instance_template.cr index bdf24a8..430b27f 100644 --- a/src/instance_template.cr +++ b/src/instance_template.cr @@ -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}}