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

anthonymau's avatar

If statements in blade template breaking code

Hi all

I've recently upgraded to Livewire 3, and I'm getting some weird behaviour when I use if statements in the blade template. Firstly, it seems to be adding comments in my source code, seconding, if I don't have a line b/w the if statement and the conditional code, it ignores it (not visible in the source code even if condition is met).

In the example below, the center coordinates change depending on whether a cookie is set (last viewed). If not, it'll return a default position (lng,lat). In this example, it's working fine.

var map = new mapboxgl.Map({
            container: 'mapBox',
            style: defaultStyle,
            @if($last_lnglat)
            center: [ {{ $last_lnglat }} ],
            @else
            center: [135, -25],
            @endif
			zoom: 12,
            projection: 'mercator',
            attributionControl: false,
            maxPitch: 60,
            antialias: true,
});

Default output:

....
style: defaultStyle,
center: [135, -25],
zoom: 12,
....

Not, with the identical code, it's outputting this:

....
<!--[if BLOCK]><![endif]-->            center: [135, -25],
<!--[if ENDBLOCK]><![endif]-->

<!--[if BLOCK]><![endif]-->
 zoom: 4.1302323600355,
....

In this case, the center variable is ignored and the map defaults to lng/lat of 0,0.

Strangely, I can solve this by adding new lines inside the @if statement:

Code:

@if($last_lnglat)
            
center: [ {{ $last_lnglat }} ],
@else

Outputs:

<!--[if BLOCK]><![endif]-->            
center: [ 146.59316816920563,-31.305648642509937 ],
<!--[if ENDBLOCK]><![endif]-->

This works, but it's not ideal and not the expected behaviour.

Seems to be something to do with this: https://github.com/livewire/livewire/blob/main/src/Features/SupportMorphAwareIfStatement/SupportMorphAwareIfStatement.php

0 likes
7 replies
LaryAI's avatar
Level 58

It appears that the issue you're encountering is related to Livewire's morphdom JavaScript library, which is responsible for efficiently updating the DOM to reflect the state of your Livewire components. The comments you're seeing (<!--[if BLOCK]><![endif]-->) are likely placeholders used by Livewire to manage conditional rendering in the DOM.

To resolve the issue, you can try a couple of approaches:

  1. Use Livewire's @entangle directive: If you're trying to bind a JavaScript variable to a Livewire property, you can use the @entangle directive to create a reactive bridge between Livewire and Alpine.js (or vanilla JavaScript).

  2. Refactor your Blade template: Instead of embedding the conditional logic directly within the JavaScript object, you can assign the center coordinates to a JavaScript variable before the map initialization and then use that variable within the map configuration.

Here's an example of how you might refactor your Blade template:

<script>
    // Define the center coordinates before initializing the map
    var centerCoordinates = @json($last_lnglat ? $last_lnglat : [135, -25]);

    var map = new mapboxgl.Map({
        container: 'mapBox',
        style: defaultStyle,
        center: centerCoordinates,
        zoom: 12,
        projection: 'mercator',
        attributionControl: false,
        maxPitch: 60,
        antialias: true,
    });
</script>

In this example, we use the @json directive to safely output the PHP variable as a JSON-encoded JavaScript variable. This approach avoids the need for inline @if statements within the JavaScript object and should prevent the issues you're experiencing with Livewire's DOM diffing.

If you continue to experience issues, make sure to check the Livewire documentation for any changes related to conditional rendering in Blade templates, as Livewire 3 may have introduced changes that affect how these are handled. Additionally, ensure that your Livewire components and scripts are up to date and that there are no JavaScript errors in the console that could be causing unexpected behavior.

1 like
Snapey's avatar

why not set the $last_lnglat appropriately in the component and just echo it, removing the @if ?

anthonymau's avatar

