Skip to content

Commit

Permalink
Dev: overload ETwigViewRenderer::renderFile() to check if template ov…
Browse files Browse the repository at this point in the history
…erride core views
  • Loading branch information
LouisGac committed Dec 15, 2016
1 parent fb33d6b commit a8f1aa9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions application/core/LSETwigViewRenderer.php
Expand Up @@ -45,4 +45,40 @@ 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

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Dec 15, 2016

Collaborator

array, not mixed

* @param boolean $return whether the rendering result should be returned
* @return mixed the rendering result, or null if the rendering result is not needed.

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Dec 15, 2016

Collaborator

string|null

This comment has been minimized.

Copy link
@LouisGac

LouisGac Dec 15, 2016

Contributor

To be honnest, I just copy/paste the original doc of ETwigViewRenderer.
I should also write a comment to explain the goal of it a little bit.

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Dec 15, 2016

Collaborator

👍

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Dec 15, 2016

Collaborator
*/
public function renderFile($context, $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
{
$requiredView = $oTemplate->viewPath.ltrim($sView, '/'); // Then we check if it has its own version of the required view
if( file_exists($requiredView.'.php') ) // 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
}
}
}

// Twig or not twig?
if( file_exists($requiredView.'.twig') ){
return parent::renderFile( Yii::app()->getController(), $requiredView.'.twig', $aData, $bReturn);
}else{
return Yii::app()->getController()->renderPartial($sView, $aData, $bReturn);
}
}
}

10 comments on commit a8f1aa9

@LouisGac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now, anywhere in frontend, we can use:
app()->twigRenderer->renderFile( Yii::app()->getController(), $sView, $aData, $bReturn);
it will use the template view if exists, else the core one. It accepts php files like twig files

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really usage of global $thissurvey ?

And update $requiredView is template is allowed to $oTemplate->viewPath.ltrim($sView, '/');

And use it after without reset it in if( file_exists($requiredView.'.twig') ){

Then :

  1. set allow_templates_to_overwrite_views in config + overwrite_question_views in template
  2. Don't put any files in $oTemplate->viewPath
  3. Test the survey : send error about a file don't exist.

No ?

@olleharstedt
Copy link
Collaborator

@olleharstedt olleharstedt commented on a8f1aa9 Dec 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ever the case that first argument is not getController()?

Edit: OK, can be widget, too.

@olleharstedt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phpdocs arguments must be named same as in function signature, otherwise scrutinizer will complain.

@LouisGac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scrutinizer can whine as a cry baby ^^
about the first argument hell, it's because of the interface that force the signature of the method....

@olleharstedt
Copy link
Collaborator

@olleharstedt olleharstedt commented on a8f1aa9 Dec 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making a wrapper function as the internal API instead? Less writing.

@olleharstedt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, $context is not used. More scrutinizer complains. ;)

@LouisGac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shnoulle: I still didn't tested to overwrite a view. I suppose it's working. I'll test it now.
@olle: for the controller first argument? Yep, I was thinking about something like that.
@scrutinizer: http://m.memegen.com/r7un8v.jpg

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not about overwriting a view ability ... its' just : code is broken ....

About : scrutinizer : don't copy paste, use @ see

@LouisGac
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.