Jump to content

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 post
Share on other sites

You can pass variable via render like:

echo $pages->get($child->id)->render(array('mobile' => $mobile));

and then in the template use $options['mobile'] to get the value. 

  • Like 5
Link to post
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 post
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 post
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 post
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 post
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 post
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 post
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 post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By baronmunchowsen
      I'm trying to get the rendered output of a page in a custom module.
      In my custom Process module, I have the following:
      public function ___execute() { $page = $this->pages->get(1); $page->of(true); $op = $page->render(); return "Hello World"; } An exception is thrown:
      The srcset functionality is used liberally to generate the desired output of a page.
      Any suggestions on what I can do to avoid the srcset error?
      Edit 1: using MarkupSrcSet module: https://processwire.com/modules/markup-src-set/ - which adds the srcset method in it's ready() function. Is it possible to boostrap this prior to calling the render() function?
      Edit 2: bypassed the issue specific to the srcset above, it further appears that rendering output is problematic when using MarkupRegions. Title updated and tagged accordingly. Can anyone provide insight into how to retrieve the output of a page using markup regions in the template layer?
    • By humanafterall
      Hi,
      I would like to set an admin template to 'https only' as recommended in the Processwire security docs.
      However if I do this it forces this setting locally too, resulting in https://localhost requests which result in an error page.
      Is there a simple way round this? Setting https for templates in the config?
      Thanks!
    • By Chris Bennett
      Hi all, I am going round and round in circles and would greatly appreciate if anyone can point me in the right direction.
      I am sure I am doing something dumb, or missing something I should know, but don't. Story of my life 😉

      Playing round with a module and my basic problem is I want to upload an image and also use InputfieldMarkup and other Inputfields.
      Going back and forth between trying an api generated page defining Fieldgroup, Template, Fields, Page and the InputfieldWrapper method.

      InputfieldWrapper method works great for all the markup stuff, but I just can't wrap my head around what I need to do to save the image to the database.
      Can generate a Field for it (thanks to the api investigations) but not sure what I need to do to link the Inputfield to that. Tried a lot of stuff from various threads, of varying dates without luck.
      Undoubtedly not helped by me not knowing enough.

      Defining Fieldgroup etc through the api seems nice and clean and works great for the images but I can't wrap my head around how/if I can add/append/hook the InputfieldWrapper/InputfieldMarkup stuff I'd like to include on that template as well. Not even sure if it should be where it is on ___install with the Fieldtype stuff or later on . Not getting Tracy errors, just nothing seems to happen.
      If anyone has any ideas or can point me in the right direction, that would be great because at the moment I am stumbling round in the dark.
       
      public function ___install() { parent::___install(); $page = $this->pages->get('name='.self::PAGE_NAME); if (!$page->id) { // Create fieldgroup, template, fields and page // Create new fieldgroup $fmFieldgroup = new Fieldgroup(); $fmFieldgroup->name = MODULE_NAME.'-fieldgroup'; $fmFieldgroup->add($this->fields->get('title')); // needed title field $fmFieldgroup->save(); // Create new template using the fieldgroup $fmTemplate = new Template(); $fmTemplate->name = MODULE_NAME; $fmTemplate->fieldgroup = $fmFieldgroup; $fmTemplate->noSettings = 1; $fmTemplate->noChildren = 1; $fmTemplate->allowNewPages = 0; $fmTemplate->tabContent = MODULE_NAME; $fmTemplate->noChangeTemplate = 1; $fmTemplate->setIcon(ICON); $fmTemplate->save(); // Favicon source $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeImage"); $fmField->name = 'fmFavicon'; $fmField->label = 'Favicon'; $fmField->focusMode = 'off'; $fmField->gridMode = 'grid'; $fmField->extensions = 'svg png'; $fmField->columnWidth = 50; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon(ICON); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($fmField); // Favicon Silhouette source $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeImage"); $fmField->name = 'fmFaviconSilhouette'; $fmField->label = 'SVG Silhouette'; $fmField->notes = 'When creating a silhouette/mask svg version for Safari Pinned Tabs and Windows Tiles, we recommend setting your viewbox for 0 0 16 16, as this is what Apple requires. In many cases, the easiest way to do this in something like illustrator is a sacrificial rectangle with no fill, and no stroke at 16 x 16. This forces the desired viewbox and can then be discarded easily using something as simple as notepad. Easy is good, especially when you get the result you want without a lot of hassle.'; $fmField->focusMode = 'off'; $fmField->extensions = 'svg'; $fmField->columnWidth = 50; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon(ICON); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($fmField); // Create: Open Settings Tab $tabOpener = new Field(); $tabOpener->type = new FieldtypeFieldsetTabOpen(); $tabOpener->name = 'fmTab1'; $tabOpener->label = "Favicon Settings"; $tabOpener->collapsed = Inputfield::collapsedNever; $tabOpener->addTag(MODULE_NAME); $tabOpener->save(); // Create: Close Settings Tab $tabCloser = new Field(); $tabCloser->type = new FieldtypeFieldsetClose; $tabCloser->name = 'fmTab1' . FieldtypeFieldsetTabOpen::fieldsetCloseIdentifier; $tabCloser->label = "Close open tab"; $tabCloser->addTag(MODULE_NAME); $tabCloser->save(); // Create: Opens wrapper for Favicon Folder Name $filesOpener = new Field(); $filesOpener->type = new FieldtypeFieldsetOpen(); $filesOpener->name = 'fmOpenFolderName'; $filesOpener->label = 'Wrap Folder Name'; $filesOpener->class = 'inline'; $filesOpener->collapsed = Inputfield::collapsedNever; $filesOpener->addTag(MODULE_NAME); $filesOpener->save(); // Create: Close wrapper for Favicon Folder Name $filesCloser = new Field(); $filesCloser->type = new FieldtypeFieldsetClose(); $filesCloser->name = 'fmOpenFolderName' . FieldtypeFieldsetOpen::fieldsetCloseIdentifier; $filesCloser->label = "Close open fieldset"; $filesCloser->addTag(MODULE_NAME); $filesCloser->save(); // Create Favicon Folder Name $fmField = new Field(); $fmField->type = $this->modules->get("FieldtypeText"); $fmField->name = 'folderName'; $fmField->label = 'Favicon Folder:'; $fmField->description = $this->config->urls->files; $fmField->placeholder = 'Destination Folder for your generated favicons, webmanifest and browserconfig'; $fmField->columnWidth = 100; $fmField->collapsed = Inputfield::collapsedNever; $fmField->setIcon('folder'); $fmField->addTag(MODULE_NAME); $fmField->save(); $fmFieldgroup->add($tabOpener); $fmFieldgroup->add($filesOpener); $fmFieldgroup->add($fmField); $fmFieldgroup->add($filesCloser); $fmFieldgroup->add($tabCloser); $fmFieldgroup->save(); /////////////////////////////////////////////////////////////// // Experimental Markup Tests $wrapperFaviconMagic = new InputfieldWrapper(); $wrapperFaviconMagic->attr('id','faviconMagicWrapper'); $wrapperFaviconMagic->attr('title',$this->_('Favicon Magic')); // field show info what $field = $this->modules->get('InputfieldMarkup'); $field->name = 'use'; $field->label = __('How do I use it?'); $field->collapsed = Inputfield::collapsedNever; $field->icon('info'); $field->attr('value', 'Does this even begin to vaguely work?'); $field->columnWidth = 50; $wrapperFaviconMagic->add($field); $fmTemplate->fields->add($wrapperFaviconMagic); $fmTemplate->fields->save(); ///////////////////////////////////////////////////////////// // Create page $page = $this->wire( new Page() ); $page->template = MODULE_NAME; $page->parent = $this->wire('pages')->get('/'); $page->addStatus(Page::statusHidden); $page->title = 'Favicons'; $page->name = self::PAGE_NAME; $page->process = $this; $page->save(); } }  
    • By benbyf
      Hello, and welcome to what I though was either my client being silly and changing things, or some evil doer. Turns out its reproducible and therefore something in Proceswire (I checked my templates and modules but couldnt find anything that would be doing this...). So what is it doing? Check out the video for evidence.
      A repeater field is interacting with a page template and another repeater field somehow to swap the fields in the template and repeater over...
      I have a template called team, and a repeater field called team_repeater with label Team. Some how and for some reason, when I change my fields on repeater called main_menu_links my team template gets those fields and when I try and revert the team template fields to the fields it should have, they get given to the repeater main_menu_links. Also this to say HELP!!!!!
      video: https://www.dropbox.com/s/exkdhc6n7x0xpsa/strange-repeater-PW-mega-bug.mov?dl=0
    • By Aleksey Popov
      Hi! Is it possible to specify explicitly the parent section when creating a new child page with a single template?
      The idea is to save the user from the possibility of mistakenly choosing a section, but not to create unnecessary identical templates
      What I mean:
      Let's say I've got 2 parent categories with names articles & news. Each of them utilized the same admin-template (category) with the category.php file. Also, I have 2 different templates for child pages (article & news).
      Next, I need to add an article page to the articles category with the "create new" button in admin. At that moment, I must choose the category to place (because both categories use the same template). 
      To runaround this (and create a page right into the exact category without the choosing step) I should create 2 templates for each category, and setup parent-children relations for each pair of templates, right?
      But Is any way to use only the one category template and different children templates and at the same time, explicitly specify which section to use for child pages, thereby removing the process of selecting a child section?
       
      articles (category.php) Article page 1 (page-article.php) Article page 2 (page-article.php) Article page 3 (page-article.php) + Add a new article page here without category chooser news (category.php) News page 1 (page-news.php) News page 2 (page-news.php) News page 3 (page-news.php)  Update: found module  but I can't get it work.
       
×
×
  • Create New...