This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit

eZ Community » Forums » Setup & design » PHP works in templates! (with...

PHP works in templates! (with viewcaching enabled)

PHP works in templates! (with viewcaching enabled)

Tuesday 30 December 2003 12:11:52 pm - 1 reply

Woohoo, I just found out how to use php with viewcaching enabled!

This is 'the story':

A time ago, I created a function to include parsed php files in templates (more info:

But, last week I found out it's hardly of no use when you DISable viewcaching (I was editing, so viewcaching was disabled).
So, when you enable caching, you'll always view a cached page (which isn't dynamic).

Today, I found a work-around, to keep your pages cached, but also dynamic!
This is how to do it:

- 'Install' the function in the kernel as I described on the page (link on top of this page)
- Put the template codes in your templates: {""|marksPHPinsert}
- In 'file.php', you should do something like this:

echo " echo <?php ";
echo " echo rand(0, 100); ";
echo " echo ?> ";

All right. This is it actually. The template-code parser parses the code to php. The output of file.php is 'echo <?php etc...'. The template-code parser puts these lines in a .php file (the cache file), but will put the compiled php code on the screen (only the first time), NOT the cached file. The second time you load your page, ezPublish will load the cached file (which contains the php code) and parse every php code in it.

So, the first time you load your page, a cach file is created, but what you see on the screen, does come out of the cache file, but from the server's memory or something.
The second time (and every next time) you load the page, you will view cached files which contain php code, which of course will be parsed by the php parser.

Example what the server does the first time you load the page:
1. Read template file (the included php file returns php text (not code))
2. Save the output of the template-code parser in a cache file (.php file)
put the same output on the screen.

Example what the server does every next time you load the page:
1. Load the cached file
2. Compile it with the php parser

Now, the first time I load the page, my code does not come out of a cache file (which I would like very much) and my 'sneaky-included' php code is not parsed.
The second time I load the page, everything will come out of a cache file, so my php code will be parsed.

It's hard to explain what's going on, so I hope this whole story is clear enough...

Now, my question: Is it possible to ALWAYS view a cached file?

-- Mark

Modified on Tuesday 30 December 2003 2:39:20 pm by Mark Overduin

Friday 02 January 2004 4:15:59 pm

This is not working here sad.gif Emoticon

With what version of ez have you tried this?

Modified on Friday 02 January 2004 4:16:28 pm by Shurbann Martes


You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from