tags:

views:

58

answers:

2

I have a Template Parser function which loads the raw html code of the template with the given name and replaces wildcards in it (%DATE%,etc). I will be using the same template more than once for some pages, e.g. a menu item, and so the html file would be loaded into memory more than once.

So one file would be read more than once because the ParseTemplate(name) function is called more than once with the same template.

For clarity: The same template is going to be loaded more than once in one page.

Is it worth storing all loaded template files in an array so they don't have to be read with file_get_contents(); more than once?

A: 

PHP variables only last for the time of the request unless you use shared memory. You'd need to base your method on how much memory you want to chew up with that instead of httpd processes or database cache.

Ignacio Vazquez-Abrams
Thanks but I know about variables.The same template will be required several times on some pages, I want to know if it is worth putting its contents into an array instead of loading it over and over again.I'll try and edit my answer so it is clearer what I mean
lamas
+1  A: 

Well if you use the same template (snippet I guess) more than once in a single page, then yes save it in a variable and print it every time it's needed after the substitution of wildcards.

kemp
What I was thinking about is if the "work" of a) Checking if the file exists in the cache array and b) Putting the file into the cache and the memory usage are actually worth it. And no, I wrote the code myself.
lamas
Well the point is that you will "put the file into the cache" anyway even if you load it just once: file_get_contents() returns a string, so you'll save it in some variable. So reusing that var will surely be faster than reading the same file over and over again.
kemp