how to add a dynamic image src with laravels blade - laravel

My question is very simple. i use blade in laravel to show the images like
<img src="{{asset('assets/images/image.jpg')}}">
but what if my image is dynamic like:
<img src="{{asset('assets/images/$imgsrc')}}">
i cant use
<img src="{{asset('assets/images/{{ $imgsrc')}}">
because then i will end with:
<img src="http://localhost/assets/images/{{ $imgsrc">
How can i call to a $variable in a blade {{}} call?

How about
<img src="{{asset('assets/images/').'/'.$imgsrc}}">
EDIT
when you need printing more than one strings where there may be variables,functions... you need to concatenate them
Also asset('assets/images/') is equivalent to asset('assets/images') so you need to add / before printing your variable $imgsrc
Finally, in Blade {{"foo"}} is equivalent to <?php echo "foo" ?>

How can i call to a $variable in a blade {{}} call?
The normal PHP way. A blade {{}} is just the equilivant of <?php echo() ?>
So the answer to your question is:
<img src="{{ asset('assets/images/' . $imgsrc) }}">

Mine had multiple dynamic parts in the src and this is how I did it after getting in the right direction, thanks to #Laurence's answer.
<img src="{{asset('storage/'.$parentFolderName.'/'. $clientNameFolder .'/logo/').'/'.$event[0]->event_logo}}" class="event-page-country-logo">

Related

Correct approach for rendering optional param in blade

I noticed that below construction is not correct for Blade
<div class="constant #if($some_condition)optional#endif">
cause this is converted to in PHP which is invalide
<div class="constant <?php if($some_condition): ?>optional<?php endif; ?>">
To make this statement work I need to put close tag on different line but it looks very bad, especially if there are many others attribute.
<div class="constant #if($some_condition)optional
#endif" data-id="1" tabindex="2" data-etc="...">
What is the right way of rendering such kind of conditions in Blade?
Try the short version of if statement.
$some_condition ? 'optional' : ''
Using the code above, if the condition is true it will return the the string 'optional', if the condition is not true it will return an empty string I mean this ''. Note, an empty string as attribute do nothing inside Html element.
In your case it should look like this:
<div class="constant {{ $some_condition ? 'optional' : '' }}">

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

Qrcode in laravel

