Jump to content

Markup region inside a Repeater or Repeater Matrix field


lpa
 Share

Recommended Posts

I have problems putting a Markup Region action inside a Repeater Matrix field template. I am trying to get a region be appended in a region defined in <head> like this:

_main.php head has this:

<head>
  ....
<pw-region id="mainhead">
</pw-region>
  ....
</head>

And my Repeater matrix field has this:

<div id="mainhead" pw-append>
	<?php echo $form->styles; ?>
	<?php echo $form->scripts; ?>
</div>

The problem is that the contents of the matrix field action region is not put in the <head> of the page but in place it is in the Repeater Matrix template. 

Is this supposed to work?

Link to comment
Share on other sites

  • 2 weeks later...

@lpa I think the issue is that your definition is <pw-region> and your insertion is a <div>. But I also think there are very few cases where it's worthwhile to use <region> or <pw-region>, especially when you've already got markup tags present that you can use. Try this instead for your <head> definition:

<head id="html-head">
  <!-- existing head contents here -->
</head>

and then use this where you want to append the scripts:

<head id="html-head" pw-append>
  <?=$form->styles . $form->scripts?>
</head>

 

Link to comment
Share on other sites

Thanks @ryan!

So the region has to be the same tag as the insertion?  

I wanted to create a pw-region inside <head> to know exactly where the scripts are inserted. How should I then insert the scripts in the pw-region -tag? I don't understand what would then be the correct way to use the <pw-region> or <region> tag that is described in the documentation here: https://processwire.com/docs/front-end/output/markup-regions/

The main question in my situation is how should I insert the JavaScript and CSS links from a RepeaterMatrix field in to the <head> and in a correct position. 

Link to comment
Share on other sites

On 11/22/2023 at 8:27 AM, lpa said:

And my Repeater matrix field has this:

<div id="mainhead" pw-append>
	<?php echo $form->styles; ?>
	<?php echo $form->scripts; ?>
</div>

The matrix field is likely to be rendered inside a markup region, and so this output would end up as a nested markup region, which isn't supported.

See this response to a similar question for how a FilenameArray or WireArray can be a solution:

 

Link to comment
Share on other sites

  • 2 weeks later...

Since you are already using markup regions, I would offload all scripts and styles to an array which gets filled along the way by all templates and then in the end, output in the main file.

You can - anywhere in the code - add something like this:

$config->styles->add($config->urls->templates . "path/to/file.css");

or for scripts, this:

$config->scripts->add($config->urls->templates . "path/to/script.js");

And then in your _main.php file, e.g. in the header, do this:

<?php foreach ($config->styles as $file) {
    echo "<link type=\"text/css\" href=\"$file\" rel=\"stylesheet\">";
} ?>

and for scripts, e.g. before the end body tag, this:

<?php foreach ($config->scripts as $file) {
    echo "<script type=\"text/javascript\" src=\"$file\"></script>";
} ?>

I am using this every time and the solution is very flexible (I think I copied this from the Admin Theme files back in the day).

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...