Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • FileTemplate
  • Helpers
  • Template

Interfaces

  • IFileTemplate
  • ITemplate

Exceptions

  • FilterException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Templating;
 13: 
 14: use Nette,
 15:     Nette\Caching;
 16: 
 17: 
 18: 
 19: /**
 20:  * Template stored in file.
 21:  *
 22:  * @author     David Grudl
 23:  */
 24: class FileTemplate extends Template implements IFileTemplate
 25: {
 26:     /** @var string */
 27:     private $file;
 28: 
 29: 
 30: 
 31:     /**
 32:      * Constructor.
 33:      * @param  string  template file path
 34:      */
 35:     public function __construct($file = NULL)
 36:     {
 37:         if ($file !== NULL) {
 38:             $this->setFile($file);
 39:         }
 40:     }
 41: 
 42: 
 43: 
 44:     /**
 45:      * Sets the path to the template file.
 46:      * @param  string  template file path
 47:      * @return FileTemplate  provides a fluent interface
 48:      */
 49:     public function setFile($file)
 50:     {
 51:         $this->file = realpath($file);
 52:         if (!$this->file) {
 53:             throw new Nette\FileNotFoundException("Missing template file '$file'.");
 54:         }
 55:         return $this;
 56:     }
 57: 
 58: 
 59: 
 60:     /**
 61:      * Returns the path to the template file.
 62:      * @return string  template file path
 63:      */
 64:     public function getFile()
 65:     {
 66:         return $this->file;
 67:     }
 68: 
 69: 
 70: 
 71:     /**
 72:      * Returns template source code.
 73:      * @return source
 74:      */
 75:     public function getSource()
 76:     {
 77:         return file_get_contents($this->file);
 78:     }
 79: 
 80: 
 81: 
 82:     /********************* rendering ****************d*g**/
 83: 
 84: 
 85: 
 86:     /**
 87:      * Renders template to output.
 88:      * @return void
 89:      */
 90:     public function render()
 91:     {
 92:         if ($this->file == NULL) { // intentionally ==
 93:             throw new Nette\InvalidStateException("Template file name was not specified.");
 94:         }
 95: 
 96:         $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
 97:         if ($storage instanceof Caching\Storages\PhpFileStorage) {
 98:             $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
 99:         }
100:         $cached = $compiled = $cache->load($this->file);
101: 
102:         if ($compiled === NULL) {
103:             try {
104:                 $compiled = "<?php\n\n// source file: $this->file\n\n?>" . $this->compile();
105: 
106:             } catch (FilterException $e) {
107:                 $e->setSourceFile($this->file);
108:                 throw $e;
109:             }
110: 
111:             $cache->save($this->file, $compiled, array(
112:                 Caching\Cache::FILES => $this->file,
113:                 Caching\Cache::CONSTS => 'Nette\Framework::REVISION',
114:             ));
115:             $cached = $cache->load($this->file);
116:         }
117: 
118:         if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) {
119:             Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters());
120:         } else {
121:             Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters());
122:         }
123:     }
124: 
125: }
126: 
Nette Framework 2.0.0 API API documentation generated by ApiGen 2.7.0