TinyButStrong - the PHP Template Engine
Categories > TinyButStrong general >

LoadTemplate from MySQL instead of a File??

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: JimT
Date: 2004-01-27
Time: 19:50

LoadTemplate from MySQL instead of a File??

Is it possible to do a "LoadTemplate" from the results of a MySQL query instead of from a file?

Would this even be desirable?  My original plan is to store templates in a MySQL DB (separate table) along with all of my other content data.... but are there issues of storing templates in Files vs. DB that I should rethink my plan??

Thanks in advance for your thoughts on this issue.....
By: SarCaSM
Date: 2004-01-27
Time: 22:11

Re: LoadTemplate from MySQL instead of a File??

My opinion would be to not do that.  Databases should not be holding that type of data. It would probably be easier for you to edit the templates anyway if they were in the form of a file.
By: Pirjo Posio
Date: 2004-01-28
Time: 10:53

Re: LoadTemplate from MySQL instead of a File??

I agree to SarCaSM's opinion about storing the whole template in a database. But storing some variables connected to templates is OK in my opinion, e.g. the name of a css-file, if you have css-files per user.
Or is there a problem? I haven't tested this.
- Pirjo
By: JimT
Date: 2004-01-28
Time: 16:07

Re: LoadTemplate from MySQL instead of a File??

Thank you for your replies so far....  I am curious as to WHY you feel that "databases should not be holding that type of data"??

I am creating a small PHP/MySQL CMS as a project to learn more about PHP.  Content is updated using a WYSIWYG editor, so HTML will be embedded in text stored in the DB (images will NOT be stored in the DB!!....just the IMG tags).

It SEEMS to be a logical step to use the same concept to maintain and store templates in the DB.  The ideal situation is to update template code using the WYSIWYG editor, worst case would be to cut and paste code created in an external editor into a form to save in the DB.  Either way, updating a template this way seems comparable in effort (after the backend is created) to FTP'ing a flat file.

As a self-admitted PHP rookie, I'm just looking for facts to support using DB-based or file-based templates (and if TBS would support the "load template from a DB" concept)...... Performance??  Preference??  Etc......

I do appreciate you sharing your thoughts......
By: SarCaSM
Date: 2004-01-28
Time: 21:43

Re: LoadTemplate from MySQL instead of a File??

I don't know how to explain it really.  I can explain it like my database professor explained something along the lines of this?  From what I think, with a lot of data in a database like a template, there can come database corruption through the possible many many times that it gets editted.  A field in a database is only meant to really (again from what I understand) hold data, andf possibly on a rare occasion gets editted.  I hope thats makes sense.  I can elaborate more if necessary. 
By: SarCaSM
Date: 2004-01-28
Time: 21:44

Re: LoadTemplate from MySQL instead of a File??

Also, I would tend to think that the constant pulling of a large amount of data such as a template from the database would be a horrible strain eventually on the DBMS itself.
By: JimT
Date: 2004-01-29
Time: 23:08

Re: LoadTemplate from MySQL instead of a File??

Sounds like database theory from the 1970's!!  :-)  I would certainly hope that advances in technology would keep me from having to worry about how many times I can update a table row or column before the DB corrupts itself.

To me, "data" means a wider range of options (i.e. TEXT and BLOB datatypes) than it once did.  And, I should be able to update data as needed, not just "rarely"  (that IS one of the functions of a data base).  If you can't process data as needed (be it a small or large amount, with consideration given to response time issues), then you have to rethink whether to use a database at all.

Your professor makes a database sound like a delicate object - to be handled lightly.  I strongly disagree.  Properly designed, indexed, etc, modern databases should be able to handle what you give it, even on a small server platform.
By: JimT
Date: 2004-01-29
Time: 23:19

Re: LoadTemplate from MySQL instead of a File??

Even after all of this database theory discussion, I still ask the question to the author of the fine TinyButStrong class:

Feature request:  Execute a "LoadTemplate" reading a template stored in a MySQL database. 

It seems to be a logical step given TBS's already existing database support.  Thank you for your consideration.......
By: SarCaSM
Date: 2004-01-30
Time: 01:45

Re: LoadTemplate from MySQL instead of a File??

technically, if I understand this right, you should be able to do that now (though I still don't recommend it).  You should be able to read it in from the database using a mergeblock, and then just through concantination add it to the source of your instance of TBS ($TBS->Source).  Then your template is in there, and your just need to execute a show and it should be  fine. 
By: JimT
Date: 2004-01-30
Time: 17:47

Re: LoadTemplate from MySQL instead of a File??

I'll try that out......  Thanks for the tip........
By: Skrol29
Date: 2004-01-30
Time: 18:19

Re: LoadTemplate from MySQL instead of a File??

That's a bad idea, but first I'm telling you how to do it.

First way: set the $TBS->Source property to the string value stored in your database.

Second way, code:
$TBS->Source = "[blk.template;htmlconv=no,protect=no]" ;
$TBS->MergeBlock('blk',$cnx,'SELECT template FROM t_table WHERE id='.$template_id) ;

Well, now here is my explaination why it is a bad idea to store templates in database. That's the same reason why NO sites stores images in database. Retrieving a content from a database is always slower than retriving content from a file. This is just because database data are themselves stored in files...

What is usually done, is to save the name of the file in the database. I enables the application to retrieve the file.
By: JimT
Date: 2004-01-30
Time: 19:28

Re: LoadTemplate from MySQL instead of a File??

I figured that performance (slow response) was the main reason for NOT storing a template in a DB, but I wanted the opinions of those who have actually had that experience.  Thanks to all for your responses!!

If I have some extra time (Ha!), I may benchmark the two ways (file vs. database) to see how big the performance penalty is.  If I do this, I'll post my results.......