Compiles a template and writes it to a cache file, which is used for inclusion.
public static template ( string $file, array $options = [] ) : string | ||
$file | string | The full path to the template that will be compiled. |
$options | array | Options for compilation include: - `path`: Path where the compiled template should be written. - `fallback`: Boolean indicating that if the compilation failed for some reason (e.g. `path` is not writable), that the compiled template should still be returned and no exception be thrown. |
return | string | The compiled template. |
public function testFallbackWithNonWritableDirectory() { $this->expectException('/failed to open stream/'); $result = Compiler::template("{$this->_path}/{$this->_file}", array('path' => LITHIUM_APP_PATH . '/foo', 'fallback' => true)); $this->assertEqual("{$this->_path}/{$this->_file}", $result); $this->expectException('/Could not write compiled template to cache/'); $this->expectException('/failed to open stream/'); $result = Compiler::template("{$this->_path}/{$this->_file}", array('path' => LITHIUM_APP_PATH . '/foo', 'fallback' => false)); }
public function testTemplateCacheHit() { $path = Libraries::get(true, 'resources') . '/tmp/cache/templates'; $original = Compiler::template("{$this->_path}/{$this->_file}", compact('path')); $cache = glob("{$path}/*"); clearstatcache(); $cached = Compiler::template("{$this->_path}/{$this->_file}", compact('path')); $this->assertEqual($original, $cached); $this->assertEqual($cache, glob("{$path}/*")); file_put_contents("{$this->_path}/{$this->_file}", "Updated"); clearstatcache(); $updated = Compiler::template("{$this->_path}/{$this->_file}", compact('path')); $newCache = glob("{$path}/*"); $this->assertNotEqual($cache, $updated); $this->assertEqual(count($cache), count($newCache)); $this->assertNotEqual($cache, $newCache); }
/** * Override compile to have a different cache path by default. * * @see lithium\template\view\Compiler::template() * @param string $file The full path to the template that will be compiled. * @param array $options Options, see `Compiler::template()`. * @return string The compiled template. */ public static function template($file, array $options = array()) { $path = Libraries::get(true, 'resources') . '/tmp/cache/mails'; $options += compact('path'); return parent::template($file, $options); }