Skip to content

Instantly share code, notes, and snippets.

@juzna
Created February 18, 2011 15:11
Show Gist options
  • Save juzna/833790 to your computer and use it in GitHub Desktop.
Save juzna/833790 to your computer and use it in GitHub Desktop.
<?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
{
protected function createInstance() {
// option 1:
// allows creating StringTemplateFactory or MySuperFileTemplateFactory by overriding this method
return new FileTemplate;
// option 2:
// allows creating StringTemplateFactory by overriding this method
// and default use of MySuperFileTemplateFactory in rest of application
// (but needs to handle environment better)
return Nette\Environment::getContext()->getService('Nette\Templates\ITemplate');
}
/**
* @param Nette\Application\Control application control or presenter
* @return ITemplate
*/
public function createTemplate(Control $control)
{
$template = $this->createInstance();
$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