lpa Posted November 21, 2023 Posted November 21, 2023 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?
lpa Posted November 23, 2023 Author Posted November 23, 2023 Is there anybody who can explain me why this does not work? Or was my question unclear?
monollonom Posted November 23, 2023 Posted November 23, 2023 Shouldn’t you write <pw-region id="mainhead" instead of <div id="mainhead" in your code? 1
ryan Posted December 3, 2023 Posted December 3, 2023 @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>
lpa Posted December 3, 2023 Author Posted December 3, 2023 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.
Robin S Posted December 3, 2023 Posted December 3, 2023 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:
poljpocket Posted December 12, 2023 Posted December 12, 2023 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).
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now