Browser-side caching of Yeast templates

The body of a Yeast template is cacheble in the client's browser. To that aim, the body of the template (HTML with Yeast code) is stored in a separate file, which can be cached in the browser in the first template load. The original template can be replaced by a much more compact document containing the engine, the view’s model data and, as body, a single script tag that loads the file with the original template body and triggers its processing. By caching Yeast templates in the browser, the server throughput is greatily improved (read more).

E.g. the following template

<html>
  <head>
    <script src="yst.js"></script>
    <script yst="model">
      people = [{name:'Fred Flintstone'}, {name:'Barney Rubble'}];
      userName = "John";        
      temperature = -2;
    </script>
  </head>
  <body>
    <p yst="value">Hello, $userName$</p>
    <p yst="if" ystTest="temperature < 0">$userName$, you should wear your coat.</p> 
    <ul><li yst="apply" ystSet="people" class="$i%2!=0?'grey':''$">$e.name$</li></ul>
  </body>
</html>  

could be transformed into the following cacheable version of the template:

<html>
  <head>
    <script src="yst.js"></script>
    <script yst="model">
      people = [{name:'Fred Flintstone'}, {name:'Barney Rubble'}];
      userName = "John";        
      temperature = -2;
    </script>
  </head>
  <body>
    <script src="templ_body.js>"></script>
  </body>
</html>  

where templ_body.js is:

function __TemplateBody(cv, i, params) {
  var rr = YST.Txt.value(cv,i,params,null,['<p>Hello, $userName$</p>']);  
  r += YST.Txt.iff(cv,i,params,null,'temperature < 0',['<p>$userName$, you should wear your coat.</p>']);  
  r += '<ul>';  
  r += YST.Txt.apply(cv,i,params,null,'people',['<li class=\"$i%2!=0?\'grey\':\'\'$\">$e.name$</li>']);  
  r += '</ul>';  
  return r;  
}  

document.write(__TemplateBody([], 0, {}));

The internal code of the function __TemplateBody corresponds to the translated version of the template (see templates translation).

How to activate the browser-side caching facility

You have two choices:

  1. Standalone: you can prepare your templates, processing then as in the above example, to be cacheable. To do it, translate them with the -c option. See more details.
  2. Using the Yeast-Server API: you can configure Yeast-Server for Java to automatically process templates so that they can be browser-side cacheable. To do it read the Yeast-Server manual.