braindeaf

braindeaf

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 :slight_smile:

RobL

Showing Posts 1 to 6

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

josevalim

josevalim

Creator of Elixir

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.

braindeaf

braindeaf OP

Thanks for the reply both :slight_smile: 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.

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

I’ve then converted it into something like

"<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.

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

I realise now this is sounding ridiculous because I could just replace link_to with

<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

cblavier

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!

egze

egze

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

cblavier

cblavier

Something like this:

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

We use it for our liveview storybook: https://storybook.phenixgroupe.com

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews