Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlrdw's avatar

@ApeWare that's weird, because in a normal php page, not even in laravel, viewing source is showing correctly. I still don't think it's a blade problem. But could be.

Try writing each line to just a text file and see if it works correctly.

ApeWare's avatar

I tried it with a text mailer and it still is stripping off the leading spaces.

In my core view for text emails, I have a centered footer block. This footer block renders perfectly fine and uses the same strategy I am trying to use for the title.

The only difference is that I am @yield()ing the components but the footer is right in the core. I am going to look into the core and see if there is something inside @yield() that does some trimming or something.

jlrdw's avatar

You stated

I already tried it with the . character and it works fine.

Thus it has to do with the space. I bet it would work correctly in writing it to just a raw text file.

ApeWare's avatar
ApeWare
OP
Best Answer
Level 5

I think I found the issue. When you use view components, the component data Laravel does a trim() to the data before merging it into its array; see source.

I am not sure how to work around this yet, but at least I know why it is happening. I don't think view should assume HTML is going to be used exclusively but that is just my opinion.

1 like
jlrdw's avatar

You can turn that off somewhere, I forget where. And this woks 100% correct

<?php
$out = fopen("list2.txt", "wb");
$nl = "";
$title = "Just a test";
$myline = str_pad( $title, 60, ' ', STR_PAD_BOTH );
fwrite($out, $myline);
fclose($out);
?>

In just native php

Edit:

The settings are in App\Http\Kernel class to turn off trimming.

ApeWare's avatar

@jlrdw, yes, the native PHP works, it is Laravel that is trimming component data.

I can handle this manually in the specific mailable I am working on but I will not be able to use Blade components for text versions of emails...unless I can convince Taylor to change this behavior.

Thanks for mentioning App\Http\Kernel I did, for sake of testing, comment that out of the middleware but it did not change the output. I don't think that affects the blade components; will have to look into that middleware to see exactly what it does.

jlrdw's avatar

At least as @Snapey said, it wasn't something daft you did. It's a blade thing. Those words he comes up with.

1 like
henrique@mustorze.com's avatar

You're using ' ' to create this spacing, right? Instead of using ' ', use this code, '&.nbsp;', but for it to work you should remove the '.' after '&'.

ApeWare's avatar

This is for a plain text email, so I can't use non-breaking space as the characters &nbsp; would be displayed as plain text.

henrique@mustorze.com's avatar

Humm. now i understand. Then, if your pass these spaces throught a variable, did you tried that?

example:

<?php $spaces = '                            '; ?>
{!! $spaces !!}
ApeWare's avatar

If I used your sample code, it too would be trimmed.

The issue as stated in a previous reply is:

When you use view components, the component data Laravel does a trim() to the data before merging it into its array; see source.

So as long as I don't use a Blade component it will display. You can see on line 77 in the source that it does a trim. The only way around this, currently, seems to be to avoid generating plain text using component unless you want the text trimmed. I was making a plain text version of an HTML component to be used for Mailables; cant do that if I want to replicate the centering of plain text.

Previous

Please or to participate in this conversation.