1

I know a bit about PHP, but never studied in depth, but now I will develop some real sites.

One doubt I have is about writing the common HTML elements like Doctype, head and fixed layout like menus or top. I always created a class called Layout to write these parts of the page, and so, in every page, I call the methods of Layout class to build the layout and basic HTML elements.

My question is if there is another way to do this without the need to call the Layout methods in every page, or maybe, no need for a Layout class. Something like a pattern file with the basic HTML for every page or something like that.

So, something like this exists or is needed to put calls in my code to generate the basic HTML content?

Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199
  • 1
    take a look on how Zend_Layout works. – Rufinus Apr 12 '12 at 16:46
  • 1
    [Symfony2 framework](http://symfony.com/) provides a [template inheritance approach](http://symfony.com/doc/current/book/templating.html#template-inheritance-and-layouts) for PHP templates. It works in the same way as a child class overriding parent methods. – noisebleed Apr 12 '12 at 16:58

1 Answers1

3

You could try to use a template engine like Twig or Smarty. In the other case if want keep it simple, you can make a file e.g. posts.php and include partials like header.php or footer.php which contains its data

<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta charset="utf-8">
        <title>HEY</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <?php include_once('header.php'); ?>

        <?php $posts = ["a", "b", "c"]; ?>
        <?php foreach ($posts as $post): ?>
            <div><?php echo $post; ?></div>
        <?php endforeach; ?>

        <?php include_once('footer.php'); ?>
    </body>
</html>
message
  • 4,513
  • 2
  • 28
  • 39