xportde Posted July 1, 2020 Share Posted July 1, 2020 Hello together, I have a question about field templates: When the output of a field template (using with $page->render->field) starts with a whitespace, it will be automatically trimmed off. Is it possible to suppress this behaviour, in case we need the whitespace explicitely? Thanks in advance, Thomas. Link to comment Share on other sites More sharing options...
adrian Posted July 1, 2020 Share Posted July 1, 2020 Is it only when using field templates, or is the trimming happening on a textarea field and as such would happen even if you output it directly? If so, take a look at this issue (https://github.com/processwire/processwire-issues/issues/459) and the noTrim option. Link to comment Share on other sites More sharing options...
xportde Posted July 1, 2020 Author Share Posted July 1, 2020 No, is happens actually in a SelectOptions field ($value) render template, that generates css classes depending on settings made by user: $css = ''; foreach ($value as $device) { switch ($device->value) { case 'mobile': $css .= ' sm-hide'; break; case 'tablet': $css .= ' md-hide'; break; case 'desktop': $css .= ' xlg-hide lg-hide'; break; } } echo $css; In case of checking the mobile option, $css contains ' sm-hide'. But in the calling template, only 'sm-hide' is delivered, without the trailing whitespace: <section class="class1 class2 class3<?=$page->render->devices?>"> is rendered as <section class="class1 class2 class3sm-hide"> Of course we could set the space directly after 'class3', but in case of no selected device option it would be unnecessary. Link to comment Share on other sites More sharing options...
Robin S Posted July 2, 2020 Share Posted July 2, 2020 @xportde, the ouput is trimmed of whitespace because the field render makes use of TemplateFile::render and by default the trim option is set to true. /** * Trim leading/trailing whitespace from rendered output? * * @var bool * */ protected $trim = true; There's no built-in way to change that setting in the render() call but you can change it with a hook if it's important: $wire->addHookBefore('TemplateFile::render', function(HookEvent $event) { $tpl = $event->object; if($tpl->field && $tpl->field->name === 'your_field_name') { $tpl->setTrim(false); } }); 2 Link to comment Share on other sites More sharing options...
xportde Posted July 2, 2020 Author Share Posted July 2, 2020 @Robin S I knew, that there is a way to do this, because it's ProcessWire! Thank you so much for the hint! 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