Jump to content

The markup regions strategy has me completely confused.


Boost
 Share

Recommended Posts

Hi,

This is kind of a follow up from my previous question at 

I still can't connect the dots after reading all of the documents. For example, I have a simple website. I have the homepage, the about us, and the services pages. So, the markup regions are usually placed on /site/templates/_main.php, right? But I have three very different pages(home,about and services). Does it mean that I have to create three different templates Like /site/templates/_home.php, /site/templates/_about.php and /site/templates/_services.php? If so, why don't you just do direct output?

Any help in wrapping my head around it would be greatly appreciated.

Thanks.

 

 

Link to comment
Share on other sites

Its a matter of taste and organization. You can have 3 pages and only one template. If services and about was using the same template basic-page, you end with only one basic-page.php template to work with. 
 

The strategy used and the templates are two distinct things. Add Pages to it, and its 3 differents things.

 

  • Like 1
Link to comment
Share on other sites

Markup regions can keep your template files cleaner.

Let's say for instance you have common header and footer section for all your pages.  Without markup regions you might abstract these to include files and then include them in all your templates, for example:

<head>
    [your template specific head content]
</head>
<body>
    <?php include($config->paths->templates . "_header.php"); ?>

    [Your template specific page content here]

	<?php include($config->paths->templates . "_footer.php"); ?>
</body>

Now this is fine if you only have 3 templates, but if your site grows bigger and more complex with possibly more include logic, that's where markup regions become of more value.

In _main.php, I often have an empty

<region id="pw-styles"> </region>

just before my closing </head> tag, and a

<region id="pw-scripts"> </region>

before my closing </body> tag.  

Where I have a need for template-specific stylesheets and scripts (e.g for a lightgallery), I can just add these regions into those templates and put the necessary code in there.You can leave these entire region tags out of most templates, then just add them to the 'lightgallery' template, keeping your other templates clean.

Over time you might find the need to add additional common code to all your templates.  If your site has grown, this could mean updating a fair number templates with that code (or another include).

With markup regions, you can just add this new code to _main.php and it's immediately included in all templates using _main.php. Meanwhile your template files remain clean with just their specific content,

e.g. a basic page template might just be as simple as this:

<region id="pw-content">

    [your template specific page content here]    

</region>

<?php include($config->paths->templates . "_main.php"); ?>

And your _main.php would look like this:

<head>
    [your common head content]
    <region id="pw-styles">
        
    </region>
</head>
<?php include($config->paths->templates . "_header.php"); ?>
<body>

<region id="pw-main">
    
</region>

<?php include($config->paths->templates . "_footer.php"); ?>
<region id="pw-scripts">
    
</region>
</body>

Wheareas a template with lightgallery might look like this:

<region id="pw-styles">
    <link rel="stylesheet" href="/css/lightgallery.css">
</region>

<region id="pw-content">
    [your template specific page content here]    
</region>

<region id="pw-scripts">
    <script src="/js/lightgallery.js"></script>
</region>

<?php include($config->paths->templates . "_main.php"); ?>

Whether that makes things clearer for you I don't know ?, but if you only have 3 templates for your entire site then using markup regions may be overkill.  

  • Like 3
Link to comment
Share on other sites

@Boost

Quote

So, the markup regions are usually placed on /site/templates/_main.php, right? Does it mean that I have to create three different templates Like /site/templates/_home.php, /site/templates/_about.php and /site/templates/_services.php?

Typically your _main.php would contain just the regions that would be common among all templates. Usually this is desirable because most websites have a common theme behind them. But if your template files are so different that nothing in <body>...</body> is the same, then maybe your _main.php is just this:

<html lang="en">
  <head id="html-head">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?=$page->title?></title>
    <!-- plus any other common head output -->
  </head>
  <body id="html-body">
    <!-- any common body stuff, or even blank if none -->
  </body>
</html>

Then template file "basic-page.php" might look like this:

<body id="html-body">
  <div class="some-container-class">
    <h1><?=$page->title?></h1>
    <?=$page->body?>
  </div>
</body>

and template file "gallery.php" might look like this:

<body id="html-body">
  <h1>Photo gallery: <?=$page->title?></h1>
  <ul class="gallery">
    <?php foreach($page->images as $image): ?>
      <li><img src="<?=$image->url?>" alt="<?=$image->description?>" />
    <?php endforeach; ?>
  </ul>
</body>

<!-- example of adding some JS and CSS files to the document head -->
<script src="<?=$urls->templates?>scripts/gallery.js" pw-append="html-head"></script>
<link rel="stylesheet" href="<?=$urls()->templates?>styles/gallery.css" pw-append="html-head">

<!-- or this does the same as the above with different syntax -->
<head id="html-head" pw-append>
  <script src="<?=$urls->templates?>scripts/gallery.js"></script>
  <link rel="stylesheet" href="<?=$urls()->templates?>styles/gallery.css">
</head>

 

Quote

Does it mean that I have to create three different templates Like /site/templates/_home.php, /site/templates/_about.php and /site/templates/_services.php? If so, why don't you just do direct output?

I personally wouldn't create separate main template files like that and instead would just use a simple enough _main.php to accommodate anything I want to do with it. 

You certainly could use direct output too, and I'm guessing maybe half of PW sites do this. But consider the case above where we needed to add script/link tags to the document <head> from our template file. This is where direct output becomes more limiting, unless you want to start adding template-specific logic in other include files, like whichever one outputs your <head>. Either that, or you end up needing your template file(s) to each output an entire HTML document. So markup regions are really convenient in part because you can output directly (like direct output), but can also  manipulate markup that's already been output, or will be output later. 

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

In my example, all pages have the same header and footer but a completely different body. So, I believe a single _main.php file is sufficient. Thank you very much!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...