braindeaf
Rendering EEx from a string
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
Most Liked Responses
cblavier
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:
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!
cblavier
Something like this:
<.button color={:primary} outline={true} text="Save" type="button"/>
We use it for our liveview storybook: https://storybook.phenixgroupe.com
benwilson512
Can you elaborate on this? Where are the templates stored that they cannot be compiled?








