mel47 Posted August 1, 2017 Share Posted August 1, 2017 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 More sharing options...
Ivan Gretsky Posted August 1, 2017 Share Posted August 1, 2017 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}'> .... } 4 Link to comment Share on other sites More sharing options...
szabesz Posted August 1, 2017 Share Posted August 1, 2017 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. 2 Link to comment Share on other sites More sharing options...
mel47 Posted August 2, 2017 Author Share Posted August 2, 2017 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 More sharing options...
Ivan Gretsky Posted August 2, 2017 Share Posted August 2, 2017 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). 3 Link to comment Share on other sites More sharing options...
Sanyaissues Posted July 8, 2023 Share Posted July 8, 2023 @Ivan Gretsky that's genius! Thanks. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now