Jump to content

$page->render() for list of child pages in delegate template approach


gebeer
 Share

Recommended Posts

Hello,

I'm using PW 2.4.11 with its delegate template approach.

I have a page "places" with template places.php to list all its sub pages

<?php
//template places 
foreach ($page->children as $child) {
	$content .= $child->render();
};
?>

All sub-pages of "places" have the place.php template

<?php
//template place 

$tags = '';
foreach ($page->tags as $tag) {
	$tags .= "<li><a href='#'>{$tag->title}</a></li>\n";
}

$content = "<article class='article'>\n";
$content .= "<ul class='nav nav-pills'>\n$tags</ul>\n";
$content .= "<h2>{$page->title}</h2>\n";
$content .= "{$page->body}";
$content .= "</article>";
?>

Problem is that for every sub page the whole _main.php gets rendered.

I guess this happens because of

$useMain = true; 

in _init.php.

How can I tell the place.php template to only include _main.php  when it is not called through the render() method?

If I add

$useMain = false;

to place.php, I get a blank page.

I'm sure the solution must be really simple. But for me not being a coder, I just can't seem to get my head around it.

Link to comment
Share on other sites

<?php
//template places 
foreach ($page->children as $child) {
    $child->rendered = true;
    $content .= $child->render();
};
?>


<?php
// on the child template

if ($this->rendered) { // not sure, could work or else take it from $page->rendered
    echo "<div>markup you need</div>"
    return; // return, end rendering & give control back to processwire
}

?>
<!doctype html> 
<html>
<head>
... etc

Can't test this right now... sorry

Link to comment
Share on other sites

Thanks for the quick reply. I adapted my code following your suggestions.

My places.php

<?php
//template places 

foreach ($page->children as $child) {
	$child->rendered = true;
	$content .= $child->render();
};
?>

and place.php

<?php
//template place
$tags = '';
foreach ($page->tags as $tag) {
    $tags .= "<li><a href='#'>{$tag->title}</a></li>\n";
}
if ($page->rendered) {
    echo "<article class='article'>\n";
    echo "<ul class='nav nav-pills'>\n$tags</ul>\n";
    echo "<h2>{$page->title}</h2>\n";
    echo "{$page->body}";
    echo "</article>";
    return;
} else {
    $content = "<article class='article'>\n";
    $content .= "<ul class='nav nav-pills'>\n$tags</ul>\n";
    $content .= "<h2>{$page->title}</h2>\n";
    $content .= "{$page->body}";
    $content .= "</article>";
}

?>

But still the same result. For each child page the whole _main.php gets rendered.

I also tried in places.php

foreach ($page->children as $child) {
	$content .= $child->render($useMain = false);
};

with my original code in places.php

But this gives a blank page again.

Link to comment
Share on other sites

@adrian

Thanks for pointing that out.

When I pass it like

$child->render(array("useMain" => false))

and then check in the child template

$useMain = ($options['useMain'] === false) ? false : true;

I get the same results as passing it like

$child->render($useMain = false)

Which renders a blank page.

So the problem is not passing the value for $useMain in the right manner.

Rather it is the delegate approach that I'm struggling with.

My places.php is calling the child template (place.php) multiple times for rendering each child page. Everytime the child page template is being called for rendering, _main.php gets included. Result is multiple rendering of the complete html from _main.php.

Now when I set $useMain to false for the child page template I'd expect to just get the markup from that template. But instead I get an empty string. That's what is confusing me.

Link to comment
Share on other sites

  • 1 year later...

I need to revisit this thread because the problem has given me a headache agein today.

It seemed like $page->render($options) cannot be used with the delegate approach where you assign your content to a variable in your template file and _main.php gets included and renders that variable.

Here my setup to test this behaviour:

basic_page.php

<?php

$gallery = $page->children()->first();

content = $gallery->render(array("useMain" => false));

$gallery has template gallery.php

<?php

if (isset($options['useMain'])) $useMain = $options['useMain'];

$content = "<h2 class='page-header'>{$page->title}</h2>
{$page->bodycopy}";

With this setup "$gallery->render(array("useMain" => false));" returns an empty string.

if I change gallery.php to

<?php

if (isset($options['useMain'])) $useMain = $options['useMain'];

// NOTE THE echo instead of $content =
echo "<h2 class='page-header'>gbr{$page->title}</h2>
{$page->bodycopy}";

Then I get the string that I would expect from  "$gallery->render(array("useMain" => false));" 

So the culprit seems to be the $page->render($options) method not returning my $content variable from the gallery.php template.

The solution is dead simple, though. gallery.php:

<?php

if (isset($options['useMain'])) $useMain = $options['useMain'];

$content = "<h2 class='page-header'>{$page->title}</h2>
{$page->bodycopy}";

if (!$useMain) echo $content;

O0

Link to comment
Share on other sites

  • 1 year later...

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