Jump to content

Recommended Posts

Posted

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.

Posted

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.

Posted

@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);
	}
});

 

  • Like 2

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.
×
×
  • Create New...