Jump to content

Rendering *two* pages from another app


giles_v
 Share

Recommended Posts

So as per https://twitter.com/...397667160604672 I wanted to set up PW to manage content inside another web app and I'm doing something similar:

require(__DIR__ . '/index.php'); // this is the ProcessWire index file for bootstrap

echo $wire->pages->get('/videos')->setOutputFormatting(true)->render();
echo "\n\n\n\n\n==============================\n\n\n\n\n\n";
echo $wire->pages->get('/people')->setOutputFormatting(true)->render();

The problem here is that the first page (videos) renders fine, but the second one (people) does not. Specifically, $page seems to be undefined in the template for "people".

If I reverse the order (first render "people", then "videos") then again, the first one renders fine, but the second does not. So I get "people" correctly, but "videos" is stunted because $page is NULL in the template.

Any ideas?

  • Like 1
Link to comment
Share on other sites

DOn't know why it is like this, but it seems to have to do with PW getting the previous page, as if using it in a template. In your case there's no "page".

I can get it to work when I call $wire->setFuel("page",$p1); after first page rendered.

echo $p1 = $wire->pages->get('/videos')->setOutputFormatting(true)->render();
$wire->setFuel("page",$p1);
echo $wire->pages->get('/people')->setOutputFormatting(true)->render();
  • Like 1
Link to comment
Share on other sites

Did some investigating here. If I'm right, it's not a bug per se, but an undesirable behavior. Try changing your code to this:

require('./index.php'); // this is the ProcessWire index file for bootstrap

$wire->pages->setOutputFormatting(false);
echo $wire->pages->get('/videos/')->render();
echo "\n\n\n\n\n==============================\n\n\n\n\n\n";
echo $wire->pages->get('/people/')->render();

That fixed it in my case. The reason was that calling setOutputFormatting() on a page only sets the outputFormatting for that page. If your page's template happens to be loading other pages, they will be loaded with outputFormatting off. By setting the default output formatting state before you render(), you correct that issue. This is only the case when you are rendering from a bootstrapped script, because that default outputFormatting state is pre-set to false. In any other situation, you wouldn't have to do this.

I think I can make this a lot simpler. I'm just going to update $page->render(); to take care of setting the right outputFormatting state for itself and the system before it attempts to render a page.

If that change didn't fix your case, then go in and check the template(s) for the pages that are being rendered. Do you have any include() statements that should be changed to include_once()? This would be the case if you had any function definitions in include files. If you have any function definitions in your main template file, then you'd want to do this instead:

if(!function_exists("my_function")): 
 function my_function() {
   // do something
 }
endif; 

Since your two page render() calls are happening in the same PHP runtime instance, you just have to watch out for any duplicate definitions or includes. Let me know if any of this solves it there?

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