views:

266

answers:

2

Is it possible to store erb templates in database? How?

+1  A: 

Sure it is.

But I think, this is not the best solution if you only need some pages with same layout and different content.

If you really want ERBs saved in database, create some model with your ERBs saved in. Then you can call in controller code like this, to render that ERB.

erb = ERB_model.find(...)
@some_model_data = SomeModel.find(...)
erb = ERB.new(erb.source)
render :text => erb.result, :layout => true (or another)

Watch ERB.new parameters.

retro
Thanks!I want to store erb's in database because I have many sub-sites in my rails app(like blogger has many blogs) with customizable templates and I don't want to store erb's on disk.Yes, I know about lliquid templates but i need mre functional engine and I don't care about security because all sub-sites are controlled by one user(my customer).But I am still thinking about storing templates in directories like "app/views/#{site.id}/". Simply I don't want to do file operations... I think it is wrong... not rails-way.
SMiX
I agree with you. DB way is more rails-way, because it will need less (ugly) code.I just added some fixes to my answer (you probably will use layouts and some variables inside ERB).
retro
+1  A: 

Hi SMiX,

I'm also working on an almost same concept as you are working on. I also need my clients to create their own web sites

Initially i also had the idea of saving all the erbs in the database, but later i have given up that idea as even though it works for plain text pages, when you want to render components and partials it will not be easy.

and mainly i had problems on saving layout.erb files in the database

so i decided to go in a way that page will construct upon the subdomain

cheers, sameera

sameera207