Store format in a variable

0

You see, I have a view where I want to show this:

<span class="badge badge-danger badge-cat">Telefono</span><br>
<span class="badge badge-danger badge-cat">Email</span><br>

Since it is too hard to put the value of "class" all the time, I have tried to store it in a variable.

<?php
    $eficiencia="badge badge-danger badge-cat";
?>

Then I have this view:

<?php
    $eficiencia="badge badge-danger badge-cat";
?>
<div class="card card-03">
    <div class="card-body">
        <h2 class="card-title"><b><i><u>{{$u->nombre()}}</u></i></b></h2>
        <table>
            <td>
                @if($u->activado)
                    <img class="card-img-top" style="width: 200px; height: 200px;" src="{{url($u->ruta())}}"/>
                @endif
            </td>
            <td>
                <span class={{$eficiencia}}>DNI: {{ $u->dni }}</span><br>
                <span class="badge badge-danger badge-cat">Telefono: {{ $u->telefono }}</span><br>
                <span class="badge badge-danger badge-cat">Email: {{ $u->email }}</span><br>
                <span class="badge badge-danger badge-cat">Fecha de nacimiento: {{ $u->fecha_nacimiento }}</span><br>
                <span class="badge badge-danger badge-cat">Ocupación: {{ $u->ocupacion->nombre }}</span><br>
                <span class="badge badge-danger badge-cat">Sexo:
                    @if($u->sexo)
                        Mujer
                    @else
                        Hombre
                    @endif
                </span><br>
                <span class="badge badge-danger badge-cat">Vehiculo propio:
                    @if($u->vehiculo)
                        Si
                    @else
                        No
                    @endif
                </span>
            </td>
        </table>
        @if($u->user_id==2)
        <hr>
            @if(!$u->activado)
                    <a href="{{ url('/correo_activacion/'.$u->id) }}" class="btn btn-primary btn-block">Enviar mensaje de activación</a>
            @elseif($u->id!=auth()->user()->id && $u->ocupacion_id!=1)
                <a href="{{ url('/desactivar_usuario/'.$u->id) }}" class="btn btn-warning btn-block">Desactivar al usuario</a>
            @endif
        @endif
    </div>
</div>

In the first "span", in which I ask DNI, I approve the variable. In the others I do it rough.

And here is where the error appears. When storing in the variable only stores the part of the "badge", but the rest does not. How do I fix this?

    
asked by Miguel Alparez 18.10.2018 в 13:03
source

1 answer

1

Use the expression @php @endphp of the Blade template engine to include php code and the error is given because instead of putting:

<span class={{$eficiencia}}>DNI: {{ $u->dni }}</span><br>

You should add the quotes that enclose your variable:

<span class="{{$eficiencia}}">DNI: {{ $u->dni }}</span><br> 

If you try to replace the value of your variable in your html tag you will realize what was wrong:

<span class=badge badge-danger badge-cat>DNI: {{ $u->dni }}</span><br>
    
answered by 18.10.2018 / 15:12
source