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