Using Lumen 5.4.5.
I'm attempting to use a Blade templating conditional to only display a value if it is populated. Here is my syntax:
{{ ($board->a1 != '') ? "a1: '$board->a1'" : '' }}
Should render like this in browser:
a1: 'wR'
Instead it's rendering the HTML encoding string like this:
a1: 'wR'
How can I successfully render the uncoded apostrophes in my Blade conditional?
You can avoid the the call to htmlspecialchars automatically done with {{ }} using {!! !!} instead. https://laravel.com/docs/5.4/blade
Related
I am using Laravel 5.7 I am having a variable stored in the database and would like to have it evaluated by Blade before rendering.
Database
Field: name, Value: {{ $organisername }}
I am pulling out this piece of data into a Blade template like this.
<h4>1. Registration</h4>
<br />
{{ $job->organisername }}
Chrome then displays {{ $organisername }} instead of evaluating {{ $organisername }}.
1. Registration
{{ $organisername }}
If I type {{ $organisername }} into the Blade template, it gets evaluated to John when the page is rendered..
1. Registration
John
I'm using TinyMCE to input {{ $organisername }} into the textarea, and it gets saved into a Database, if that helps.
If by evaluate you mean to do some consistency for your variable, know that this Blade echo statement "{{ }}" already does that with htmlspecialchars, like explained in the docs:
https://laravel.com/docs/5.7/blade#displaying-data
Blade templates are compiled down into PHP before they evaluate. Essentially, blade compiles:
{{ $job->organisername }}
Into
<?php echo e($job->organisername); ?>
Once you understand this and how PHP evaluates variables, you should be able to understand why you can't set Blade constructs in the database and have them evaluated when it's printed.
It's basically the same as echoing a literal string:
<?php echo '{{ $organisiername }}'; ?>
You wouldn't want Blade or PHP evaluating any expression they come across as they print, that would potentially be very insecure.
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
I want to display full name as title:
#section('title', ['title' => $userdetail->fullName])
I have used the above code and it shows the error exception of string to object conversion.
modify your #section to this
#section('title', {{$userdetail->fullName}})
#section('title', $userdetail->fullName)
You don't need to specify it as an associative array, just put variable and section will be the value of this variable
Do like this --
#section('title', "$userdetail->fullName")
Wrap the second parameter inside double quotes since it renders it automatically renders a variable and normal text, whereas single quotes only render text and not variable.
And remember that you cannot use blade echo {{ }} inside a blade syntax.
like - #section('title', {{ $userdetail->fullName }})
I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.
You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}
In Laravel 4.1, I want to echo:
<label for="name">Name <small>required</small></label>
This doesn't work:
{{ Form::label('name','Name <small>required</small>') }}
are automatically converted to code text…
Is there a way or do I have to skip Form::label and do it manually?
If you want to use a Html tag inside another by Laravel one you should use HTML::decode.
Correct code :
{{ HTML::decode(Form::label('name','Name <small>required</small>')) }}
Try this:
{!! Html::decode(Form::label('name','Name <small>required</small>')) !!}