Skip to content

Commit

Permalink
template loading ... unlimited inheritence level supported
Browse files Browse the repository at this point in the history
you can define grid as grid(data, _self) it will use blocks from current template and automatically extend it from base template
  • Loading branch information
Sorien committed Oct 8, 2011
1 parent 3980376 commit 4c9b1ef
Showing 1 changed file with 47 additions and 29 deletions.
76 changes: 47 additions & 29 deletions Twig/DataGridExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
*/
class DataGridExtension extends \Twig_Extension
{
const DEFAULT_TEMPLATE = 'SorienDataGridBundle::blocks.html.twig';

/**
* @var \Twig_Environment
*/
protected $environment;
/**
* @var \Twig_Template
*/
protected $template;
protected $templates;
protected $theme;
/**
* @var \Symfony\Component\Routing\Router
Expand Down Expand Up @@ -188,60 +190,76 @@ public function nextOrder($value)
*/
private function renderBlock($name, $parameters)
{
//load template if needed
if (is_null($this->template))
if (empty($this->templates))
{
$this->loadTemplate();
$this->loadTemplates();
}

if ($this->template->hasBlock($name))
{
return $this->template->renderBlock($name, $parameters);
}
elseif (($parent = $this->template->getParent(array())) !== false)
foreach ($this->templates as $template)
{
return $parent->renderBlock($name, $parameters);
}
else
{
throw new \InvalidArgumentException(sprintf('Block "%s" doesn\'t exist in grid template "%s".', $name, $this->theme));
if ($template->hasBlock($name))
{
return $template->renderBlock($name, $parameters);
}
}

throw new \InvalidArgumentException(sprintf('Block "%s" doesn\'t exist in grid template "%s".', $name, $this->theme));
}

/**
* Return true is block exist in template or parent template
* @param string $name
* Has block
*
* @param $name string
* @return boolean
*/
private function hasBlock($name)
{
//load template if needed
if (is_null($this->template))
if (empty($this->templates))
{
$this->loadTemplate();
$this->loadTemplates();
}

if ($this->template->hasBlock($name))
foreach ($this->templates as $template)
{
return true;
if ($template->hasBlock($name))
{
return true;
}
}

if (!$this->template->hasBlock($name) && ($parent = $this->template->getParent(array())) !== false)
{
return $parent->hasBlock($name);
}
return false;
}

private function loadTemplate()
/**
* @throws \Exception
*/
private function loadTemplates()
{
//get template name
if(is_null($this->theme))
if ($this->theme instanceof \Twig_Template)
{
$this->theme = 'SorienDataGridBundle::blocks.html.twig';
$this->templates[] = $this->theme;
$this->templates[] = $this->environment->loadTemplate($this::DEFAULT_TEMPLATE);
}
elseif (is_string($this->theme))
{
$template = $this->environment->loadTemplate($this->theme);
while ($template != null)
{
$this->templates[] = $template;
$template = $template->getParent(array());
}

$this->template = $this->environment->loadTemplate($this->theme);
$this->templates[] = $this->environment->loadTemplate($this->theme);
}
elseif (is_null($this->theme))
{
$this->templates[] = $this->environment->loadTemplate($this::DEFAULT_TEMPLATE);
}
else
{
throw new \Exception('Bad template definition for grid');
}
}

public function getName()
Expand Down

0 comments on commit 4c9b1ef

Please sign in to comment.