Question is in the title.
I have a conditional menu.
So I wrote an array in an element.
But how can I pass this array to the view ?
debug(array) returns “Undefined variable”
I think you can set array in Session or cookies and then you can use it into view. But you need to call element first then you can use it into the view file.
Trying to pass anything from an element to a view is an indication that you’re doing things in the wrong place. Perhaps an element isn’t really the right place for that code, for example, or maybe you’re trying to do too much in that element. An element should generate output, that’s all. If you add more description about your specific case, and some pertinent code, we may be able to give more specific advice.
Thank you for your answers.
I did not provide code because it is straightforward.
A simple array containing links :
$menu[link_1] = $first_link;
$menu[link_2] = $second_link;
…
According to the context (controller, cookie value), I need to display several elements of the array. It may be 1, 2, 5, 6 or 2, 3, 4, 8 etc. All pages are different.
I first tried a cell but it did not work. Then I tried an element (I generally write menus in an element).
@ [Zuluru] I get the idea. I’ll try the app controller.
-> does not work as you can’t use $this->Html->link (html helper) in a controller.
I got it !
The Element was the right way (at least one right way)
my array is set this way :
$main_menu[‘start_1’] = ‘<ul class=“menu-consultation”>’;
$main_menu[‘link_1’] = ‘<li>’ . $this->Html->link("> Consulter", [‘plugin’ => ‘Medias’, ‘controller’ => ‘dossiers’, ‘action’ => ‘index’]) . ‘</li>’;
$main_menu[‘link_2’] = ‘<li>’ . $this->Html->link("> Rechercher", [‘plugin’ => ‘Medias’, ‘controller’ => ‘fichiers’, ‘action’ => ‘search’]) . ‘</li>’;
$main_menu[‘start_2’] = ‘<ul class=“menu-gestion”>’;
$main_menu[‘link_3’] = ‘<li>’ . $this->Html->link("> Déposer un fichier", [‘plugin’ => ‘Medias’, ‘controller’ => ‘dossiers’, ‘action’ => ‘choose’]) . ‘</li>’;
$main_menu[‘link_4’] = ‘<li>’ . $this->Html->link("> Ajouter un dossier", [‘controller’ => ‘dossiers’, ‘action’ => ‘select’, ‘plugin’ => ‘Medias’]) . ‘</li>’;
$main_menu[‘link_5’] = ‘<li>’ . $this->Html->link("> Édition", ‘javascript:editMode()’) . ‘</li>’;
etc …
$main_menu[‘end’] = ‘</ul>’;
Then each time I need menu elements, I load the element and pass it another array this way :
echo $this->element(‘main-menu’, [‘lines’ => array(‘start_1’,‘link_1’,‘link_4’,‘end’)]);
Thank you all for helping me.
I think you can create an array-like:
$menuLinks = array(
‘Consulter’ => array(
‘dossiers’ => ‘index’
),
‘Rechercher’ => array(
‘fichiers’ => ‘search’
)
);
Pass array to element:
echo $this->element(‘main-menu’, [‘menuLinks’ => $menuLinks]);
How you can create link in element file:
> if (! empty($menuLinks)) {
echo '<ul class="menu-consultation">'; foreach ($menuLinks as $link => $details) { $controller = key($details); $action = $details[$controller]; echo '<li>' . $this->Html->link('> ' . $link, [ 'plugin' => 'Medias', 'controller' => $controller, 'action' => $action ]) . '</li>'; } echo '</ul>'; }
Please let me know if it work for you.
Thank you for trying to help.
I did not try you solution but it may work.
One thing I did not mention is that the elements on the menu will (probably) never change so they are hard coded (It is an enterprise application, not web site).
Look at mine, it is very flexible.
Thank you again for taking time to help others.