Php 是的,我正在尝试为我的视图创建一个动态布局
我希望使用一个布局(sections.php)呈现不同的视图,但是布局应该为每个视图加载不同的背景。如何在不为每个视图使用4种不同布局的情况下实现这一点 受保护/视图/站点Php 是的,我正在尝试为我的视图创建一个动态布局,php,html,css,yii,Php,Html,Css,Yii,我希望使用一个布局(sections.php)呈现不同的视图,但是布局应该为每个视图加载不同的背景。如何在不为每个视图使用4种不同布局的情况下实现这一点 受保护/视图/站点 -index.php -about.php -product.php -brands.php 受保护/视图/布局 -sections.php 霉菌控制者 class SiteController extends Controller { public function actionAbout() {
-
-index.php
-about.php
-product.php
-brands.php
-
-sections.php
class SiteController extends Controller
{
public function actionAbout()
{
$this->layout = "sections";
$this->render('about');
}
//functions for other views is similar to above
如果我理解你的问题,你可以这样做。 1.取当前页面名称。 2.为每个页面存储另一个值。 你想去哪里就去哪里
<?php
$page=basename($_SERVER['PHP_SELF']);
if($page == 'Home') {
$css="#Home";
}
if($page == 'about') {
$css="#about";
}
if($page == 'products') {
$css="#product";
}
if($page == 'brands') {
$css="#brands";
}
?>
<div id="<?php echo $css ?>" class="section-cont"> </div>
听起来你想根据页面的不同来改变背景。我这样做的方式不是为每个页面创建一个视图。在sections布局中为类的body标记中设置变量。例如:
<body class="<?php echo $body_class; ?>">
然后,您将能够以页面为目标,并通过在CSS中以类为目标来更改背景图像
希望这有帮助
编辑:需要将变量传递到布局的问题
对不起,我对布局和查看文件感到困惑。
Layout将访问当前控制器的$this
。
因此,在控制器中,如果添加公共属性/可变土地,请按如下方式编辑操作:
class SiteController extends Controller
{
public $body_class;
public function actionTest()
{
$this->layout = "testlayout";
$this->body_class = 'test-page';
$this->render('test');
}
然后将布局文件中的body类或div类更改为$this->body\u class
注意:如果您愿意,也可以在页面视图中指定$this->body\u class
。Yii。我投降_我正在更改一个div的背景,而该div只在布局文件中,而不是视图文件中,你能将变量发送到布局文件吗?@YungA编辑了我的原始评论,以回答问题,以获得更好的格式。哇..非常感谢dude。你的回答最好。我从没想过布局文件可以访问$this
或当前控制器
<body class="<?php echo $body_class; ?>">
$this->render('about',array('body_class'=> 'about-page'));
class SiteController extends Controller
{
public $body_class;
public function actionTest()
{
$this->layout = "testlayout";
$this->body_class = 'test-page';
$this->render('test');
}