Reputation: 3499
Im here again with a question about yii framework.
I've got a page under views/myviewname/admin.php.
I've got a page under views/myotherviewname/admin.php.
Now i want to give those pages another style. But how do i do that?
I've created a page under themes/classis/views/myviewname/admin.php
and in that file i got this:
<?php /* @var $this Controller */ ?>
<?php echo $content; ?>
But i get an error. Because $content
is not defined.
How do i style those pages? Would be nice if i can style all admin pages at once.
Upvotes: 1
Views: 293
Reputation: 14459
First of all, this is undeniable that $content
variable will be known as undefined, since it can only be used in Layouts
, not Views
.
As you probably know, if you already have set a theme for your application(in main config file by 'theme'=>'myTheme'
), Yii looks for that into themes/myTheme
and all views will be rendered in themes/myTheme/views/x/y.php
instead of views/x/y.php
. Also, your layouts will be overridden by layouts located into themes/myTheme/layouts
.
Now, lets assume that we want to create 2 themes:
We should create structures like below:
+themes
+darkTheme
+views
+layouts
+main.php
+myLayout1.php
+myLayout2.php
+myController
+myView1.php
+lightTheme
+views
+layouts
+main.php
+myLayout1.php
+myLayout2.php
+myController
+myView1.php
We have a main.php
which holds our base theme structure(skeleton), and 2 layouts named myLayout1.php
and myLayout2.php
respectively. Also we already defined a default layout into our base controller(Usually Controller.php
) like below:
public $layout='//layouts/myLayout1';
Now, we have a main layout which shows everything inside myLayout1
by default. We can change layout in out action like below:
$this->layout="myLayout2";
Also we can change application theme like below:
Yii::app()->theme="lightTheme";
Note: Theme name is case-sensitive. If you attempt to activate a theme that does not exist, Yii::app()->theme will return null.
Above codes can be written into beforeAction()
method or every action. Please note that, if you render myView1
($this->render('myView1')
) and if the theme is set to darkTheme
, Yii will render themes/darkTheme/views/myController/myView1.php
instead of views/myConteoller/myView1.php
.
To be more clear, $content
will be used in layouts. Also, this is remarkable that, $content
will be replaced by everything inside a view. So if you want to modify the whole page's schema, you must modify main.php
layout. In front, if you want to modify the style of a view's content, you need to modify your layout.
Upvotes: 2