ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Add @trim to blade

Open donnysim opened this issue 4 years ago • 7 comments

Introduce @trim "directive" to blade by default to remove trailing and leading whitespaces, helpful when dealing with those pesky little whitespaces between elements without having to resort to ugly comments or workarounds.

$this->callAfterResolving('blade.compiler', static function (BladeCompiler $compiler) {
    $compiler->extend(static function ($value) {
        return \preg_replace('/\s*@trim\s*/m', '', $value);
    });
});
@for($i = 0; $i < 10; $i++)
  @trim
  <button class="btn btn-primary">
    Action {{ $i }}
  </button>
  @trim
@endfor

Normal result:

msedge_KXrqYvIdws

With @trim:

msedge_mKhcsSs7yk

donnysim avatar Apr 19 '20 14:04 donnysim

It's more of CSS rather than HTML. How white space is added? Is there any bootstrap padding?

mohitpawar10 avatar Apr 26 '20 19:04 mohitpawar10

This has nothing to do with css you can read about it here for what tricks can be used to avoid it: https://css-tricks.com/fighting-the-space-between-inline-block-elements/

Imo, all css solutions except flexbox are too brittle, so this would be nice to have when you can't or don't want to use it.

donnysim avatar Apr 27 '20 04:04 donnysim

Really good idea 👍

julienbourdeau avatar Apr 28 '20 07:04 julienbourdeau

Why not send a PR in?

alexbowers avatar May 01 '20 12:05 alexbowers

Why not send a PR in?

Mainly because I don't really know the proper way to add this to blade but that's probably not a big deal; want to see what other opinions are and have a feeling it will get insta-closed 😅

donnysim avatar May 01 '20 17:05 donnysim

Just make this as a custom directive... In my 4 years working with Laravel I have never experienced this issue.

linaspasv avatar May 05 '20 07:05 linaspasv

I went ahead and ran a search here in the Ideas area and found this thread.

Not sure if this alternative approach is worthwhile to explore (this @trim directive seems like it might handle a slightly different requirement), but one thing I have ran into using Blade already is additional whitespace / newlines can be added unnecessarily at times...for example, I'm working on a new project and there is some Blade related code that I'm using the Blade comments option for, but there is also a loop of additional values, and for each of those I have the value commented out with a Blade comment.

Blade example:

{{ cas()->getAttribute('display_name') }}
{{-- cas()->getAttribute('first_name') --}}
{{-- cas()->getAttribute('last_name') --}}
@foreach(cas()->getAttribute('groups') as $group )
    {{-- $group --}}
@endforeach

In my HTML output the first 3 Blade lines above would result in the following (I'm not including the foreach part above, which would add additional blank lines into the HTML output):

<p class="text-sm leading-5 font-medium text-white">
Taylor Otwell
  
  
</p>

I don't believe I'm the only one that has encountered this type of issue and luckily, there is a similar discussion that's already occurred with a solution created over in the Golang community here: https://github.com/golang/go/issues/9969

With associated commits:

  • https://github.com/golang/go/commit/e6ee26a03b79d0e8b658463bdb29349ca68e1460 (primary commit for the feature in Go's text/template library)
  • https://github.com/sanbornm/gowsdl/commit/baa80261659b15c820807cda230de5f576f70fa1 (additional commit that may or may not be needed if such a feature were brought into Blade)

In some ways, I'm not entirely sure what the behavior should be in the Blade comment scenario, but I feel like it would be nice if the Blade comments would automatically prevent unnecessary whitespace from being included in the HTML output, which doesn't appear to be the current behavior.

From a developer perspective, I think for some of us we may want to leave some Blade code included in our file in a commented out fashion, since it might be something we can utilize later on as a project progresses (e.g. I might run some tests early on and make some notes of what I can use later on in the project, but can't utilize it immediately), but at the same time we don't want those commented out Blade lines to pollute the HTML output with unnecessary blank lines either (even if it's not going to do anything horrible to our web application, visually it can be a bit of a nitpick sort of item that would be nice to be able to avoid if possible).

Extra Text from the Golang Release Notes for 1.6:

https://golang.org/pkg/text/template/#hdr-Text_and_spaces

Text and spaces

By default, all text between actions is copied verbatim when the template is executed. For example, the string " items are made of " in the example above appears on standard output when the program is run.

However, to aid in formatting template source code, if an action's left delimiter (by default "{{") is followed immediately by a minus sign and white space, all trailing white space is trimmed from the immediately preceding text. Similarly, if the right delimiter ("}}") is preceded by white space and a minus sign, all leading white space is trimmed from the immediately following text. In these trim markers, the white space must be present: "{{- 3}}" is like "{{3}}" but trims the immediately preceding text, while "{{-3}}" parses as an action containing the number -3.

For instance, when executing the template whose source is

"{{23 -}} < {{- 45}}"

the generated output would be

"23<45"

For this trimming, the definition of white space characters is the same as in Go: space, horizontal tab, carriage return, and newline.

Back in the first link I shared above where the issue was discussed (https://github.com/golang/go/issues/9969) there are also comments mentioning that Jinja and Ruby seem to already have a similar syntax in place (and Golang does now too since v1.6) so it might be useful to have it available as an option within the Blade syntax as well.

orware avatar Apr 27 '21 15:04 orware