What about Template Caching?

Started by chinmay.sahoo, 07-21-2016, 05:56:29

Previous topic - Next topic

chinmay.sahooTopic starter

Template engines such as Smarty[4] often talk about template caching. Usually, these engines offer an in-built mechanism for storing a compiled version of a template (i.e. the native PHP generated from the template), which prevents us having to recompile the template every time a page is requested. This should not be confused with output caching, which refers to the caching of the rendered HTML (or other output) that PHP sends to the browser. You can successfully use both types of caching together on the same site.

Here, we'll look at PHP's in-built caching mechanism, the output buffer[5], which can be used with whatever page rendering system you prefer (templates or no templates). Consider a situation in which your script displays results using, for example, echo or print, rather than sending the data directly to the browser. In these cases, you can use PHP's output control functions to store the data in an in-memory buffer, which your PHP script has both access to and control over.


Quote<?php
// Start buffering the output
ob_start();
// Echo some text (which is stored in the buffer);
echo '1. Place this in the buffer<br />';
// Get the contents of
$buffer = ob_get_contents();
// Stop buffering and clean out the buffer
ob_end_clean();
// Echo some text normally
echo '2. A normal echo<br />';
// Echo the contents from the buffer
echo $buffer;
?>


The buffer itself stores the output as a string. So, in the above script, we commence buffering with ob_start and use echo to display something. We then use ob_get_contents to fetch the data the echo statement placed in the buffer, and store it in a string. The ob_end_clean function stops the output buffer and trashesm the contents; the alternative is ob_end_flush, which displays the contents of the
buffer.


saravjeet

The first time a template is used, it is loaded in the template cache for quick retrieval. You can load templates directly into the cache in a script tag, or by consuming the $templateCache service directly.


Lishmalinyjames

$templateCache is a Cache object created by the $cacheFactory. The first time a template is used, it is loaded in the template cache for quick retrieval. You can load templates directly into the cache in a script tag, by using $templateRequest , or by consuming the $templateCache service directly.

saravanan28

It builds a cache key (either automatically, or uses the one defined), and then looks for an existing cached template. If a cache is found for that key, it is outputted and the template process moves on. If no cache is found for that key, the caching process begins.
  •  

Lishmalinyjames

Template caching options are not available for Portal Templates for pages.