CakePHP : How To Pass Variable To An Element To Make Menu SelectedPosted by in CakePHP
Hello Friends, As I told you in my last posts that currently I am working on my new cakePHP Project and by this project I am learning so many new things, So today I like to share a new thing with my readers about cakePHP Element that can we pass any variable value to an element in cakePHP.Many applications have small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout. CakePHP can help you repeat parts of your website that need to be reused. These reusable parts are called Elements.
One example of element is Navigation menu, we can make one element file for top menu and include that element in view wherever we want to show navigation menu, Now after include element menu in view I got one problem that how can i show selected menu item, I have tried so much to do this with different ideas like take URL and then check one keywork in URL and then make selected related menu, but this is not a genuine solution for this, So finally I got one solution to make selected menu item in cakePHP is to pass one variable and its value to element in view, So that we can get that variable value in navigation element and make selected menu. Below are the sysntax that how can we pass variable to element.
<?php echo $this->element('front_element/client/after_login', array("buymore" => "current")); ?>
Above are one example to pass variable to element. In above example green path are element path we have to include related to element folder in views folder, So after_login.ctp element exists in “app/views/element/front_element/client/” folder. Word is RED is variable name we want to sent to that element and “current” is the value of variable “buymore”. So in different different pages we can change this variable name and value.
Now we can check in that element to make selected value like this :
<li><a href="#" <?php if($buymore == "current") echo 'class="current"';?>>Buy More</a></li>
Above is one little block of code of after_login.ctp to show that how can we check to make Buy More menu selected.
Let me know if you have any confusion in this or you want any help in this.