# Help with if-like macro

**URL:** <https://discourse.julialang.org/t/help-with-if-like-macro/31856>\
**Category:** General Usage\
**Created:** [December 4, 2019, 2:32pm UTC](https://discourse.julialang.org/t/help-with-if-like-macro/31856 "2019-12-04T14:32:54Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [December 4, 2019, 7:08pm UTC](https://discourse.julialang.org/t/help-with-if-like-macro/31856/21 "2019-12-04T19:08:27Z")

</div>

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

> [@yuyichao](#):
>
> (Again, as I said in another thread, you almost never need to completely throw out expression literal and interpolation just because you can’t construct one particular level)

Indeed, writing macros in that way is quite challenging.

---

<div class="post-metadata">

**Author:** ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)\
**Post date:** [December 4, 2019, 7:27pm UTC](https://discourse.julialang.org/t/help-with-if-like-macro/31856/22 "2019-12-04T19:27:26Z")

</div>

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:

```nohighlight
<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

```julia
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.

[Previous page](https://discourse.julialang.org/t/help-with-if-like-macro/31856.md?page=1)
