Return HTML or Slime Inside Phoenix Views?

Hello all,

I’m wondering if someone can tell me which would be more performant inside Phoenix Views/Templates. I’m using Slime as my template engine because I find the syntax a lot easier to read and work with than HTML because it’s concise and has a lot of white space.

In my templates there are several places where the generated text/html is based on options the user has selected. Basically, it’s a multi-step form. To keep my Templates clean I call a function in the View that has a case statement to determine which HTML to send back.

e.g.

In my Template.html.slimeex
= option_a_or_b(foo)

In my View.ex

def option_a_or_b(foo) do
  case foo do
    "option A" -> raw('<h1>This is option A</h1>')
    "option B" -> raw('<h2>This is option B</h2>')
  end
end

So in my function I am returning HTML to the Slime template. Would it be more performant to instead return Slime? Is it even possible to return Slime syntax (I’m actually not sure and can’t find this answer either).

It appears to work fine this way, I’m just curious how the internals work of Template Engines and what is a better practice.

Returning the HTML is probably the most performant, since the Slime engine likely just inlines the HTML.

3 Likes

Response from Chris McCord on Slack

chrismccord 6:14 AM
does slime have it’s own sigil/macro to build markup dynamically?
you can use the ~E sigil to build eex, so you get HTML escaping and it will be just as fast as a template, because that’s all a template is

title = ...
~E """
<h1><%= title %></h1>
"""

Waiting for an answer if this approach is better than
raw('<h1>#{title}</h1>')

1 Like

Slime implements a sigil.

4 Likes