我需要根据控制器操作中的某些条件动态设置viewPath。我通过在控制器中放置一个名为getViewPath()的方法来了解一种方法,如下所示:
public function getViewPath() {
$controllername = $this->getId();
$newPath = "application.views.extra";
$newPath = YiiBase::getPathOfAlias($newPath);
return $newPath;
}
但正如我之前所说,我需要根据条件设置viewPath。像这样:
public function actionView($section) {
switch ($section) {
case 'yoga':
$viewpath = 'yoga';
break;
case 'cycling':
$viewpath = 'cycling';
break;
}
// Now this should render from either:
1. protected/views/yoga/
2. protected/views/cycling/
$this->render('view');
}
Controller类有getViewPath()但不是setViewPath(),viewPath属性也是readonly。
感谢您的任何帮助。
答案 0 :(得分:0)
在渲染路径中使用//
默认为protected/views/
,因此您可以执行以下操作:
public function actionView($section) {
switch ($section) {
case 'yoga':
$viewpath = '//yoga/';
break;
case 'cycling':
$viewpath = '//cycling/';
break;
}
// Now this should render from either:
//1. protected/views/yoga/
//2. protected/views/cycling/
$this->render($viewpath.'view');
}