Jump to content

render() and global variables


vanderbreye
 Share

Recommended Posts

Is it somehow possible to let page->render() use global variables?

I want to render subpages in a template, which works fine for some cases.

The problem:

In the template there is an include, depending on a variable, which is set in the header.

in the first head/ first template:

$mobile = true;

foreach($landingpage->children as $child) {
echo $pages->get($child->id)->render();
}

and in the template:

 
include_once("./header.php");
if ($isMobile) {include("./mobile.inc");}

but page->render() will (of course) not recognize the unknown $mobile variable.

is there a way to render the page with all its variables?

Best,
Jan

 
 
Link to comment
Share on other sites

  • 3 years later...

I am trying to get creative and store both HTML and text email template as a page on PW rather than a regular PHP file.

My code to render the html

 $html = $pages->get(6090)->renderField('html_body', '', array('message' => $reasons, 'stripe_id' => $stripeId));

Where html_body is a textarea field with my HTML email template. I am trying to pass the two variables using

<?php echo $options['message']; ?> <?php echo $options['stripe_id'] ?>

HTML renders fine but it didn't render the variables. 

My question is, did I miss anything? Is this doable or do I have to save template as a file?

Thx

Edited by Rudy
Edited code showing 3 arguments
Link to comment
Share on other sites

It doesn't work because renderField() method doesn't support additional data to be passed. 

You can render the file manually using $files->render() and by populating $data argument. Also remember to populate 'value', 'field' and 'page' if you're utilizing one of them in the field template. 

public function ___renderField($fieldName, $file = '', $value = null) {...} 
 
Link to comment
Share on other sites

9 hours ago, abdus said:

It doesn't work because renderField() method doesn't support additional data to be passed. 

You can render the file manually using $files->render() and by populating $data argument. Also remember to populate 'value', 'field' and 'page' if you're utilizing one of them in the field template. 

public function ___renderField($fieldName, $file = '', $value = null) {...} 
 

Hmm, according to the documentation https://processwire.com/api/ref/page/render-field/  it states that $page->renderField takes $value as third argument.

// usage with all arguments
$mixed = $page->renderField(string $fieldName, string $file = '', $value = null);

But in reality, I haven't been able to pass data to the rendered output.

Link to comment
Share on other sites

In your code

 $html = $pages->get(6090)->renderField('html_body', array('message' => $reasons, 'stripe_id' => $stripeId));

your option value is the second argument, shouldn't be the third ?

$html = $pages->get(6090)->renderField('html_body', 'test', array('message' => $reasons, 'stripe_id' => $stripeId)); // given there is a 'test' markup file

 

and you grab values with :  $value['message'] ... not $options.

<?php echo $value['message']; ?> <?php echo $value['stripe_id'] ?>

 

Edited by flydev
code typo
Link to comment
Share on other sites

22 minutes ago, Rudy said:

$page->renderField takes $value

The $value is to use any value as if it was $page->$fieldName value, it's not $data as in render() method

public function renderField(Page $page, $fieldName, $file = '', $value = null) {
		// ..
	
		if(is_null($value) && $fieldName) $value = $page->getFormatted($fieldName);
		if(is_null($value)) return '';
		// ...
		$tpl = $this->wire(new TemplateFile($renderFile));
		$tpl->set('page', $page);
		$tpl->set('value', $value);
		$tpl->set('field', $field);
		
		return $tpl->render();
}

image.thumb.png.f2f94c690d33efea25f5780149764a5f.png

Link to comment
Share on other sites

I think you can do this with your own function with a str_replace inside and passing the field string and the values array like in this post from @Macrura 

https://processwire.com/talk/topic/12969-intermediate-template-structure-without-string-concatenation/?do=findComment&comment=117616

in your case the variable $tpl is filled with the content of your field instead of file_get_contents

  • Like 1
Link to comment
Share on other sites

58 minutes ago, Pixrael said:

I think you can do this with your own function with a str_replace inside and passing the field string and the values array like in this post from @Macrura 

https://processwire.com/talk/topic/12969-intermediate-template-structure-without-string-concatenation/?do=findComment&comment=117616

in your case the variable $tpl is filled with the content of your field instead of file_get_contents

I am going to try this approach and report back. Thx!

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