Precondition: coming from a Custom PHP applications(using symfony, zend and so on) world, for me it feels unnatural to have template separated in a way that is done in original wordpress theme. e.g top part of a page is in header, center in a “index, page…” and bottom part in a footer.
example of current structure:
index.php
[include header]
... main content...
[include footer]
and header.php
<html>
<head>
....
</head>
<body>
.....
and footer.php
.....
</body>
</html>
I am more used to the structure where you have a layout and include other (partials) template inside it.
example: index.php template
<html>
<head> [include header] </head>
<body>
[include body] [include footer]
</body>
</html>
So I am trying to think about advantages of original wordpress template structure.
Question: What are the ADVANTAGES of ORIGINAL wordpress template structure in contrast to the SECOND example I described above ?
First, as @Toscho implies, the
get_header()
andget_footer()
template tags simply include files namedheader.php
andfooter.php
, respectively. Both template tags are called from within template files, and you can separate content between template file (index.php
) and template part file (header.php
,footer.php
) any way you want. So, WordPress certainly can accommodate your latter example.That said, the real power in the standard, “WordPress” method of separating content derives from the WordPress Template Hierarchy. In WordPress, the
index.php
file is actually the last template file used. It is the default fallback template file when no other, more-specific template file is found:Since any one of 7 (primary) to 16 (secondary) template files (and that’s just for a publicly distributed Theme; custom-designed Themes can have any number of template files) will be used, depending on the current context, the “WordPress” method of markup separation minimizes the amount of duplicated code.
Let’s say you want to change your doctype from transitional to strict (or you want to move to HTML5), or that you want to change your copyright statement in your site footer. Using the “WordPress” method of markup separation, you make those changes in one file. Using your latter method of content separation, you make those changes seven to sixteen (or more) times.
Edit
I figured this question would come up:
We have several options:
header.php
wp_head
(orwp_enqueue_scripts
, etc.)get_header( $context )
, where$context
is based on the template hierarchy, and which will includeheader-$context.php
if found, orheader.php
if not found.But the multiple options don’t really address your underlying question regarding performance hit. Personally, I think the performance hit is negligible, because all of the query conditionals are cached as part of the query object cache, anyway. So calling them really shouldn’t incur any meaningful performance hit.
There is no contradiction, you can use both.
Example
The advantage of the WordPress system is: You are free to structure your templates as you wish.