# Rendering EEx from a string

**URL:** <https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588>\
**Category:** Discussions\
**Tags:** phoenix, heex\
**Created:** [May 5, 2022, 11:52am UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588 "2022-05-05T11:52:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![braindeaf](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/braindeaf/32/13906_2.png) [@braindeaf](https://forum.elixirforum.com/u/braindeaf)\
**Post date:** [May 5, 2022, 11:52am UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/1 "2022-05-05T11:52:37Z")

</div>

Hi, there,

I’ve taken on the enviable task of porting my shoddy Rails blog to Phoenix and it’s posed some interesting challenges. I have posts written in Textile and I need to replace some syntax to convert to Markdown before converting to HTML and now I’m on my custom Pages which where written in HAML. Ok I can convert from HAML to EEx which is fine and rewrite link\_to to link, etc. But now I need to evaluate that template as a string.

Can anyone point me in the right direction, I appreciate I need the @conn to give whichever function I am after to render with the right context but _render_ seems to always want a compiled template.

Any help is very much appreciated 🙂

RobL

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [May 5, 2022, 12:21pm UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/2 "2022-05-05T12:21:12Z")

</div>

> [@braindeaf](#):
>
> But now I need to evaluate that template as a string.

Can you elaborate on this? Where are the templates stored that they cannot be compiled?

---

<div class="post-metadata">

**Author:** ![josevalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/32/1787_2.png) [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Post date:** [May 5, 2022, 12:47pm UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/3 "2022-05-05T12:47:19Z")

</div>

Keep in mind that all a controller does is to call render/2 on the view. And you can define that manually:

```
defmodule MyView do
  def render(template, assigns) do
    ...
  end
end

```

So you could do:

```
defmodule MyView do
  def render(template, assigns) do
    EEx.eval_file(template, assigns)
  end
end

```

But of course be careful so you don’t allow someone to evaluate any file on your system. As Ben said, compiling templates is going to be the safest and most efficient.

---

<div class="post-metadata">

**Author:** ![braindeaf](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/braindeaf/32/13906_2.png) [@braindeaf](https://forum.elixirforum.com/u/braindeaf)\
**Post date:** [May 5, 2022, 1:27pm UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/4 "2022-05-05T13:27:41Z")

</div>

Thanks for the reply both 🙂 I wasn’t very clear, my apologies. What I meant was instead of a file template I have a string. Read directly from the DB it had something like.

```elixir
%section
    %h1= link_to "SOMETHING", "http://whatever.org"

```

I’ve then converted it into something like

```elixir
"<section><h1><%= link \"SOMETHING\", \"http://whatever.org\" %></h1></section>"

```

So now I have to eval this string so I could do something like this I think.

```elixir
EEx.eval_string("<%= link \"BLAH\", \"BLAH\" %>")

```

I realise now this is sounding ridiculous because I could just replace link\_to with

```elixir
<a href="BLAH">BLAH</a>

```

And forget about using the link function, but templates must be rendered in such a way that they have access to those View functions so I guess I’m just trying to understand how this all fits together.

Cheers guys

RobL

---

<div class="post-metadata">

**Author:** ![cblavier](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cblavier/32/29763_2.png) [@cblavier](https://forum.elixirforum.com/u/cblavier)\
**Post date:** [May 5, 2022, 4:12pm UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/5 "2022-05-05T16:12:37Z")

</div>

I have a project where I might be doing what you’re asking: rendering HTML from fragments of `eex` stored in JSON files.

Here is the code we’re using:

```elixir
alias Phoenix.LiveView.Engine, as: LiveViewEngine
alias Phoenix.LiveView.HTMLEngine

def render_markup(markup) do
  quoted_code = EEx.compile_string(markup, engine: HTMLEngine)
  
  {evaluated, _} =
    Code.eval_quoted(quoted_code, [assigns: []],
      aliases: component_aliases(),
      requires: [Kernel],
      functions: [
        {Phoenix.LiveView.Helpers, [live_component: 1, live_file_input: 2]}
      ]
    )
  
  LiveViewEngine.live_to_iodata(evaluated)
end

```

let me know if it’s helpful!

---

<div class="post-metadata">

**Author:** ![egze](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/egze/32/13522_2.png) [@egze](https://forum.elixirforum.com/u/egze)\
**Post date:** [May 5, 2022, 7:03pm UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/6 "2022-05-05T19:03:19Z")

</div>

Just curious, what’s an example markup that you would pass into the function?

---

<div class="post-metadata">

**Author:** ![cblavier](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/cblavier/32/29763_2.png) [@cblavier](https://forum.elixirforum.com/u/cblavier)\
**Post date:** [May 5, 2022, 7:14pm UTC](https://forum.elixirforum.com/t/rendering-eex-from-a-string/47588/7 "2022-05-05T19:14:00Z")

</div>

Something like this:

```elixir
<.button color={:primary} outline={true} text="Save" type="button"/>

```

We use it for our liveview storybook: [https://storybook.phenixgroupe.com](https://storybook.phenixgroupe.com)
