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

Alizey's avatar

Why Blade Template Always Echo Variables :(

At my view i dont want to echo this variable of path

   {!! $path = "uploads/$testimonial->logo" !!}
      @if(!File::exists($path) || $testimonial->logo =="")
             image not found
      @else
            <img alt="alt" src='{{URL::to("/uploads/$testimonial->logo")}}'>
      @endif 

but why its always echo ? its echo the path variable. i don't want to show that :(

0 likes
7 replies
xsmalbil@icloud.com's avatar

cause it is <?php echo $var; ?>

But you are in luck. Cause Php was designed as a templating language. :p

Just use php in you blade templates if you can't get it to work.

In the end it is name.BLADE.PHP. So it's plain php with blade helpers. If blade doesn't help you just use PHP.

<?php $path = "uploads/$testimonial->logo";  ?>

 @if(!File::exists($path) || $testimonial->logo =="")
             image not found
      @else
            <img alt="alt" src='{{URL::to("/uploads/$testimonial->logo")}}'>
      @endif 

Alizey's avatar

@xsmall Yes this works always

<?php $path = "uploads/$testimonial->logo";  ?>

ok now i use php where i want some variable like path etc. Thanks

RachidLaasri's avatar

Why would you put all that PHP code in your template? Extract that to a helper or something.

function testimonialLogo($logo)
{
    $image = "uploads/".$logo;

    if(!File::exists($image) || $logo =="")
    {
        return "image not found";
    }

    return "<img alt='alt' src='{{ URL::to('.$image.') }}'>";
}

Usage in your template file :

{!! testimonialLogo($testimonial->logo) !!}
xsmalbil@icloud.com's avatar

Yeah don't be affraid to use php as a templating language. Like..why not? Our community talks a lot about SOLID, DRY..and maybe you've heared of Hexagonal design?

So in hexagonal design, everything is a detail of a application. A swappable part. Even the framework is a part of your application that could be swapped wit another one. This hardly ever happens..but I had to do this one time. Luckily I had all my stuff in commands etc. My swap was between Symfony and Laravel. I was using packages like Tactician. So it was not very hard to move. ....

But anyways.. what I want to say is that Blade is not something that every framework uses. In many cases it is maybe better to use Twig. But nevermind that. If you want to enjoy your views in the future..try to keep this in mind and maybe use plain php whenever you can.

It's just a thought though :D I use blade as much as I can too. But that's because I know I won't be swapping Laravel for a better framework anytime soon. Cause..you know... Laravel FTW! :D

Have fun my friend.

xsmalbil@icloud.com's avatar

Oh and what @RachidLaasri said is always a good idea. Twig has Twig Extensions. Blade has Blade macro's or helpers. Angular has Filters.. So if you have logic that can be moved elsewhere...please do so.

Behinder's avatar

I have the same problem. Which is weird because in one blade file variable is echoed and in another is not...

jlrdw's avatar

Why Blade Template Always Echo Variables

It don't, for php logic you use the correct blade tags:

Example:

@php echo str_replace('/?', '?', $dogs->appends($qs)->render('custompager')) @endphp

Because of the str_replace in this case.

otherwise

{{ $dogs->links('cpag.custom') }}

Okay you got me, a 4 year old post, really, start a new post.

Please or to participate in this conversation.