Jump to content

$page->render($filename) confusion


interrobang
 Share

Recommended Posts

I have a setup with several templates without a templatefile. But I want to render some of the pages using these templates with $page->render($filename). I think I did not understand how the $page->render options work.

A simple example: In the default profile I just delete the "basic-page.php" file and create a new file "basic-page-alt.php" with this simple code

<?php 
// basic-page-alt.php
echo "<h1>$page->template</h1>";
and I modify the home.php file to
<?php 
// home.php
include("./head.inc"); 
echo $pages->get('/about')->render("basic-page-alt.php");
include("./foot.inc"); 
When I try to view the homepage I get an error

Error: Exception: Template file does not exist: '/XXX/Sites/pw_test/site/templates/basic-page.php' (in /Users/XXX/pw_test/wire/core/TemplateFile.php line 67)

When I put a empty "basic-page.php" back in my templates folder I get the expected result. I am using the latest dev from github. Is this the intended behaviour?

  • Like 1
Link to comment
Share on other sites

Thanks for the answer, but all my templatefiles are in "site/templates/"

This way it works without having a "basic-page.php" file:

include("./head.inc");
$p = $pages->get('/about');
$p->template->filename = $config->paths->templates . "basic-page-alt.php";
echo $p->render();
include("./foot.inc");
Shouldn't this be exactly the same as echo $pages->get('/about')->render("basic-page-alt.php") ?
Link to comment
Share on other sites

Hi interrobang,

I cannot really follow what you are try to do - or I cannot find the specs for that the option can be a template filename:

   https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/modules/PageRender.module#L244

   http://cheatsheet.processwire.com/ $page->render() also doesn't show anything, but it could be that there is missing newer stuff because ist seems to stick with the 2.3 stable.

From where do you get that with the template filename?

Link to comment
Share on other sites

I figured this also. There seems to be a difference. Set filename then render and render then specify template file.

Calling with render with that option seems to try see if its viewable and It's not. Thus the error... there's not file.. and attaching your template would come after that. Only my guess without study code.

  • Like 3
Link to comment
Share on other sites

When I put a empty "basic-page.php" back in my templates folder I get the expected result. I am using the latest dev from github. Is this the intended behaviour? 

This is not intended behavior. Good call. It's happening because the TemplateFile class is checking if the file it's given exists from the constructor rather than from the render() method. The PageRender module gets a copy of the TemplateFile instance from the Page, and then changes the filename if you've given if one. As a result I think I can solve this just by moving the "file does not exist" exception out of the TemplateFile constructor and to the render() method. I've just made the change, so it should appear in the next batch of commits to dev.

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