Created
February 18, 2011 15:51
-
-
Save janmarek/833851 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of the Nette Framework (http://nette.org) | |
* | |
* Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com) | |
* | |
* For the full copyright and license information, please view | |
* the file license.txt that was distributed with this source code. | |
*/ | |
namespace Nette\Templates; | |
use Nette, | |
Nette\Application\Control; | |
/** | |
* Responsible for creating a new instance of template. | |
* | |
* @author Patrik Votoček | |
*/ | |
class TemplateFactory extends Nette\Object implements ITemplateFactory | |
{ | |
/** | |
* @param Nette\Application\Control application control or presenter | |
* @return ITemplate | |
*/ | |
public function createTemplate(Control $control) | |
{ | |
$template = new FileTemplate; | |
$this->configureTemplate($template, $control); | |
return $template; | |
} | |
/** | |
* @param Nette\Templates\ITemplate template to configure | |
*/ | |
public function configureTemplate(ITemplate $template, $control) | |
{ | |
$presenter = $control->getPresenter(FALSE); | |
$httpRequest = $presenter->getContext()->getService('Nette\\Web\\IHttpRequest'); | |
$template->onPrepareFilters[] = callback($this, 'templatePrepareFilters'); | |
// default parameters | |
$template->control = $control; | |
$template->presenter = $presenter; | |
$template->user = $presenter->getContext()->getService('Nette\\Application\\Application'); | |
$template->baseUri = rtrim($httpRequest->getUri()->getBaseUri(), '/'); | |
$template->basePath = preg_replace('#https?://[^/]+#A', '', $template->baseUri); | |
// flash message | |
if ($presenter !== NULL && $presenter->hasFlashSession()) { | |
$id = $control->getParamId('flash'); | |
$template->flashes = $presenter->getFlashSession()->$id; | |
} | |
if (!isset($template->flashes) || !is_array($template->flashes)) { | |
$template->flashes = array(); | |
} | |
// default helpers | |
$template->registerHelper('escape', 'Nette\Templates\TemplateHelpers::escapeHtml'); | |
$template->registerHelper('escapeUrl', 'rawurlencode'); | |
$template->registerHelper('stripTags', 'strip_tags'); | |
$template->registerHelper('nl2br', 'nl2br'); | |
$template->registerHelper('substr', 'iconv_substr'); | |
$template->registerHelper('repeat', 'str_repeat'); | |
$template->registerHelper('replaceRE', 'Nette\String::replace'); | |
$template->registerHelper('implode', 'implode'); | |
$template->registerHelper('number', 'number_format'); | |
$template->registerHelperLoader('Nette\Templates\TemplateHelpers::loader'); | |
return $template; | |
} | |
/** | |
* Descendant can override this method to customize template compile-time filters. | |
* @param Template | |
* @return void | |
*/ | |
public function templatePrepareFilters(Template $template) | |
{ | |
// default filters | |
$template->registerFilter(new LatteFilter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment