2016-06-22 3 views
0

Ich versuche, einen Variablenwert vom Controller an meine Layoutdatei main.php übergeben, aber ich bekomme immer Fehler Undefinierte Variable: contentWrapperBackground. unten ist mein CodeWie Werte von Controller zu Layout-Datei in yii2 übergeben werden

controller 
public function actionList() 
{ 

    $contentWrapperBackground = "ff4400;"; 
    $contentBackground = "3c8dbc;"; 
    return $this->render('list', [ 

     'contentWrapperBackground' =>$contentWrapperBackground, 
     'contentBackground' => $contentBackground, 

    ]); 
} 

und in meinem Layout-Datei wie diese

<div class="content-wrapper" style="background-color:#<?=$contentWrapperBackground?>"> 
    <!-- Content Header (Page header) --> 


    <!-- Main content --> 
    <section class="content" style="background-color:#<?=$contentBackground?>"> 

aber ich immer Fehler undefinierte Variable erhalten: contentWrapperBackground. Ich versuche, die Hintergrundfarbe für verschiedene Seiten zu ändern. jede Hilfe zu diesem Thema, und ich bin auch offen für eine andere Idee, wie man das funktioniert dank

+0

Mögliches Duplikat von [Wie param von Controller-Layout in YII2 passieren] (http://stackoverflow.com/questions/28038912/how-to-pass-param-from-controller-to-layout-in-yii2) – soju

+0

Verwenden s et Sitzung und erhalten Sitzungskonzept .. –

Antwort

2

nicht Sitzung dafür verwenden!

Einfache Lösung:

class Controller extends \yii\web\Controller 
{ 
    $public $contentWrapperBackground; 

    $public $contentBackground; 
} 

class YourController extends Controller 
{ 
    public function actionList() 
    { 

     $this->contentWrapperBackground = "ff4400;"; 
     $this->contentBackground = "3c8dbc;"; 
     return $this->render('list', []); 
    } 
} 

in Ihrem Haupt-Layout

<div class="content-wrapper" style="background-color:#<?=Yii::$app->controller->contentWrapperBackground?>"> 

oder eine andere Option

<div class="content-wrapper" style="background-color:#<?=$this->context->contentWrapperBackground?>"> 
Verwandte Themen