CakePHP: How To Include Element In View In CakePHPPosted by in CakePHP
Hello Friends, As I am writing some important stuffs on CakePHP, But today i want to share one simple and basic thing with you about cakePHP element that how can we include cakePHP element in view. Basically 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.
So to make an element simple make a blank php file then write your element code in it and save that file in your app/views/element folder and save it as .ctp file, for this article we save it as top.ctp. Now in top.ctp you can write code you want to use/repeat in other/all pages. In top.ctp you can put html code for navigation menu or footer links or any other code which you want to use/repeat in pages.
Now below is the syntax by which you can include or call element:
<?php echo $this->element('top', array("variable_name" => "current")); ?>
Here you have to write only ‘top’ as element name, not top.ctp and you can pass one variable to that element(top.ctp) file as shown above. In above example after top there is one array by which we can pass a variable to that element file.If you want to see in detail that how to use passed variables in element then check out here.