I have built websites for a while, came up with a design and made a bunch of pages. I've also done the index.php page then including all the content via ajax to save having dozens of individual pages.

Now, is there such as thing as a template - where I can have a contact page and when loaded it's just embedded into the template, so when I change the design is changes the whole site?

or does this approach rely on includes around the main content?

    There might be several ways to handle this. One is CSS ... if you have everything classed or IDed in your HTML and no inline "style" attributes, a theme changer could call different CSS sheets to change styles.

    The vBulletin software we used to have here used a PHP script to output CSS, and the CSS was all stored in the DB with a "styleid" variable. Changing "styleid" via a selector in your control panel would get you different colors and fonts on the site.

    Finally, keeping the "business logic" separate from the display logic and templates is a well-known concept in programming. It's just sometimes a little difficult to achieve this in practice because most typically a PHP project isn't Waterfalled, but grows organically from some little nugget which happens to include both PHP and HTML ... although it doesn't have to be that way.

      You're kind of discussing several different things at once, I think. There's obviously more than one way to build a site - you talk about using AJAX from index.php; you can also use rewrite rules from an index.php front-loader to dynamically route to a back-end controller, for example. In my experience, most modern web-based coding goes this route - Laravel, Symfony, Drupal, WordPress, CodeIgniter, etc... (I've not used Cake, but from what I've read it's the same paradigm). Now, on top of that you're talking about changing the entire site design easily - this can be done via CSS as dalecosp described, or it can be done using modular site template files, which is what I think you're actually asking about here.

      Given that assumption, there are a ton of different templating solutions out there - personally, I prefer Twig, but there's also Blade, Mustache, heck - even Smarty is apparently still kicking. Run a Google search for PHP template engines and you can spend six days reading non-stop. And finally, you can just create plain HTML files with injected PHP code.

      At any rate, the basic premise behind template engines is that you'll create one or two "base" or "parent" templates - typically these include the outer-most structural markup and include directives to other files, called "partials". Partials are HTML/template code bits and pieces that serve a specific purpose. For instance, you can have a parent page that lays out the open/close HTML, head, body, and footer tags. Within the head, it'll include a partial that actually lays out all the head meta-data. The body partial is dynamic and controlled by the framework depending on what page the user is on, or what data has been presented and processed by the system - this can also include other partial files. The footer contains any additional code - JavaScript includes, coda, etc.

      The joy of doing things this way is that you can change one file - the base, or parent, template - and that change happens on every page in the site because they all extend from that base file. Or you can change a more specific file and that change will be scoped to the pages that use or include that partial.

      At my last job, we worked a lot with WordPress. The WordPress way of "templating" is to create HTML files with PHP injected directly, and it's dumb. I hate it. So, I found a plugin that let us use Twig and I built a theme bootstrap. My personalized version of it is here. Take a look in the /wp-content/themes/theme-folder/views directory for Twig-based template files and how it's all laid out. You can also take a look at the /wp-content/themes/theme-folder/includes/Functions.php file for an idea of how the data gets from the WordPress database to the template files for output.

      Well that was much longer than intended, and hopefully it makes some sort of sense!

      Write a Reply...