Skip to content

Instantly share code, notes, and snippets.

@aftabnaveed
Last active August 29, 2015 14:21
Show Gist options
  • Save aftabnaveed/364645cfb3bc1851b8fc to your computer and use it in GitHub Desktop.
Save aftabnaveed/364645cfb3bc1851b8fc to your computer and use it in GitHub Desktop.
<?php
namespace Digibuzz\CoreBundle\Design;
use Tree\Node\NodeInterface;
use Tree\Node\Node;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Templating\TemplateNameParser;
use Symfony\Component\Templating\Loader\FilesystemLoader;
use Symfony\Component\Templating\DelegatingEngine;
use Symfony\Bridge\Twig\TwigEngine;
class Block extends Node
{
/**
* Template to be rendered
*/
protected $templateName = '';
static $blocks = array();
/**
* Block Type, must be an instance of Block
*
*/
protected $type = null;
protected $templating = null;
public function __construct($name, $type = null, $templateName = '', $children = array())
{
if($this->type !== null) {
$this->type = $type;
}
if($templateName != '') {
$this->templateName = $templateName;
}
self::$blocks[$name] = $this;
parent::__construct($type, $children);
}
public function addBlock(NodeInterface $block)
{
$this->addChild($block);
return $this;
}
public function getChildHtml($name)
{
if(isset(self::$blocks[$name])) {
$block = self::$blocks[$name];
}
if($block->isLeaf()) {
return $block->render();
}
foreach($block->getChildren() as $_block) {
$out .= $_block->render();
}
return $out;
}
public function setType(Block $blockType)
{
$this->type = $blockType;
return $this;
}
public function setLayout($layout)
{
$this->layout = $layout;
}
public function getLayout()
{
return $this->layout;
}
public function removeBlock(NodeInterface $block)
{
$this->removeChild($block);
return $this;
}
public function getTemplateEngine()
{
$loader = new FilesystemLoader(__DIR__.'/views/%name%');
$templating = new PhpEngine(new TemplateNameParser(), $loader);
$engines[] = $templating;
$delegationEngine = new DelegatingEngine($engines);
return $delegationEngine;
}
public function setTemplateEngineService($service)
{
$this->templating = $service;
}
public function setTemplate($templateName)
{
}
/**
* Pass the Block type if given and render the template
*
*/
public function render()
{
$params = array('this' => $this);
//$this->templating = $this->getLayout()->getTemplateEngineService();
$this->templating = $this->getTemplateEngine();
return $this->templating->render($this->templateName, $params);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment