Home

Yii: How to set a value in the parent layout from a child view

Published on 3/9/2011


Say you have a search box at the top of your page, but you want to make it contextual to the area that you’re in. For example, on the contacts view, you want the search button to say “Search Contacts”. Your search button is on the layouts/main.php page. Here’s how:

open protected/components/Controller.php

add a variable, such as “searchButtonArea”

in layouts/main.php, add that value to the search buttons, such as searchButtonArea"?>

then, in your contact views, set $this->searchButtonArea = “Contacts”;

Or, add a property to the ContactsController, $searchButtonArea=‘Contacts’;

... views