Skip to content

Commit

Permalink
Dev: refactorisation of twig renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Dec 16, 2016
1 parent e83ef48 commit 543ee2b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
48 changes: 29 additions & 19 deletions application/core/LSETwigViewRenderer.php
Expand Up @@ -47,38 +47,48 @@ public function addExtensions($extensions)

/**
* Renders a view file.
* This method is required by {@link IViewRenderer}.
* @param CBaseController $context the controller or widget who is rendering the view file.
* @param string $sourceFile the view file path
* @param mixed $data the data to be passed to the view
* @param boolean $return whether the rendering result should be returned
* @return mixed the rendering result, or null if the rendering result is not needed.
*/
public function renderFile($context, $sView, $aData, $bReturn=true)
public function render( $sView, $aData, $bReturn=true)
{
global $thissurvey;
$requiredView = Yii::getPathOfAlias('application.views').$sView;
if(isset($thissurvey['template']))
{
$sTemplate = $thissurvey['template'];
$oTemplate = Template::model()->getInstance($sTemplate); // we get the template configuration
if($oTemplate->overwrite_question_views===true && Yii::app()->getConfig('allow_templates_to_overwrite_views')) // If it's configured to overwrite the views
{
if( file_exists($requiredView.'.php') || file_exists($requiredView.'.twig') ) // If it the case, the function will render this view
{
Yii::setPathOfAlias('survey.template.view', $requiredView); // to render a view from an absolute path outside of application/, path alias must be used.
$sView = 'survey.template.view'; // See : http://www.yiiframework.com/doc/api/1.1/CController#getViewFile-detail
$requiredView = $oTemplate->viewPath.ltrim($sView, '/'); // Then we check if it has its own version of the required view
}
}

$this->_twig = parent::getTwig(); // Twig object
$loader = $this->_twig->getLoader(); // Twig Template loader
$oTemplate = Template::model()->getInstance($thissurvey['template']); // Template configuration

$requiredView = Yii::getPathOfAlias('application.views').$sView; // By default, the required view is the core view
$loader->setPaths(App()->getBasePath().'/views/'); // Core views path

// Check if template provides its own twig view
if(file_exists($oTemplate->viewPath.ltrim($sView, '/').'.twig')){
$loader->setPaths($oTemplate->viewPath); // Template views path
$sView = str_replace('/views/', '', $sView );
$requiredView = $oTemplate->viewPath.ltrim($sView, '/');
}

// Twig or not twig?
// We check if the file is a twig file or a php file
// This allow us to twig the view one by one, from PHP to twig.
// The check will be removed when 100% of the views will have been twig
if( file_exists($requiredView.'.twig') ){
return parent::renderFile( $context, $requiredView.'.twig', $aData, $bReturn);

// We're not using the Yii Theming system, so we don't use parent::renderFile
// current controller properties will be accessible as {{ this.property }}
$data['this'] = Yii::app()->getController();
$template = $this->_twig->loadTemplate($sView.'.twig')->render($data);

if ($bReturn) {
return $template;
}else{
echo $template;
}
}else{
return Yii::app()->getController()->renderPartial($sView, $aData, $bReturn);
}

}


Expand Down
2 changes: 1 addition & 1 deletion application/helpers/qanda_helper.php
Expand Up @@ -6560,5 +6560,5 @@ function getLabelInputWidth($labelAttributeWidth,$inputAttributeWidth){
*/
function doRender($sView, $aData, $bReturn=true)
{
return Yii::app()->twigRenderer->renderFile( Yii::app()->getController(), $sView, $aData, $bReturn);
return Yii::app()->twigRenderer->render( $sView, $aData, $bReturn);
}

0 comments on commit 543ee2b

Please sign in to comment.