{"id":78,"date":"2011-09-28T01:24:58","date_gmt":"2011-09-28T01:24:58","guid":{"rendered":"http:\/\/info.ffteixeira.net\/2011\/09\/28\/cakephp-working-with-elements\/"},"modified":"2011-09-28T01:24:58","modified_gmt":"2011-09-28T01:24:58","slug":"cakephp-working-with-elements","status":"publish","type":"post","link":"https:\/\/blog.ffteixeira.net\/?p=78","title":{"rendered":"CakePHP: working with Elements"},"content":{"rendered":"<p>An element (like it is named) is a part of a web page which can be display into multiple pages (a sort of template if you want). Here are some elements\u2019 examples:<\/p>\n<ul>\n<li>A header: with your banner, your brand logo<\/li>\n<li>A menu: with your categories, links<\/li>\n<li>A footer: company name, copyright<br \/>\n<!--more--><br \/>\nAn element (like it is named) is a part of a web page which can be display into multiple pages (a sort of template if you want). Here are some elements\u2019 examples:<\/p>\n<ul>\n<li>A header: with your banner, your brand logo<\/li>\n<li>A menu: with your categories, links<\/li>\n<li>A footer: company name, copyright<!--more--><\/li>\n<\/ul>\n<p>In CakePHP, Elements are located in the app\/views\/elements\/ directory (or if you are using a custom theme you can create specific elements and put them in the \/app\/views\/themed\/your_theme_folder\/elements\/ directory).<\/p>\n<p>So let\u2019s get started. We will create a basic element which will display \u2018Hello World!\u2019:<\/p>\n<div>\n<div>\n<h1>Hello World<\/h1>\n<\/div>\n<\/div>\n<p>You can save this Element: app\/view\/elements\/helloworld.ctp<br \/>\nThe last step is to include the element in our pages, to do so you just have to request it like that:<\/p>\n<div>\n<div>\n<pre>\/*For instance I want to display Hello Word on my app\/views\/posts\/index.ctp page:*\/\necho $this-&gt;element('helloworld');<\/pre>\n<\/div>\n<\/div>\n<h3>Elements using Controller with requestAction<\/h3>\n<p>Displaying basic information is great, but it would be much more better if we could access to our data and request something like the five latest comments or posts added. This is indeed possible through the requestAction method.<\/p>\n<p>First of all, we have to add a method in our controller which will allow us to retrieve the data we want to render.<\/p>\n<div>\n<div>\n<pre>\/* For instance I want to display the 5 latest posts on my index page. *\/\npublic function lastposts($limit=5)\n{\n\/* We retrieve only the required fields, and configure the query. *\/\n$posts = $this-&gt;Post-&gt;find('all', array('fields'=&gt;array('Post.id', 'Post.name', 'Post.created'),\n   'recursive'=&gt;0,\n   'order'=&gt;array('Post.created desc'),\n   'limit'=&gt;$limit));\nif(isset($this-&gt;params['requested']))\n{\nreturn $posts;\n}\n$this-&gt;set('lastposts', $posts);\n}<\/pre>\n<\/div>\n<\/div>\n<p>Second of all, we create our element. This step is exactly the same as what we have done before, create a page called lastposts.ctp in your app\/views\/elements\/ directory but this time there will be more code\u2019s lines.<\/p>\n<p>The first thing to do in your element is to get our data, to do that you will have to use the requestAction method, now you are ready to display your latest posts:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An element (like it is named) is a part of a web page which can be display into multiple pages (a sort of template if you want). Here are some elements\u2019 examples: A header: with your banner, your brand logo &hellip; <a href=\"https:\/\/blog.ffteixeira.net\/?p=78\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78"}],"version-history":[{"count":0,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ffteixeira.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}