/** * Try to retrieve the compiled template from the cache. * * @param string $templateName Template that should be retrieved. * * @return bool|Layout Compiled template in form of a string, or bool false if the template is not in cache. */ private function getFromCache($templateName) { // try to get it from cache $cachedTemplate = $this->htpl->getCache()->read($templateName); if (!$cachedTemplate) { return false; } /** * @var Layout $layout */ $layout = unserialize($cachedTemplate); // check when cache was last touched, so we don't need to revalidate all templates $lastTouched = $layout->getLastTouched(); if ($lastTouched > time() - $this->htpl->getOptions()['cacheValidationTTL']) { return $layout; } foreach ($layout->getIncludedFiles() as $tplFile => $tplFileCreatedOn) { // verify if cache is still fresh, for all included templates in this layout if ($lastTouched < $this->htpl->getTemplateProvider()->createdOn($tplFile)) { $this->htpl->getCache()->delete($templateName); return false; } } // update last touched $layout->setLastTouched(time() + $this->htpl->getOptions()['cacheValidationTTL']); // cache it again $this->htpl->getCache()->write($templateName, serialize($layout)); return $layout; }
public function testSetGetForceCompile() { $provider = new ArrayProvider(['test.htpl' => '{var}']); $htpl = new Htpl($provider); $this->assertSame(false, $htpl->getOptions()['forceCompile']); $this->assertSame(false, $htpl->getForceCompile()); $htpl->setForceCompile(true); $this->assertSame(true, $htpl->getOptions()['forceCompile']); $this->assertSame(true, $htpl->getForceCompile()); }
/** * Static callback that does the minification. * The method is called from within the compiled template. * * @param array $files List of files that need to minified. * @param string $type Is it a js or a css minification in question. * @param Htpl $htpl Current htpl instance. * * @throws HtplException */ public static function minifyCallback($files, $type, Htpl $htpl) { // get minify driver instance $options = $htpl->getOptions()['minify']; if (empty($options)) { throw new HtplException('Missing options for w-minify function.'); } // get driver $driver = isset($options['driver']) ? $options['driver'] : '\\Webiny\\Htpl\\Functions\\WMinify\\WMinify'; if (!is_object($driver)) { $driver = new $driver($htpl); } if (!$driver instanceof \Webiny\Htpl\Functions\WMinify\WMinifyAbstract) { throw new HtplException('Minify driver must implement \\Webiny\\Htpl\\Functions\\WMinify\\WMinifyAbstract.'); } if ($type == 'js') { $minifiedFile = $driver->minifyJavaScript($files); echo '<script type="text/javascript" src="' . $minifiedFile . '"/>'; } else { if ($type == 'css') { $minifiedFile = $driver->minifyCss($files); echo '<link rel="stylesheet" href="' . $minifiedFile . '"/>'; } else { throw new HtplException(sprintf('Unknown $type value for minify callback: %s', $type)); } } }