Skip to content

Instantly share code, notes, and snippets.

@purplebeanie
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save purplebeanie/96469427fcea9f5c8902 to your computer and use it in GitHub Desktop.
Save purplebeanie/96469427fcea9f5c8902 to your computer and use it in GitHub Desktop.
DefaultTwigView.php
<?php
namespace Purplebeanie\View;
use Joomla\Filesystem\Path;
use Joomla\Model\ModelInterface;
use Joomla\View\AbstractHtmlView;
use Joomla\Language\Text;
use Pms\Pms;
use emberlabs\GravatarLib\Gravatar;
//include(JPATH_ROOT.'/vendor/emberlabs/gravatarlib/emberlabs/gravatarlib/Gravatar.php');
/**
* Sublcasses the standard AbstractHtmlView to create a view that renders a twig document with the provided data.
*
*/
class DefaultTwigView extends AbstractHtmlView
{
protected $layout = 'default';
protected $path; // the path to the top level template dir
protected $data; //data to be passed to the twig rendered
protected $app; //a link to the app itself
protected $hidesubmenu = false;
var $submenuActions = array();
/**
* Method to instantiate the view.
*
* @param ModelInterface $model The model object.
* @param string $path The path to be used
*
* @since 1.0
*/
public function __construct(Pms $app, ModelInterface $model, $path = null)
{
parent::__construct($model);
// Setup dependencies.
$this->path = isset($path) ? $path : null;
$this->data = array('host'=>JPATH_HOST);
$this->addData('flashbag',$app->getMessageQueue());
$this->app = $app;
$this->addData('business',$app->getSession()->get('business'));
}
/**
* Method to get the view layout.
*
* @return string The layout name.
*
* @since 1.0
*/
public function getLayout()
{
return $this->layout;
}
/**
* Method to render the view.
*
* @return string The rendered view.
*
* @since 1.0
* @throws \RuntimeException
*/
public function render()
{
$this->addData('submenuactions',$this->submenuActions);
//load gravatar and add the user email to the view
$gravatar = new \emberlabs\GravatarLib\Gravatar();
$gravatar->setAvatarSize(60);
if (isset($this->app->getSession()->get('user')->email))
$this->addData('email',$this->app->getSession()->get('user')->email);
// Start an output buffer.
ob_start();
// Load the layout.
$loader = new \Twig_Loader_Filesystem($this->path);
$twig = new \Twig_Environment($loader);
//some filters - these are needed to use the Joomla \Form class
$getLabel = new \Twig_SimpleFilter('getLabel',function($field){
return $field->label;
},array('is_safe'=>array('html')));
$getInput = new \Twig_SimpleFilter('getInput',function($field){
return $field->input;
},array('is_safe'=>array('html')));
$jtextFunction = new \Twig_SimpleFunction('jtext',function($string){
$translation = Text::_($string);
return $translation;
},array('is_safe'=>array('html')));
$twig->addFilter($getLabel);
$twig->addFilter($getInput);
$twig->addFunction($jtextFunction);
$twig->addGlobal('gravatar',$gravatar);
echo $twig->render($this->layout.'.twig',$this->data);
//echo $twig->render('index.twig',$this->data);
// Get the layout contents.
$output = ob_get_clean();
return $output;
}
/**
* Method to set the view layout.
*
* @param string $layout The layout name.
*
* @return AbstractHtmlView Method supports chaining.
*
* @since 1.0
*/
public function setLayout($layout)
{
$this->layout = $layout;
return $this;
}
/**
* Method to add some data to the data array
* @param string key
* @param mixed the obhect to add
*/
public function addData($key,$object)
{
$this->data[$key] = $object;
}
/**
* Method to add an action to the submenu array
* @param array the item to add
*/
public function addSubmenuActions($actions = array())
{
$this->submenuActions = array_merge($this->submenuActions,$actions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment