Jump to content

Are Markup Regions and Field Templates compatible?


Lance O.
 Share

Recommended Posts

I'm having issues with using markup regions and including field templates. I'm using a PageTable field to add "stacks" to a page.

For example, I'm including this in my _main.php file:

echo "<main id='main' data-pw-id='content'></main>";

And this in my home.php template:

echo "<div data-pw-id='content'>";
echo $page->render('stacks');
echo "</div>";

My fields/stacks.php field template looks like this:

<?php namespace ProcessWire;
if (wireCount($value)) {
    echo "<div class='stacks'>";
        foreach ($value as $stack) {
            if (!$stack->isHidden()) {
                echo $stack->render("fields/stacks/{$stack->template}.php");
            }
        }
    echo "</div>"; // stacks
}

Then the code for each stack is located in separate field template files like this:

fields/stacks/stack-intro.php
fields/stacks/stack-events.php
fields/stacks/stack-spread.php
fields/stacks/stack-promo.php

Unfortunately this approach is not rendering the stack field templates. I'm wondering if the 

echo $stack->render("fields/stacks/{$stack->template}.php");

line is causing issues? When I comment that line out and just replace it with a simple echo, the markup region works correctly.

Debugging shows the following errors, although I'm not sure how to interpret this:

image.png.dfe9e56ada7e1e126110daae0e34a02d.png

 

 

Link to comment
Share on other sites

I think that since $stack is a Page the valid parameter for render() is a field name and not a path

You might be better of doing something like this:

<?php namespace ProcessWire;
if (wireCount($value)) {
    echo "<div class='stacks'>";
        foreach ($value as $stack) {
            if (!$stack->isHidden()) {
                echo wireRenderFile("fields/stacks/{$stack->template}", ['stack' => $stack]);
            }
        }
    echo "</div>"; // stacks
}

Just adapt the array, passes as a second parameter to wireRenderFile to be named according to the variables in "stack-{$template}.php"

  • Like 3
  • Thanks 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...