In CakePHP, let’s say the default.ctp just does the framing HTML, with <body> containing $content_for_layout only. This is great for most pages of mysite.com. However, let’s say the views within mysite.com/account/ need to share a navigation bar. Which would be the proper approach for the views under the account area?
A) Make the nav bar its own element, and this element is included in each view:
<!-- settings.ctp, profile.ctp, myfiles.ctp ... -->
<div id="account_area">
<?php echo $this->element('account_nav'); }
<div>...</div>
</div>
What I don’t really like in A) is that each .ctp duplicates the same wrapping code (however minimal). So perhaps something like B) is better:
B) Create a view account/index.ctp and have each action set a $section variable and do $this->render('/account/index'), placing each screen in its own element:
<!-- /views/account/index.ctp -->
<div id="account_area">
<div id="account_nav">...</div>
<?php echo $this->element("account/$section"); }
</div>
C) Something else
Thanks, Brian
Typically the way I do this is Option A. But I put the
$this->element('account_nav');in the layout. This prevents putting the code in every view.If the navigation requires configuration or disabling, just add logic to the layout and pass variables to the view as you would normally. Then you can configure it from any action if needed.
In the end, it’s a hybrid approach. But from my experience provides the greatest flexibility.