Skip to content

Instantly share code, notes, and snippets.

@technokid
Created May 2, 2018 16:22
Show Gist options
  • Save technokid/e8838a26f09aa0349be349c12bd66a44 to your computer and use it in GitHub Desktop.
Save technokid/e8838a26f09aa0349be349c12bd66a44 to your computer and use it in GitHub Desktop.
Yii console render file
<?php
/**
* Took the components necessary to render views from CController, adding it to a console command
*/
class RenderReadyConsoleCommand extends CConsoleCommand
{
protected $_widgetStack;
public function run($args) { }
public function renderPartial($view,$data=null,$return=true)
{
if(($viewFile=$this->getViewFile($view))!==false)
{
$output=$this->renderFile($viewFile,$data,true);
if($return)
return $output;
else
echo $output;
}
else
throw new CException(Yii::t('yii','{controller} cannot find the requested view "{view}".',
array('{controller}'=>get_class($this), '{view}'=>$view)));
}
/**
* File Render Methods
*/
/**
* Renders a view file.
*
* @param string view file path
* @param array data to be extracted and made available to the view
* @param boolean whether the rendering result should be returned instead of being echoed
* @return string the rendering result. Null if the rendering result is not required.
* @throws CException if the view file does not exist
*/
public function renderFile($viewFile,$data=null,$return=false)
{
$widgetCount=count($this->_widgetStack);
$content=$this->renderInternal($viewFile,$data,$return);
if(count($this->_widgetStack)===$widgetCount)
return $content;
else
{
$widget=end($this->_widgetStack);
throw new CException(Yii::t('yii','{controller} contains improperly nested widget tags in its view "{view}". A {widget} widget does not have an endWidget() call.',
array('{controller}'=>get_class($this), '{view}'=>$viewFile, '{widget}'=>get_class($widget))));
}
}
/**
* Renders a view file.
* This method includes the view file as a PHP script
* and captures the display result if required.
* @param string view file
* @param array data to be extracted and made available to the view file
* @param boolean whether the rendering result should be returned as a string
* @return string the rendering result. Null if the rendering result is not required.
*/
public function renderInternal($_viewFile_,$_data_=null,$_return_=false)
{
// we use special variable names here to avoid conflict when extracting data
if(is_array($_data_))
extract($_data_,EXTR_PREFIX_SAME,'data');
else
$data=$_data_;
if($_return_)
{
ob_start();
ob_implicit_flush(false);
require($_viewFile_);
return ob_get_clean();
}
else
require($_viewFile_);
}
/**
* File Retrieval Methods
*/
public function getViewFile($viewName)
{
return $this->resolveViewFile($viewName,$this->getViewPath());
}
public function getViewPath()
{
return dirname(__FILE__).'/../views/email';
}
public function resolveViewFile($viewName,$viewPath)
{
if(empty($viewName))
return false;
$extension='.php';
$viewFile=$viewPath.DIRECTORY_SEPARATOR.$viewName;
if(is_file($viewFile.$extension))
return Yii::app()->findLocalizedFile($viewFile.$extension);
else if($extension!=='.php' && is_file($viewFile.'.php'))
return Yii::app()->findLocalizedFile($viewFile.'.php');
else
return false;
}
public function createWidget($className,$properties=array())
{
$className=Yii::import($className,true);
$widget=new $className($this);
foreach($properties as $name=>$value)
$widget->$name=$value;
return $widget;
}
public function widget($className,$properties=array(),$captureOutput=false)
{
if($captureOutput)
{
ob_start();
ob_implicit_flush(false);
try
{
$widget=$this->createWidget($className,$properties);
$widget->run();
}
catch(Exception $e)
{
ob_end_clean();
throw $e;
}
return ob_get_clean();
}
else
{
$widget=$this->createWidget($className,$properties);
$widget->run();
return $widget;
}
}
public function beginWidget($className,$properties=array())
{
$widget=$this->createWidget($className,$properties);
$this->_widgetStack[]=$widget;
return $widget;
}
public function endWidget($id='')
{
if(($widget=array_pop($this->_widgetStack))!==null)
{
$widget->run();
return $widget;
}
else
throw new CException(Yii::t('yii','{controller} has an extra endWidget({id}) call in its view.',
array('{controller}'=>get_class($this),'{id}'=>$id)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment