Jump to content

$page->render() isnt generating the same output as viewing a page


ArtArmstrong
 Share

Recommended Posts

So we have a project setup that has added a module that uses the addHookAfter('save') functionality. Essentially the goal is after a we actually "Save" the page options in the backend of PW we grab specific fields we have created and do an API call to an email service which includes doing a $page->render() to grab the full html for the page we are editing and send that along as the body of that email.

The issue I am seeing when I actually view the page via the browser there are images that are showing correctly from a loop of articles but when I do the render I am only getting the path to the image like for example if the image url is:

/site/assets/files/2123/feature_car.jpg

I am only getting via the $page->render() instead of the full path:

/site/assets/files/2123/

The section that this is happening is running a loop on articles inside of the template file and is causing the error is similar to this. The problem is happening with this part of the code:

<?php echo base_url().$a->featured_image->url; ?>

Heres the overall code:

<?php
$related_articles = $pages->get(1007)->children("template=article,is_national=1,related_edition={$pages->get(1021)->related_edition},related_categories!={$pages->get(1132)},sort=-date,include=all"); 
if ($related_articles->count()) {
foreach ($related_articles as $a) {
?>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
 <td>
  <a href="<?php echo base_url().$a->url; ?>" title="Learn More: <?php echo $a->title; ?>" target="_blank"><?php echo ($a->headline != '') ? $a->getUnformatted("headline") : $a->title; ?></a>

  <?php
  $suffix = ' <a href="'.base_url().$a->url.'" target="_blank" style="text-decoration: none;" title="Learn More: '.$a->get->title.'">Learn More »</a>';
  if ($a->featured_image) { ?>
  <table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin-bottom: 10px;">
   <tr>
 <td>
  <?php if ($a->featured_image->url->description != '') { ?>
  <a href="<?php echo $a->featured_image->url->description; ?>" title="<?php echo $a->get('headline|title'); ?>"><img src="<?php echo base_url().$a->featured_image->url; ?>" alt="<?php echo $a->get('headline|title'); ?>" border="0" /></a>
  <?php } else { ?>
  <img src="<?php echo base_url().$a->featured_image->url; ?>" alt="<?php echo $a->get('headline|title'); ?>" border="0" />
  <?php } ?>
 </td>
 <td valign="top"><p style="font-family: Arial, sans-serif; font-size: 13px; color: #555555; line-height: 16px; margin-top: 0; margin-bottom: 10px;"><?php echo (trim($a->summary)) ? $a->summary.$suffix : truncate($a->body, 250, '..'.$suffix); ?></p></td>
   </tr>
  </table>
  <?php } else { ?>
  <p style="font-family: Arial, sans-serif; font-size: 13px; color: #555555; line-height: 16px; margin-top: 0; margin-bottom: 10px;"><?php echo (trim($a->summary)) ? $a->summary.$suffix : truncate($a->body, 250, '..'.$suffix); ?></p>
  <?php } ?>

 </td>
</tr>
</table>					 
<?php
}
}
?>
Link to comment
Share on other sites

Do you get any notices or errors?

Also note that I have to do this call to actually render the page and put it in a variable:

$page->setOutputFormatting(true);
$exact_target_email_body = $page->render();
$page->setOutputFormatting(false);

Are you really sure? Because this is somehow "wrong".

If you're rendering you have OF on anyway, so this code won't do anything. In cases where you want to modify and save fields you have to call $page->of(false) to turn off OF.

I can't spot anything wrong from your code and rendering a page like this works perfectly.

..$a->featured_image->url->description;...

Shouldn't it be $a->featured_image->description ?

Also some more details, anything you can add would help us.

Oh and welcome! :)

Link to comment
Share on other sites

Well if I try to comment out the setOutputFormatting calls like this:


//$page->setOutputFormatting(true);
$exact_target_email_body = $page->render();
//$page->setOutputFormatting(false);

Then it produces this error in my module when I try to save the page:

Page /emails/test-email/ may not be rendered because outputFormatting is set to false. Call $page->setOutputFormatting(true) before rendering the page.

It runs fine when I uncomment those lines and save but like mentioned above the "$exact_target_email_body" variable that saves the html for that page only has the path to where the images are stored but does not include the filename itself.

Link to comment
Share on other sites

Ahhh there's a module? ;)

If I do something like this from an autload module, it still works as it should.


public function init() {
 $this->pages->addHookAfter('save', $this, 'renderPage');
}

public function renderPage(HookEvent $event) {
 $page = $event->arguments[0];
 $file = wire("config")->paths->root . "xpage.html";
 wire("config")->urls->root = "http://pw2-dev.ch/";
 $page->of(true);
 file_put_contents($file, $page->render());
}

xpage.html saved and with correct markup.

Link to comment
Share on other sites

/site/assets/files/2123/

<?php echo base_url().$a->featured_image->url; ?>

Usually you get this as url, if there's no image uploaded. Or you're having a multiple image field and you'd have to call $a->feature_image->first()->url to the first image.

