Right now, I am building out menus and they are all static, but,
certain ones should only be displayed on certain pages.
I have created some nested elements and variables passed in from the
controller to each element to decide what to display. It seems to
work well for may purposes so far. All the menus are in one place
and changes to one file, updates all the pages.
But, is this a good way to do this? as I add more and more
controllers and other things, is this going to start to fail? should
I be looking at a helper instead?
I would appreciate some feedback on this design.....
I start with a common view
view/common/organization.ctp
<?php
echo $this->element('leftmenu');
?>
<div class="organization view">
<h1><?php echo $this->fetch('title'); ?></h1>
<?php echo $this->fetch('content'); ?>
</div>
The leftmenu element defines all the menu options.
view/elements/leftmenu.ctp
<?php
echo $this->element('menus/leftmenu-start');
echo $this->element('menus/leftmenu-user-home' );
echo $this->element('menus/leftmenu-org' );
echo $this->element('menus/leftmenu-member' );
echo $this->element('menus/leftmenu-roles' );
echo $this->element('menus/leftmenu-levels' );
echo $this->element('menus/leftmenu-logout' );
echo $this->element('menus/leftmenu-end');
?>
The leftmenu-start is just to define some basic info about how the
menu is to be built.
view/elements/leftmenu-start.ctp
<div class="actions">
<h3><?php echo __('Menu'); ?></h3>
<ul>
Then in a specific menu like org - I put all the possible menu items
that are related to the controller. The controller creates the
variable that has information used by the menu elements to decide
what to display or provide needed parameters. Since you can not
pass an array into elements, the "assign" function only allows
strings, I used getVar to get the parameters.
view/elements/leftmenu-org.ctp
<?php
$menuVars= $this->getVar('menuVars');
$orgId = $menuVars['orgId'];
$member = $menuVars['orgMember'];
$update = $menuVars['orgUpdate'];
$userId = $menuVars['userId'];
?>
<li><?php echo
$this->Html->link(__('List
ALL Organization'), array('controller' =>
'organizations','action' => 'index', 'organization' =>
$orgId, null)); ?> </li>
<li><?php echo
$this->Html->link(__('My
Organizations'), array('controller' => 'organizations','action'
=> 'index', 'organization' => $orgId, $userId)); ?>
</li>
<li><?php echo
$this->Html->link(__('Add
Organization'), array('controller' => 'organizations', 'action'
=> 'add','organization' => $orgId, $orgId)); ?>
</li>
<?php if (!$member) : ?>
<li><?php echo
$this->Html->link(__('Join
Organization'), array('controller' => 'organizations', 'action'
=> 'join','organization' => $orgId, $orgId)); ?>
</li>
<?php else : ?>
<li><?php echo
$this->Html->link(__('Cancel
Membership'), array('controller' => 'memberRelationships',
'action' => 'cancel', 'organization' => $orgId, $orgId));
?> </li>
<?php endif; ?>
In the above example, the controller has the information that the
user is a member and if they are not a member, the element will
display the "Join" button. But if they are a member, then it will
display the "Cancel" button.
The controller just has to put all the information that it knows
about and then pass in 'menuVars' using set.
in controller
$this->set('menuVars', $menuVars);
Again, this seems to work for the one common layout that I have
setup so far. But I now need to start adding and fixing other common
layouts to use this same design.
So, before I do a bunch of work, then have to change later, I wanted
to get some comments on this design.
If there is a better way to do this, please let me know.
Thanks,
Bill
--
View this message in context:
http://cakephp.1045679.n5.nabble.com/Is-using-Elements-the-best-way-to-build-menus-tp5708485.html
Sent from the CakePHP mailing list archive at Nabble.com.
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php