Jump to content
saleo

Markup regions template strategy. Is this normal?

Recommended Posts

Hello, I'm using the Markup regions template strategy https://processwire.com/docs/front-end/output/markup-regions/

I noticed that any code at the beginning of the source file is executed twice. For example

<?php namespace ProcessWire;

echo "TEST<br>"
?>
<main id="main" pw-append>
    
</main>

will output the word "TEST" twice.

Is this normal behavior? Maybe @ryan can shed some light?

Thank you

Leo

Share this post


Link to post
Share on other sites

Hello,

8 hours ago, saleo said:

Is this normal behavior?

No, it's not. Probably something else is behind the cause of this. I do not think simply using Markup Regions should result in such a behavior.

Share this post


Link to post
Share on other sites

I don't know the exact reason why your "TEST<br>" line is output twice, but what you're doing there is not correct usage of Markup Regions.

The docs explain that when using Markup Regions you set...

$config->appendTemplateFile = '_main.php';

...so that every template file will append the markup contained in _main.php. And...

Quote

 This _main.php file is where your main document HTML markup will be contained, from <!DOCTYPE html> to </html> and everything in between, which is where regions will be defined.

This means that all the markup that is in a template file will be output before the <html> tag that is in _main.php. Of course that would normally be invalid HTML because no markup should occur before the opening <html> tag. But as the docs explain...

Quote

How it works (technical detail)

  • Any markup output before the <html> tag (beginning of an HTML document) is considered to contain region actions – tags that will populate or modify regions.

  • Any markup output after the <html> tag is considered to contain region definitions – tags that can be populated and/or modified by the region actions.

So all the markup in a template file should correspond to a matching region that is defined in _main.php, so that when Markup Regions parses the output it can relocate the markup in the template file into the corresponding regions defined in _main.php. If you have markup in a template file that does not correspond to a region in _main.php then Markup Regions is not going to be able to make sense of it, and that is what's happening with your "TEST<br>" line.

To confirm you can enable debugging for Markup Regions and it will alert you to problems: https://processwire.com/docs/front-end/output/markup-regions/#debugging-regions

2020-10-01_101509.png.87ef1893e58be54237536161cd0756c1.png

  • Like 1

Share this post


Link to post
Share on other sites

You're right, but I only used the echo code to make the problem more obvious. In fact, initially I noticed the problem when I saw that a call to Aws was executed twice.
So I'm wondering if it is possible to insert any code in the opening section of the template file, without it being executed twice?

Share this post


Link to post
Share on other sites
On 10/1/2020 at 2:55 PM, saleo said:

So I'm wondering if it is possible to insert any code in the opening section of the template file, without it being executed twice?

As @Robin S explained, there is no reason to echo anything in a context like that. If you just put any other PHP code there, that will not be executed twice (at least it should not do that for sure). Only echo() is what gets executed twice but it should not even happen once, meaning not calling echo() is the "solution". I have sites with various code being executed in that context, none of them runs twice.

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...