Jump to content

Output region and field template


M1ss1on
 Share

Recommended Posts

Hi,

Is there any solution to making output regions work with field templates?

I've inserted this at the end of "/site/templates/fields/images.php" file. What I'd like to achieve is to insert the js module only on field render (to optimize the js...)

?>
<script pw-append="modules" type="module" src="/site/templates/scripts/gallery.js" defer></script>

of course i have the region defined in the "_main.php"

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

and it works from regular templates, just not from field templates... because they're just "compiled" in a different way.

Thank you!

  • Like 1
Link to comment
Share on other sites

I had the desire to do this too, but also ran into this issue. For me, my current field templates are primarily for fields that are used in every template, so I've instead just included the JS by default, even if the fields aren't rendered (thankfully there's not a lot of JavaScript).

As a workaround you could, in the page templates that take advantage of these fields, test whether the fields are in use, and if so then determine whether to render the JS via your region (from the page template itself, instead of the field template). It's a bit of extra work, but should be doable this way instead.

Link to comment
Share on other sites

Yes, exactly my course of action... I just wanted to ask if there's a creative solution, although I went through the documentation and API.

It's a shame it would be such a clean implementation... just render the field and everything you need is included without additional checks and possible mishaps.

Link to comment
Share on other sites

I don't think there is anything about field templates that means they can't include a markup region. But you can't have nested markup regions (where your template output includes one markup region inside another markup region). So if you are using a markup region inside a field template you would not be able to render that field template inside another markup region, which is something you would often want to do.

I think a better solution is to use a FilenameArray and add whatever JS files you need to it in your field template.

In /site/templates/_init.php

$config->siteJs = new FilenameArray();

In /site/templates/_main.php

<?php foreach($config->siteJs as $jsFile): ?>
	<script src="<?= $jsFile ?>"></script>
<?php endforeach; ?>

In your field template

$config->siteJs->add('/site/templates/js/myField.js');

Or you can follow the same general idea using an ordinary WireArray and include the whole "<script src..." string so you can optionally use "defer" or other attributes.

  • Like 1
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...