Do you get the right image src's when you call the "page" directly?

Link to comment
Share on other sites


Ahhh there's a module? ;)

If I do something like this from an autload module, it still works as it should.


public function init() {
$this->pages->addHookAfter('save', $this, 'renderPage');
}

public function renderPage($event) {
$page = wire("pages")->get(wire("input")->get('id'));
$file = wire("config")->paths->root . "xpage.html";
wire("config")->urls->root = "http://pw2-dev.ch/";
$page->of(true);
file_put_contents($file, $page->render());
}

xpage.html saved and with correct markup.

This looks like it was save the html from the render to a file which isnt exaclty what we were trying to do. I like the idea though.

Usually you get this as url, if there's no image uploaded. Or you're having a multiple image field and you'd have to call $a->feature_image->first()->url to the first image.

Do you get the right image src's when you call the "page" directly?

Well the odd thing is that when I would view the page via the browser I would get the full path to the image, but via the render it would only show the path to where the image is located but not the image filename. So it exists but wasnt showing the full thing.

As a side note we found a fix for the problem after trying about 20 different things...

The fix was to add the "setOutputFormatting" to the $a that was looping for the articles like this, not sure if it was the best way but it works:

<?php
$related_articles = $pages->get(1007)->children("template=article,is_national=1,related_edition={$pages->get(1021)->related_edition},related_categories!={$pages->get(1132)},sort=-date,include=all"); 
if ($related_articles->count()) {
foreach ($related_articles as $a) {
?>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
 <td>
  <a href="<?php echo base_url().$a->url; ?>" title="Learn More: <?php echo $a->title; ?>" target="_blank" style="text-decoration: none;"><?php echo ($a->headline != '') ? $a->getUnformatted("headline") : $a->title; ?></a>

  <?php

  // Set output formatting to true so article images work
  $a->setOutputFormatting(true);

  $suffix = ' <a href="'.base_url().$a->url.'" target="_blank" style="text-decoration: none;" title="Learn More: '.$a->get->title.'">Learn More »</a>';
  if ($a->featured_image) { ?>
  <table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin-bottom: 10px;">
   <tr>
 <td style="padding-right:10px;" valign="top">
  <?php if ($a->featured_image->url->description != '') { ?>
  <a href="<?php echo $a->featured_image->url->description; ?>" title="<?php echo $a->get('headline|title'); ?>"><img src="<?php echo base_url().$a->featured_image->url; ?>" alt="<?php echo $a->get('headline|title'); ?>" border="0" /></a>
  <?php } else { ?>
  <img src="<?php echo base_url().$a->featured_image->url; ?>" alt="<?php echo $a->get('headline|title'); ?>" border="0" />
  <?php } ?>
 </td>
 <td valign="top"><p style="font-family: Arial, sans-serif; font-size: 13px; color: #555555; line-height: 16px; margin-top: 0; margin-bottom: 10px;"><?php echo (trim($a->summary)) ? $a->summary.$suffix : truncate($a->body, 250, '..'.$suffix); ?></p></td>
   </tr>
  </table>
  <?php } else { ?>
  <p style="font-family: Arial, sans-serif; font-size: 13px; color: #555555; line-height: 16px; margin-top: 0; margin-bottom: 10px;"><?php echo (trim($a->summary)) ? $a->summary.$suffix : truncate($a->body, 250, '..'.$suffix); ?></p>
  <?php } ?>

  <?php
  // Reset the output formatting to false (original)
  $a->setOutputFormatting(false);
  ?>

 </td>
</tr>
</table>					 
<?php
}
}
?>
  • Like 1
Link to comment
Share on other sites

Your solution makes sense to me. Assuming this code is running in an environment where output formatting is OFF by default, then you'll have to turn it on, as you are. Usually you want output formatting ON when you are generating markup, and OFF in any other usage. You can also set the default output formatting state for newly loaded pages with $pages->setOutputFormatting(true|false).

Link to comment
Share on other sites

Your solution makes sense to me. Assuming this code is running in an environment where output formatting is OFF by default, then you'll have to turn it on, as you are. Usually you want output formatting ON when you are generating markup, and OFF in any other usage. You can also set the default output formatting state for newly loaded pages with $pages->setOutputFormatting(true|false).

Well inside of the module we were setting $pages->setOutputFormatting(true) before getting the variable that help the render output and then setting as false after. But just had to do the same thing inside of the template file.

Link to comment
Share on other sites

Most likely the page that had output formatting off had somehow loaded before the default output formatting status had changed. When you set $pages->setOutputFormatting(true|false); it only affects pages loaded from that point forward. It will not change the output formatting status of existing pages in memory. When you retrieve a page from $pages->get() or $pages->find() for example, it's going to see if it has a copy of it in memory already and give you that one before it attempts to load a new one. Though you can clear all pages it's caching (forcing it to load fresh copies) by calling $pages->uncacheAll(); However, I think the route that you've already taken is a better one, so wouldn't change what you've got.

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

×
×
  • Create New...