# RHTML in database?

**URL:** <https://www.ruby-forum.com/t/rhtml-in-database/99930>\
**Category:** Rails\
**Created:** [July 26, 2006, 8:55am UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930 "2006-07-26T08:55:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [July 26, 2006, 8:55am UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/1 "2006-07-26T08:55:45Z")

</div>

Hi,  
is it possible to store RHTML templates or partials in database?

## thanks, Bojan M.

Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [July 27, 2006, 10:40am UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/2 "2006-07-27T10:40:04Z")

</div>

It is possible to serve all your rhtml from a non-file based location  
but  
not without a few changes to the Rails core classes. For the MasterView  
project we wanted the ability to retrieve rhtml through our own  
consolidated  
IO layer so that it would allow retrieval from database, alternate  
paths, as  
well as allowing the content to run through filters. With a little  
digging  
in the Rails code I was able to hook the right classes to get this to  
work  
(retrieval from non-file based locations), however I am wanting to  
submit a  
patch to the Rails core team so that people could hook in easier, right  
now  
there are File.method calls inside some of the key Rails core methods  
which  
means there is not a clean way to hook rails. I stubbed out the code  
for  
doing database based retrieval but didn’t get to finish that yet.

I believe that with regards to rhtml that it was the most difficult to  
get  
layout rhtml from a non-file location, the other view rhtml and partials  
were more easily hooked as I recall. So if you are only doing view or  
partials your work should be a little easier.

When I can find some time to put together a more generic patch, then I  
will  
submit that to the Rails core team so that all rhtml access can be  
easily  
hooked into other mechanisms.

Also I haven’t checked if edge rails makes this any easier yet,  
obviously  
one should do that first to see if a change has already been made.

If you would like some pointers on how to go about doing this, I’d be  
glad  
to point you to the code in my project that accomplished it.

Jeff B.  
MasterView Project founder - rails-optimized (x)html-friendly template  
engine  
[http://masterview.org/](http://masterview.org/)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [July 28, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/3 "2006-07-28T16:22:32Z")

</div>

Jeff B. wrote:

> there is not a clean way to hook rails. I stubbed out the code for
> 
> [http://masterview.org/](http://masterview.org/)  
> Bojan M.
> 
> * * *
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Thanks for your answer Jeff. Do you think it would be possible to use  
different input resources (db, filesystem). Something like Smarty PHP  
has.

best regards,  
Bojan

–  
Bojan M.  
Informatika Mihelac, Bojan M. s.p. | [www.informatikamihelac.com](http://www.informatikamihelac.com)  
 → tools, scripts, tricks from our code lab: [http://source.mihelac.org](http://source.mihelac.org)

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [July 28, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/4 "2006-07-28T20:13:52Z")

</div>

Bojan,

Once you make the changes like we did for MasterView then theoretically  
you  
could serve from anywhere. Like I mentioned in MasterView we are able to  
easily change to serve from filesystem or a memory hash, and we plan to  
add  
serving from a db as well. So the short answer is yes, I don’t see any  
limitations once you make the code changes.

Jeff

Thanks for your answer Jeff. Do you think it would be possible to use

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [August 4, 2006, 3:38am UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/5 "2006-08-04T03:38:35Z")

</div>

Seems like storing rhtml in the model is a violation of MVC…

?

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [August 4, 2006, 4:13am UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/6 "2006-08-04T04:13:05Z")

</div>

On 8/3/06, Brez! !! [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Seems like storing rhtml in the model is a violation of MVC…

Doesn’t it depend on whether your RHTML represents an instance of a  
model?

If you’re doing a CMS, then wouldn’t it make sense to keep templates  
in a database?

–  
Austin G.  
Thinking & Making: IA, UX, and IxD  
[http://thinkingandmaking.com](http://thinkingandmaking.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [August 4, 2006, 12:41pm UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/7 "2006-08-04T12:41:49Z")

</div>

Well don’t call the MVC cops 🙂

Actually I don’t think the physical location of where you store your  
templates would violate anything, in the end you are still applying  
the same MVC pattern regardless.

And I would agree if you are creating a CMS then it is a great place  
to store them, you have one place to backup for all user data  
(templates and content), you can have many versions, you don’t have to  
provide users with access to be able to write to your file system,  
etc.

---

<div class="post-metadata">

**Author:** ![bmihelac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/5fc32e/32.png) [@bmihelac](https://www.ruby-forum.com/u/bmihelac)\
**Post date:** [August 3, 2006, 12:10pm UTC](https://www.ruby-forum.com/t/rhtml-in-database/99930/8 "2006-08-03T12:10:31Z")

</div>

Bojan M. \<lists@…\> writes:

> Hi,  
> is it possible to store RHTML templates or partials in database?
> 
> thanks,  
> Bojan M.

Hi Bojan,

DB2 Express (the free version) and DB2 9.0 allows storing XML documents  
(column type XML). So you may encapsulate your partials/RHTML templates  
in XML tags and store the document as it is in database.  
For details, please refer to  
[http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0311wong/](http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0311wong/)

DB2 stores the documents as XML documents (does not do thrashing)and  
allows Xqueries on the column so that you can retrieve only specific  
part of the  
document.

There is a starter toolkit for Rails on DB2 available  
([http://www.alphaworks.ibm.com/tech/db2onrails](http://www.alphaworks.ibm.com/tech/db2onrails)) that contains complete  
installation of DB2 Express, DB2 adapter for Rails and RoR.

HTH,  
Ashutosh
