Jump to content

Render field and a variable (solved)


mel47
 Share

Recommended Posts

Hi

I'm not sure I understand correctly the rendering of a field, more specifically passing $options. I want to use some variables to avoid creating many templates, but I'm not sure how. It should be something like this, but it doesn't work:
 

---in my page
  $sizeColumn = "2";
  $content .= $thanks_page->render('images', '/images_column', $sizeColumn);

---In images_column.php
$sizeColumn ='';

foreach($value as $image) {

			echo "
			<article class='column is-{$sizeColumn}'>
			....
              }

 

What I'm doing wrong?

Thanks
Mélanie

Link to comment
Share on other sites

Hey, @mel47!

Here is the code for the method. Seems like the 3rd parameter is to override default field value, not to provide additional variables to the template:

 @param mixed|null $value Optionally specify value to render, otherwise it will be pulled from this $page. 

Did not try this, but you probably can do this to have additional variable in the field template context:

// main template
$page->$sizeColumn = 2;
$content .= $thanks_page->render('images', '/images_column');

// images_column.php
foreach($value as $image) {
	echo "
		<article class='column is-{$page->sizeColumn}'>
		....
}

 

  • Like 4
Link to comment
Share on other sites

3 hours ago, Ivan Gretsky said:

$page->sizeColumn

"Also worth mentioning is that each field template file receives all of ProcessWire's API variables. It also receives....":

https://processwire.com/blog/posts/more-repeaters-repeater-matrix-and-new-field-rendering/#processwire-3.0.5-introduces-field-rendering-with-template-files

Since no additional variables can be passed to the method, the workaround shown by @Ivan Gretsky is the recommended way to do it.

  • Like 2
Link to comment
Share on other sites

Thanks @Ivan Gretsky, effectively adding a new field in the template do the job. 

Final result:

// main template
$content .= $thanks_page->render('images', '/images_column');

// images_column.php
foreach($value as $image) {
	echo "
		<article class='column is-{$page->sizeColumn}'>
		....
}

 

Link to comment
Share on other sites

5 hours ago, mel47 said:

Thanks @Ivan Gretsky, effectively adding a new field in the template do the job. 

Great, @mel47! But I was not actually advising to add an extra field to the template. I thought it would be enough just to write

$page->sizeColumn = 2;

before the render call. It would add a temporary property to the current $page object, which will be passed to field template (but will not write this field to database).

  • Like 2
Link to comment
Share on other sites

  • 5 years later...

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