Jump to content

TemplateEngineSmarty: Render Children Pages Question


LimeWub
 Share

Recommended Posts

Hey agaaaaiiin,

So this time, I have a site which uses some of it's children as building blocks. On this site there is a "page" template (basic-page renamed) and a "block" template.

I am trying to do something along these lines in the "page.php" template:

$blocks = $page->children("template=block");
$blockHTML = '';
foreach ($blocks as $block){
	$blockHTML .= $block->render();
}
$view->set("blockHTML",$blockHTML);

 

then in "page.tpl" I am just outputting {$blockHTML}.

Block is a page in the admin with a different template (block.php and block.tpl respectively) I am expecting it to render using its own stuff.

 

UNFORRRRTUNATELYYYY... it's not working.

When I try to access my page I get errors from the template that:

Notice: Undefined index: blockHTML in /Users/blablabla/www/site/assets/cache/TemplateEngineSmarty_compile/63d505f3501d05cbbf561ef4b5267fd8258c06dc_0.file.page.tpl.php on line 

It seems like block is trying to render with Page's template (which errors as blockHTML is not set in block...??)? I have tried dying after the first iteration of the loop and still get the error, which means this is a problem with block's rendering.

Am I doing the render thing wrong?

Help pls, this is quite urgent and I just can't get my head around why and what's the problem... T>T

EDIT: Worth noting I have tried passing the .tpl file into the render function basically this way: $block->render("pathtomytemplate.tpl"). No luck.

@Wanze

 

Link to comment
Share on other sites

@LimeWub

Please post in the existing TemplateEngineSmarty thread next time.

The problem is that the global "$view" API variable provided by the factory is still associated with the page you're currently viewing, which is using "page.tpl" smarty template. Meaning if you are using $view in your block.php controller, this $view still points to "page.tpl".

You can use chunks or partial templates in this case, here's a quick possibility with chunks:

$blocks = $page->children("template=block");
$blockHTML = '';
foreach ($blocks as $block) {
    $chunk = $factory->chunk('block');
    $chunk->page = $block;
    $blockHTML .= $chunk->render();
}
$view->set('blockHTML', $blockHTML);

Note that I'm passing the block page as variable "$page" to the chunk, so I'm faking the locally scoped $page variable from ProcessWire. Usually I put my chunks into a subfolder chunks, so I don't mix them up with "regular" controllers/views.

Does it help?

Cheers

  • Like 1
Link to comment
Share on other sites

@Wanze

It does bring me closer to what I need yup. ^^

But using view->set() in the block seems to also set the things to the outter page's view...?: Yh just re-read your answer so I suppose this is expected behaviour so... erm ...ok.

Also what's the case if I want the child page to also be a page on its own? Is this not supported?

 

 

Link to comment
Share on other sites

@LimeWub

How do you mean it sets the things to the outer page view? It shouldn't do this :)

A child page can be rendered on its own, just use your block.php as controller and corresponding smarty template as view. If you need to display content from other pages, you should use chunks or partials. In your example, I would create a chunk for a block and render it as many times as you need in your page.tpl template.

Link to comment
Share on other sites

@Wanze

Not sure atm. Due to having the deadline next week we've just switched to Processwire's templating engine for this site. Once we have some time, either in the Xmas holidays or from the new years I'll try it again and let you know :o

Sry... I'll bump this again, once I have some time to breath (and test it).

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