mpope

mpope

How include layouts with render_to_iodata

I am trying to cache static web pages in ETS, and using render_to_iodata does not seem to include the layout. I was wondering the proper way to do this. Here is how I am currently using it in my controller:

def post(conn, _param) do
  rendered_page = Phoenix.View.render_to_iodata(BlogWeb.LayoutView, "app.html", 
      %{  
        conn: conn,
        view_module: BlogWeb.PostView,
        view_template: "post.html"
      })  
  html(conn, rendered_page)
end

What happens is that this spins forever and nothing gets loaded, and nothing is really logged to the console.

My page.html.eex:

Hello, world!

And the layout/app.html.eex (shortened) is:

<h1>Welcome!</h1>
<%= render @view_module, @view_template, assigns %>

I have a barebones view for each. I am using the default layout.

Marked As Solved

jola

jola

Since you’re not getting any errors it’s hard to say what’s wrong. I’ve never tried to do it that way, but I can suggest an alternative solution. You can cache rendered content by using Plug.Conn.register_before_send/2. Here’s some untested code

def post(conn, _param) do
  conn
  |> Plug.Conn.register_before_send(cache/1)
  |> render("post.html")
end

def cache(conn) do
  content = :erlang.iolist_to_binary(conn.resp_body)
  :ets.insert(:table, {you_pick_your_key, content})
  conn
end

The reason you want to coerce the iodata to binary is that based on experience the ETS memory usage will balloon if you don’t.

This solution also means you can create a Plug responsible for checking the cache and caching responses, and add it to a pipeline. I have an example of that here cache_example/lib/cache_example_web/plugs/item_cache2.ex at master · joladev/cache_example · GitHub

Note that the example is specific to my use case, you’d want to adjust it to yours :slight_smile:

Last Post!

mpope

mpope

This seems much more idomatic. Thank you very much!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement