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
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
Popular in Discussions
Other popular topics
Latest Phoenix Threads
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









