How to eval Blade variable when pulling variable from database - laravel

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.

Related

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

How to the domain url in laravel5

I have to get the domain url (ex. http://www.example.com/) in laravel blade. I've tried using {{ url() }} but it returns the path to my public directory. Is there any one line function to get this? How do I get the domain in blade? Need help. Thanks.
You can also try
{{ Request::server ("SERVER_NAME") }}
{{ Request::server ("SERVER_NAME") }}
Or go with
{{ Request::root() }}

About Laravel - How to use function string in view

i try to to show data from database article...the content is contain html tags and long text..
so i want make a read more and convert tag html to html view..
this is my code is run :
{{ HTML::decode($show->content) }}
{{ str_limit($show->content, $limit=100, $end=' ...') }}
i try this :
{{ HTML::decode(str_limit($show->content,$limit=100, $end=' ...')) }}
{{ str_limit(HTML::decode($show->content),$limit=100, $end=' ...') }}
but not show ( blank )
annyone can help me to fix it??
thank u b4
Is {{ $show->content }} returning any data to format?
Is your template is .blade?
Maybe it placed somewhere in HTML, where it is not visible?
Try to use this construction with some predefined string to find out if it works. Here is working example:
{{ str_limit('Some big text', $limit = 5, $end='...') }}

Get image name from database in laravel

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) }}

Using HTML Placeholder in Laravel 4

{{ Form::text('username', Input::old('username')) }}
This is my code, I want to add Username as placeholder in the text box. I already use bootstrap in the project.
I tried to use the example
{{ Form::text('username', 'Username', Input::old('username')) }}
But it gives a default value which needs to be deleted and then the user has to type his value. I want something like how it shows up on Twitter.com etc. Which gets erased once user types into it.
{{ Form::text('username', Input::old('username'), array('placeholder'=>'Username')) }}
This works for Laravel 4!!
{{ Form::text('username', null, array('placeholder'=>'Username' )); }}
Use:
{{ Form::text('txtUsername', '',array('class'=>'input', 'placeholder'=>'Username')) }}
Been a while since this was updated but with Former for Laravel the way to do this would be:
Former::text('field')->placeholder('langfile.path.to.placeholder.text')))
The lang file would be called langfile.php with the array containing:
<?php
return array (
'path.to.placeholder.text' => 'Some Placeholder Text.'
);
In case somebody wonders how this should work for a password field:
{{ Form::password('password', array('placeholder'=>'Password')) }}

Resources