@Snapey I could definitely optimise the code, but this was just one example. Sometimes I use the @if to do more than set a variable (e.g. @if(x==y) block of code @else another block of code @endif. So this was just a breaking change that I was not aware of, and I'm not really sure why @if is an issue as it's always worked.

Snapey's avatar

@anthonymau It should definitely not be a breaking change in ANY blade files, but in this case its inside a script tag and you are potentially changing the JS variables on the fly. Clearly something you ar doing is confusing the Livewire dom-diffing logic

anthonymau's avatar

@Snapey I thought that too, but I'm getting the same behaviour in jetstream templates (unchanged)

e.g. logout-other-browser-sessions-form.blade.php

@if (count($this->sessions) > 0)
    <div class="mt-5 space-y-6">
        <!-- Other Browser Sessions -->
        @foreach ($this->sessions as $session)
            <div class="flex items-center">
                <div>
                    @if ($session->agent->isDesktop())
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-8 h-8 text-gray-500">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M9 17.25v1.007a3 3 0 01-.879 2.122L7.5 21h9l-.621-.621A3 3 0 0115 18.257V17.25m6-12V15a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 15V5.25m18 0A2.25 2.25 0 0018.75 3H5.25A2.25 2.25 0 003 5.25m18 0V12a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 12V5.25" />
                        </svg>
                    @else
                        <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-8 h-8 text-gray-500">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M10.5 1.5H8.25A2.25 2.25 0 006 3.75v16.5a2.25 2.25 0 002.25 2.25h7.5A2.25 2.25 0 0018 20.25V3.75a2.25 2.25 0 00-2.25-2.25H13.5m-3 0V3h3V1.5m-3 0h3m-3 18.75h3" />
                        </svg>
                    @endif
                </div>

                <div class="ml-3">
                    <div class="text-sm text-gray-600">
                        {{ $session->agent->platform() ? $session->agent->platform() : __('Unknown') }} - {{ $session->agent->browser() ? $session->agent->browser() : __('Unknown') }}
                    </div>

                    <div>
                        <div class="text-xs text-gray-500">
                            {{ $session->ip_address }},

                            @if ($session->is_current_device)
                                <span class="text-green-500 font-semibold">{{ __('This device') }}</span>
                            @else
                                {{ __('Last active') }} {{ $session->last_active }}
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
@endif

Gives

    <div class="mt-5 space-y-6">
    <!-- Other Browser Sessions -->
    <!--[if BLOCK]><![endif]-->                    <div class="flex items-center">
            <div>
                <!--[if BLOCK]><![endif]-->                                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-8 h-8 text-gray-500">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M9 17.25v1.007a3 3 0 01-.879 2.122L7.5 21h9l-.621-.621A3 3 0 0115 18.257V17.25m6-12V15a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 15V5.25m18 0A2.25 2.25 0 0018.75 3H5.25A2.25 2.25 0 003 5.25m18 0V12a2.25 2.25 0 01-2.25 2.25H5.25A2.25 2.25 0 013 12V5.25" />
                    </svg>
                    <!--[if ENDBLOCK]><![endif]-->
            </div>

            <div class="ml-3">
                <div class="text-sm text-gray-600">
                    1 - 1
                </div>

                <div>
                    <div class="text-xs text-gray-500">
                        127.0.0.1,

                        <!--[if BLOCK]><![endif]-->                                        <span class="text-green-500 font-semibold">This device</span>
                            <!--[if ENDBLOCK]><![endif]-->
                    </div>
                </div>
            </div>
        </div>
        <!--[if ENDBLOCK]><![endif]-->
</div>
<!--[if ENDBLOCK]><![endif]-->

In this case $session->agent->platform() returns 1, but returns the platform correctly if I use

{{ $session->agent->platform() }}

Elsewhere...

flip's avatar

I needed the same fix. Really annoying and code breaking when upgrading.

mylesduncanking's avatar

For anyone looking into this (as I was) the Livewire 3 docs mention there is a config option to disable this:

you can disable it with the following configuration in your application's config/livewire.php file: 'inject_morph_markers' => false,

livewire.laravel.com/docs/morphing#injecting-morph-markers

3 likes

Please or to participate in this conversation.