Skip to content

Instantly share code, notes, and snippets.

@benoittgt
Last active July 7, 2020 21:08
Show Gist options
  • Save benoittgt/0190f373c0fcad6a78b82f704d618861 to your computer and use it in GitHub Desktop.
Save benoittgt/0190f373c0fcad6a78b82f704d618861 to your computer and use it in GitHub Desktop.
require 'erb'
expected = %{
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
}
template_unindented = %{
<ul>
<% list = [1,2,3] %>
<% for item in list %>
<% if item %>
<li><%= item %></li>
<% end %>
<% end %>
</ul>
} # space is needed for correct formating
template_indented = %{
<ul>
<% list = [1,2,3] %>
<% for item in list %>
<% if item %>
<li><%= item %></li>
<% end %>
<% end %>
</ul>
}
template_indented_with_surrounded_dash = %{
<ul>
<%- list = [1,2,3] -%>
<%- for item in list -%>
<%- if item -%>
<li><%= item %></li>
<%- end -%>
<%- end -%>
</ul>
}
template_indented_with_ending_dash = %{
<ul>
<% list = [1,2,3] -%>
<% for item in list -%>
<% if item -%>
<li><%= item %></li>
<% end -%>
<% end -%>
</ul>
}
templates = [
{
name: 'template_unindented',
trim_mode: '<>',
content: template_unindented
},
{
name: 'template_indented',
trim_mode: '<>',
content: template_indented
},
{
name: 'template_indented',
trim_mode: '%>',
content: template_indented
},
{
name: 'template_indented',
trim_mode: '-',
content: template_indented
},
{
name: 'template_indented_with_surrounded_dash',
trim_mode: '-',
content: template_indented_with_surrounded_dash
},
{
name: 'template_indented_with_ending_dash',
trim_mode: '-',
content: template_indented_with_ending_dash
}
]
templates.each do |template|
puts "🎲 With trim_mode: #{template[:trim_mode]}, for #{template[:name]}\n\n"
pp ERB.new(template[:content], trim_mode: template[:trim_mode]).result
puts
end
puts "🎲 Expected\n\n"
pp expected
@benoittgt
Copy link
Author

benoittgt commented Jul 2, 2020

🎲  With trim_mode: <>, for template_unindented

"\n" +
"<ul>\n" +
"<li>1</li>\n" +
"<li>2</li>\n" +
"<li>3</li>\n" +
"</ul>\n" +
" "

🎲  With trim_mode: <>, for template_indented

"\n" +
"<ul>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>1</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>2</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>3</li>\n" +
"  \n" +
"  \n" +
"</ul>\n"

🎲  With trim_mode: %>, for template_indented

"\n" +
"<ul>\n" +
"        <li>1</li>\n" +
"        <li>2</li>\n" +
"        <li>3</li>\n" +
"    </ul>\n"

🎲  With trim_mode: -, for template_indented

"\n" +
"<ul>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>1</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>2</li>\n" +
"  \n" +
"  \n" +
"  \n" +
"  <li>3</li>\n" +
"  \n" +
"  \n" +
"</ul>\n"

🎲  With trim_mode: -, for template_indented_with_surrounded_dash

"\n" +
"<ul>\n" +
"  <li>1</li>\n" +
"  <li>2</li>\n" +
"  <li>3</li>\n" +
"</ul>\n"

🎲  With trim_mode: -, for template_indented_with_ending_dash

"\n" +
"<ul>\n" +
"        <li>1</li>\n" +
"        <li>2</li>\n" +
"        <li>3</li>\n" +
"    </ul>\n"

🎲  Expected

"\n" +
"<ul>\n" +
"  <li>1</li>\n" +
"  <li>2</li>\n" +
"  <li>3</li>\n" +
"</ul>\n"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment