{"id":48,"date":"2008-09-24T16:20:10","date_gmt":"2008-09-24T20:20:10","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=48"},"modified":"2015-01-30T14:19:47","modified_gmt":"2015-01-30T19:19:47","slug":"simple-masterpages-with-php","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2008\/09\/24\/simple-masterpages-with-php\/","title":{"rendered":"Simple MasterPages with PHP"},"content":{"rendered":"<p>If you are an ASP.Net developer used to working with MasterPages (or any equivalant on another framework), you may be looking for an equivalant while working with php.\u00a0 I was looking to do something like that with my homepage (hoolihan.net).\u00a0 This is a very simple site, where a full framework would have been overkill.<\/p>\n<p>Here&#8217;s how I pieced it out:<\/p>\n<p>First: Create a template page, I called mine master.php.<br \/>\n<code><!-- head, title, css, rss feeds, etc here --><br \/>\n&lt;body&gt;<br \/>\n&lt;div&gt;&lt;?php include('menu.php');?&gt;&lt;\/div&gt;<br \/>\n&lt;div&gt;&lt;?php include($page_content);?&gt;&lt;\/div&gt;<br \/>\n&lt;div&gt;&lt;?php include('footer.php');?&gt;&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<\/code><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- php ad --><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:inline-block;width:468px;height:60px\"\n     data-ad-client=\"ca-pub-7560642149740630\"\n     data-ad-slot=\"3652451363\"><\/ins><br \/>\n<script>\n(adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<p>Second: Create a content piece, for example about_text.php.<br \/>\n<code>&lt;p&gt;This is information about me.&lt;\/p&gt;<\/code><\/p>\n<p>Third: Create the page with the actual name you want to use:<br \/>\n<code>&lt;?php<br \/>\n$page_content = 'about_text.php';<br \/>\ninclude('master.php');<br \/>\n?&gt;<\/code><\/p>\n<p>One\u00a0added benefit is that the content piece\u00a0is then loadable in other places if need be.\u00a0<\/p>\n<p>Obviously, you&#8217;ll want to add a few bells and whistles.\u00a0\u00a0 For example,\u00a0It&#8217;s a good idea to check for $page_content being null, and provide a default in that case.\u00a0 I left out some of those things for the sake of brevity.<\/p>\n<p>For more patterns in php, check out the following books:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.amazon.com\/gp\/product\/143022925X\/ref=as_li_ss_tl?ie=UTF8&#038;tag=timhoosblo-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399369&#038;creativeASIN=143022925X\">PHP Objects, Patterns and Practice (Expert&#8217;s Voice in Open Source)<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=&#038;l=as2&#038;o=1&#038;a=143022925X&#038;camp=217145&#038;creative=399369\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><\/li>\n<li><a href=\"http:\/\/www.amazon.com\/gp\/product\/1430210117\/ref=as_li_ss_tl?ie=UTF8&#038;tag=timhoosblo-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399369&#038;creativeASIN=1430210117\">PHP Object-Oriented Solutions<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.com\/e\/ir?t=&#038;l=as2&#038;o=1&#038;a=1430210117&#038;camp=217145&#038;creative=399369\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/><\/li>\n<\/ul>\n<p>Basically, it&#8217;s a real simple way to cleanly break up your php pages.\u00a0 Hope\u00a0you found \u00a0this useful&#8230;<\/p>\n<p>Update: I&#8217;ve posted <a href=\"http:\/\/hoolihan.net\/blog-tim\/2011\/08\/31\/more-on-using-masterpages-layouts-patterns-in-languages-like-php\/\" title=\"More on Using Masterpages (Layouts) Patterns in Languages Like PHP\">a follow-up article<\/a> about the ajax ideas mentioned in this article. Also, see more <a href=\"http:\/\/hoolihan.net\/blog-tim\/tag\/php\/\" target=\"_blank\">php posts<\/a> on this blog and <a href=\"http:\/\/hoolihan.net\/blog-tim\/category\/programming\/\" target=\"_blank\">more programming posts<\/a>.<\/p>\n<p>Update 2: User Ton posted a <a href=\"#comment-2838\">good comment below<\/a> about how to setup the content variable (and simplify your url) with apache.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to do a straight forward version of ASP.Net masterpage template in PHP. No framework required.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,18,24],"tags":[39,38],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-aspnet","category-programming","category-web","tag-master-pages","tag-php"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/48\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}