Packages

  • 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

  • NFileTemplate
  • NTemplate
  • NTemplateHelpers

Interfaces

  • IFileTemplate
  • ITemplate

Exceptions

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