opsb
opsb

Reputation: 30211

render layout stored in database

Doing some integration work with another site I've got the unusual requirement of needing to create the layout at runtime.

At the moment I'm having to resort to something like this:

def new
  body = render_to_string 'new', :layout => false      
  page = add_layout(body, db.load_template)
  render :text => page
end

This is a bit awkward, I'd rather do something like:

def new
  ...
  render 'new', :layout => db.load_template
end

Is there a cleaner way to do this? Perhaps it's possible to register new layouts at runtime and use the normal syntax?

Upvotes: 2

Views: 1219

Answers (2)

Dutow
Dutow

Reputation: 5668

You can extend ActionController::Base (or ApplicationController) with a module and alias_method_chain to make this work.

module Foo

  alias_method_chain :render, :dblayout

  def render_with_dblayout options = nil, extra_options = {}, &block
   if options.include? :dblayout
     ...
   else
     render_without_dblayout options, extra_options { yield }
   end
  end
end

ActionController::Base.send(:include, Foo)

Upvotes: 1

nathanvda
nathanvda

Reputation: 50057

Ha! I encountered a project that will solve just that. Check out panoramic. It stores rails views in the database instead of the filesystem.

Upvotes: 3

Related Questions