6
我在視圖中有一個電子郵件模板,我想編寫一個ConsoleApplication過程來準備要發送的電子郵件。因爲它是ConsoleApplication我沒有訪問控制器。是否有任何方法來呈現視圖?在yii控制檯應用程序中渲染視圖
我在視圖中有一個電子郵件模板,我想編寫一個ConsoleApplication過程來準備要發送的電子郵件。因爲它是ConsoleApplication我沒有訪問控制器。是否有任何方法來呈現視圖?在yii控制檯應用程序中渲染視圖
下面是我用:
private function render($template, array $data = array()){
$path = Yii::getPathOfAlias('application.views.email').'/'.$template.'.php';
if(!file_exists($path)) throw new Exception('Template '.$path.' does not exist.');
return $this->renderFile($path, $data, true);
}
它需要電子郵件模板從的意見/電子郵件。
如果一切都失敗(在我的情況):
<?php
/**
* Renders a view file & returns result.
* @param string $_viewFile_ view file path
* @param array $_data_ optional data to be extracted as local view variables
* @param boolean $_return_ whether to return the rendering result instead of displaying it
* @return mixed the rendering result if required. Null otherwise.
*/
public function myRenderPartial($_viewFile_,$_data_=null,$_return_=true) {
if(is_array($_data_))
extract($_data_,EXTR_PREFIX_SAME,'data');
else
$data=$_data_;
if($_return_)
{
ob_start();
ob_implicit_flush(false);
require(YiiBase::getPathOfAlias("application.views").$_viewFile_.'.php');
return ob_get_clean();
}
else
{
require($_viewFile_);
}
}
?>
有從模型來呈現什麼好辦法?或者是否有任何與CConsoleCommand目前啓動的鏈接? – liysd 2010-11-20 19:22:14
將此方法添加到您的控制檯命令。您可以將數組$ this和您的模型傳遞給$ data參數。 – 2010-11-20 19:41:06
downvote,沒有提供使用示例,它只是粘貼到命令中有什麼混亂 – Tebe 2016-09-14 12:35:00