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
  • Deprecated
  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:  * Template stored in file.
 20:  *
 21:  * @author     David Grudl
 22:  */
 23: class FileTemplate extends Template implements IFileTemplate
 24: {
 25:     /** @var string */
 26:     private $file;
 27: 
 28: 
 29:     /**
 30:      * Constructor.
 31:      * @param  string  template file path
 32:      */
 33:     public function __construct($file = NULL)
 34:     {
 35:         if ($file !== NULL) {
 36:             $this->setFile($file);
 37:         }
 38:     }
 39: 
 40: 
 41:     /**
 42:      * Sets the path to the template file.
 43:      * @param  string  template file path
 44:      * @return self
 45:      */
 46:     public function setFile($file)
 47:     {
 48:         $this->file = realpath($file);
 49:         if (!$this->file) {
 50:             throw new Nette\FileNotFoundException("Missing template file '$file'.");
 51:         }
 52:         return $this;
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Returns the path to the template file.
 58:      * @return string  template file path
 59:      */
 60:     public function getFile()
 61:     {
 62:         return $this->file;
 63:     }
 64: 
 65: 
 66:     /**
 67:      * Returns template source code.
 68:      * @return string
 69:      */
 70:     public function getSource()
 71:     {
 72:         return file_get_contents($this->file);
 73:     }
 74: 
 75: 
 76:     /********************* rendering ****************d*g**/
 77: 
 78: 
 79:     /**
 80:      * Renders template to output.
 81:      * @return void
 82:      */
 83:     public function render()
 84:     {
 85:         if ($this->file == NULL) { // intentionally ==
 86:             throw new Nette\InvalidStateException("Template file name was not specified.");
 87:         }
 88: 
 89:         $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
 90:         if ($storage instanceof Caching\Storages\PhpFileStorage) {
 91:             $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
 92:         }
 93:         $cached = $compiled = $cache->load($this->file);
 94: 
 95:         if ($compiled === NULL) {
 96:             try {
 97:                 $compiled = "<?php\n\n// source file: $this->file\n\n?>" . $this->compile();
 98: 
 99:             } catch (FilterException $e) {
100:                 $e->setSourceFile($this->file);
101:                 throw $e;
102:             }
103: 
104:             $cache->save($this->file, $compiled, array(
105:                 Caching\Cache::FILES => $this->file,
106:                 Caching\Cache::CONSTS => 'Nette\Framework::REVISION',
107:             ));
108:             $cached = $cache->load($this->file);
109:         }
110: 
111:         if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) {
112:             Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters());
113:         } else {
114:             Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters());
115:         }
116:     }
117: 
118: }
119: 
Nette Framework 2.0.12 API API documentation generated by ApiGen 2.8.0