processwire / processwire-issues

ProcessWire issue reports.
44 stars 2 forks source link

Render fields doesn't work as expected when run outside of PW templates #1105

Open dragan1700 opened 4 years ago

dragan1700 commented 4 years ago

Short description of the issue

The call to $child->render('offer_matrix'); is not resulting in rendering of a field, but rather a page - it's looking for "offer_matrix" in site/templates/, instead of "offer_matrix.php" in site/templates/fields/. This works as expected in a regular PW template, but it doesn't when run inside Tracy Debugger console (and perhaps also when PW is bootstrapped - but I did not test that at this point).

$myOffer = 11404;
$myOfferPage = $pages->get($myOffer);
// using absolute path doesn't work:
$str = wireRenderFile("/home/domainname/www/dev.mysite/site/templates/offer-index-dev.php", array("offer_index_page_id" => $myOffer));
// using relative path doesn't work either: (default is site/templates/ - according to docs)
$str = wireRenderFile("offer-index-dev.php", array("offer_index_page_id" => $myOffer));
// using relative path without .php suffix doesn't work either: (according to docs .php suffix can be omitted)
$str = wireRenderFile("offer-index-dev", array("offer_index_page_id" => $myOffer));

// $str is then used to generate a PDF and save it to a page - but the error appears above this
// fatal error msg: * Exception: Invalid output file location or specified file does not exist. on line: 518 in /home/mydomain/www/dev.mysite/wire/modules/PageRender.module

inside offer-index-dev.php, the relevant place where the error is triggered:

foreach($offer_index->children as $child) {
    echo $child->render('offer_matrix'); // that's where PW gives up and terminates
    // echo $child->render('/home/mydomain/www/dev.mysite/site/templates/fields/offer_matrix.php', $vars = ["pid" => $child->id]); // I've tried again with abs. path here
    // echo $child->render('/home/mydomain/www/dev.mysite/site/templates/fields/offer_matrix'); // ... and abs. path w/o .php suffix here
}

Expected behavior

I would expect that PW renders the field templates like it does when run from inside a "normal" PW template. Especially in the case where I use the absolute path, incl. file-suffix.

Actual behavior

Exception: Invalid output file location or specified file does not exist. on line: 518 in /home/mydomain/www/dev.mysite/wire/modules/PageRender.module

The call to $child->render('offer_matrix'); is not resulting in rendering of a field, but rather a page - it's looking for "offer_matrix" in site/templates/, instead of "offer_matrix.php" in site/templates/fields/.

Steps to reproduce the issue

  1. Create field templates, as described here https://processwire.com/blog/posts/processwire-3.0.7-expands-field-rendering-page-path-history-and-more/#field-rendering-with-template-files
  2. Create a page that uses these fields.
  3. In the Tracy Debugger console, use some similar code as posted above in the short description.
  4. Try to get a value back when you run wireRenderFile()
  5. Use the same code to output fields inside a regular PW template - you should see it there working just fine.

Setup/Environment

adrianbj commented 4 years ago

@ryancramerdesign - even though this issue seems to be due to it trying to render the template, rather than the field, I wonder if this issue is also at least a bit related to https://github.com/processwire/processwire-issues/issues/511

It would be great if you could also test that again while you are looking into this one because so many of us are using Tracy's Console panel for code testing and this is the one area where there seems to be problems.

FYI - this (https://github.com/adrianbj/TracyDebugger/blob/7d1209394d34c00991296159187754a0b6bd9ce5/includes/CodeProcessor.php#L120-L153) is where the Console Panel instantiates a new template file and renders it.