i want to make qrcode, in there i can make it but i have something trouble when i want to change the format of qrcode to png file. but its only show symbol
here my view :
<?php echo QrCode::size(265)->generate($row->id) ?>
this qrcode i use :
"simplesoftwareio/simple-qrcode": "~1"
here my referance : https://www.simplesoftware.io/docs/simple-qrcode
can anyone help me ? or can someone give me solution ?
before i change format to png :
and this after i change it :
There are more simple example available as well.
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(100)->generate('QrCode as PNG image!')) !!} ">
If you are formatting it as png file format, you need to include it with a <img> tag.
Taken from the documentation
//Inside of a blade template.
<img src="{!!$message->embedData(QrCode::format('png')->generate('Embed me into an e-mail!'), 'QrCode.png', 'image/png')!!}">
You can also do this:
$png = QrCode::format('png')->size(512)->generate(1);
$png = base64_encode($png);
echo "<img src='data:image/png;base64," . $png . "'>";
In controller
$path = getenv('IMAGE_URL')."/img/logo.png";
$png = QrCode::format('png')->merge($path, .17, true)->size(300)->errorCorrection('H')->generate($data);
$png = base64_encode($png);
In blade file
<img src='data:image/png;base64,{{$png}}'>
I came to this page because I needed to create a PNG file to send inline in an email in laravel8.
I used the examples above in order to create the QR code as a png which worked brilliantly. I used the following code:
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(100)->generate('QrCode as PNG image!')) !!}" />
However a number of email services (including Gmail) do not display images that use inlined base64 as above. Instead, they require you to add the base64 image as an attachment to the email and then reference that attachment in the img src.
Thankfully laravel (I'm using laravel8) has a really cool function that does this for you, so my code ended up looking like this (which worked):
<?php
$qrCodeAsPng = QrCode::format('png')->size(500)->generate("my text for the QR code");
?>
<img src="{{ $message->embedData($qrCodeAsPng, 'nameForAttachment.png') }}" />
The $message variable is one that is in every blade that is being sent as an email in laravel. In my case I did not want to create an actual image on the server, but if you wanted to use an image that you had stored you would use $message->embed().

Put the content of a Blade section in a variable

In my blade template, there is a simple section :
#section('container')
My html markup here
#stop
that I call in a "master.blade.php" file :
#yield('container')
As I could understand, the yield function print the content of container. But I would like to know if I can put the content of container in a variable instead of ?
I would like to do something like that :
$myvar = #yield('container');
I know it is not correct syntaxically but there is the idea. I want to put the content of the section container in a variable.
When returning the view, read the html markup from a file and use with:
$html = File::get($filepath);
return view('yourview')->with(compact('html'));
Then in your Blade view file you can just output it like so:
#section('container')
{{ html }}
#stop
You can read more on in the Laravel Views Docs
Why don't you use some javascript to do something like this:
var somvar = $("#container").html();
or something like this:
<?php ob_start(); ?>
<div>Contents goes <b>here</b></div>
<?php $contents = ob_get_contents(); ?>
Does it have to be in the view? Can you do something like...
$myvar = View::make('yourview.blade.php')->with('somevars');

MVC Question: How do I know when I'm putting too much logic into my view?

Consider the following from CodeIgniter. If I am doing this wrong, then please tell me:
<html>
<head>
<title><?=$title ?></title>
</head>
<body>
<h1><?=$heading; ?></h1>
<?php foreach ($query->result() as $row):?>
<p>
<?=$row->LastName . ', ' . $row->FirstName . ' ' . $row->MiddleName?>
</br>
<?=$row->myField?>
<?php if ($row->myField == ''):?>
no data found
<?php endif;?>
some text
<?=$row->anotherField ?>
</br>
<?php if ($row->purchasedate !='') {echo 'Purchase Date ' . mdate('%m' . '/' . '%d' . '/' . '%Y', strtotime($row-> purchasedate));}?>
</p>
<p>
<label for="ProductNumber">Product</label>
<input type="text" name="Product_number" id="ProductNumber" value =
<?=$row->ProductNumber?>
/>
</br>
<?php if ($row->Purchased == '-1'): ?>
<h3>Bought</h3>
<?php endif;?></br>
<?php if ($row->Sold == '-1'): ?>
<h3>Sold</h3>
<?php endif;?></br>
</p>
<?php endforeach;?>
</body>
</html>
I know some of it will not make sense. I am experimenting and also changed some of the names of fields.
My question is: Is this too much intermixing of code into my view? It seems this occurs with most templating but that there is some vague line that just feels wrong when you cross it and you say, "That's too much logic in my view." I don't know that line. Is this example crossing it? A foreach, if thens, echo, strtotime, the ?php tags, etc.
Is it just crossing the "line" when I put db access logic and start emitting all my html tags from print or response.write statements all on one big page behind the scenes on the server?
There's better checks you can do instead of shoving that logic in the view. For example you're doing things like if($row->Purchased == '-1') when in fact it would be better to have if($row->isPurchase), that way determining the validity and control flow is placed further onto the state of your model. If you followed this logic throughout your code example you'll see it actually turns into something much simpler.
MVC Question: How do I know when I’m putting too much logic into my view?
IMHO
If your logic is doing anything other than making presentational decisions.
In the example all your if statements are doing something graphical.
So I think there is nothing wrong with that code.
Using all those php tags can make a view look like a mess. But thats just php.
Thats why I use smarty, it makes it a bit more readable.
Yeah I know php is a template language but its just my opinion.

Resources