Help with if-like macro

Oh that’s much easier. I’ll keep that in mind :upside_down_face:.

Indeed, writing macros in that way is quite challenging.

Thanks, in this case, I think I correctly understood your point. The issue is that I don’t really “emit” the code. I tried to make the engine as generic as possible.

The overall workflow for the following code is something like the following.

Example template:

<div>
  <% if logged(user) %>

  <h2>Hello $(user.name)</h2>

  <ul class="menu">
    <li><a href="prefs.html">Preferences</a>
    <li><a href="logoff.html">Log off</a>
  </ul>

  <% end %>
</div>

Step 1: I replace all the <% with <script type="julia/eval"> and %> with </script>. This produces 100% pure/valid HTML.

Step 2: I parse the resulting HTML with Gumbo.jl

Step 3: I iterate over the resulting DOM in Julia and convert each node to a Julia function call, ex: <ul class="menu">...</ul> results in something like

Html.ul(
  [Html.li(...), Html.li(...)],  
  class="menu"
)

Step 4: if the tag name is script and the type is julia/eval I just output its content (so I don’t touch the user Julia code). As for the $ interpolated Julia code, that too, remains as is.

So when in the DOM I encounter something like <script type="julia/eval"> end </script> I have no idea if this end matches an opening if (nor do I need to care). This greatly reduces the complexity and the potential for mistakes.

1 Like