0

I had some working code within a template making use of the php variable $_SERVER['HTTP_ACCEPT'], to check for webp image format support from the user's browser. This was working fine, but after turning on template caching, the output for

<?php echo $_SERVER['HTTP_ACCEPT']; ?>

is blank. Whether the php is processed via output or input doesn't appear to impact its blank value.

I'm using 5.2.4

Has anyone else experienced this? If so, any ideas on a reason or workaround?

  • If you need a dynamic response (your template wants to check something via PHP) how would you expect that to happen in a cached template? You would need to not cache that template. If you can be clearer on how you're going about caching (show us your template) and if it's static caching or what, we can help you better. – jrothafer Mar 11 at 15:43
  • I don't understand how template caching occurs at a deeper level. I expected the template itself, and maybe the underlying query results to be cached (since the docs state that template caching supersedes tag caching), but my expectation was that the php would still be executed and work as expected per browser session. Not a huge deal, I was wrong. Even if I don't cache that particular template, and still cache a shared header template, I'm still unable to access the session variables. Could you point me to some documentation that would help to spell out these limitations? – Poultry in Motion Mar 11 at 21:53
  • Are you using EE native caching? Or CE Cache? The docs for EE caching is here docs.expressionengine.com/latest/optimization/caching.html – jrothafer Mar 12 at 8:20
0

I'm not sure native EE caching even works with PHP enabled templates. I don't think it does (but don't have time to test right now). But if it does:

So this is a general overview, but when you cache an entire template, what gets cached is the output of the template. No dynamic pieces will really function, because what is cached is the output. So for example:

{!-- this is our template with PHP turned that is a cached template --}
{exp:channel:entries entry_id="1"}
    <h1>{title}</h1>
    <h2><?php echo "PHP"; ?></h2>
{/exp:channel:entries}

This will result in a cached output that will render like this:

<h1>My Entry Title</h1>
<h2>PHP</h2>

Now, let's look at the template with a dynamic input, where a POST variable has a value of "PHP Post TEST" (so test_post_string POST value is "PHP Post TEST"):

{!-- this is our template with PHP turned that is a cached template --}
{exp:channel:entries entry_id="1"}
    <h1>{title}</h1>
    <h2><?php echo $_POST["test_post_string"]; ?></h2>
{/exp:channel:entries}

The output of this template when it get's cached will be:

<h1>My Entry Title</h1>
<h2>PHP Post TEST</h2>

So that is how that template will be cached. Until that cache is cleared, no matter what you POST to that template and route (say $_POST["test_post_string"] = "hello"), it will still render the same cache output, where that POST variable had that original string.

So that dynamic portion of the template is just cached. So you need a more complex solution if you want some dynamic aspect of a template that gets cached to work.

| improve this answer | |
  • This is the answer, thanks for this! It's a shame that this isn't more apparent either in the docs or within the UI, when caching is enabled (perhaps as a warning?). While I could understand this effect with a static cache, I erroneously anticipated that a template cache was perhaps less "static", for lack of a better way to put it, and only cached all tag data or somesuch, while still retaining any scripted functionality. Thanks again for the answer. – Poultry in Motion Mar 13 at 6:52
  • Solving complex cache issues is tough; but what you might be looking to do isn't impossible. Ask another question with clearer definitions (and like I said, include template code and more indepth explanation of what you want to accomplish) and I bet I can provide practical solution ideas. Thanks! – jrothafer Mar 14 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.