Laravel 5 echo out session variable containing html in blade - php

I did a redirect in laravel:
return redirect('admin')->with($returnData);
$returnData is a string that contains a bootstrap info div with the result from the controller. Almost everything is working except when the page loads again it shows the html on the page as if it were text, brackets and everything. If I use this:
#if(!empty(Session::get('error'))) {{ Session::get('error')}} #endif
Then it shows is as pure text. If I change it to
<?php if(!empty(Session::get('error'))) { echo Session::get('error'); } ?>
It works fine. Im ok keeping it like this but I would rather utilize Blade / Laravel as its supposed to be used so I was wondering if there is a way to have the #if statement show the rendered html and not the text version?

I would recommend returning just the error message then in your view create the div. So if you were to change the layout of the view, you would it in one place.
#if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error')}}
</div>
#endif
hope this help.

To show the rendered HTML you should use {!! $variable->coontent !!} in your view, and this gonna convert your HTML text to render

May this example will help you.
Try this
#if (session('Error'))
<div class="alert alert-success">
{{ session('Error') }}
</div>
#endif

If you want to display plain text from error without any HTML entities you can simply use:
{{ Session::get('error') }}
or
{{ session('error') }}
If you have HTML entities in your variable then use:
{!! Session::get('error') !!}

Try changing your blade code to following.
#if(!empty(Session::get('error')))
{!! Session::get('error') !!}
#endif

Related

Noob Q: Laravel 5.4 - redirect()->with()

I'm currently learning Laravel 5.4. I'm following a beginner's tutorial (we're on the same Laravel version).
In the video tutorial, the guy uses the following line of code in a controller:
return redirect()->route('posts.index')->with('error','Unauthorised!');
We both have the following in the view:
#if(count($errors))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{ $error }}
</div>
#endforeach
#endif
The code works perfectly fine for the tutor on screen, however it doesn't for me - it redirects but doesn't pass the errors.
I used the following modified code in my controller and it worked:
return redirect()->route('posts.index')->withErrors(['error'=>'Unauthorised!']);
In order for me to learn, I need to know why the original code works for him - but not me? Like I said previously, we're both using the same version of Laravel.
Can anyone explain why?
I haven't seen the video, so can't say to as why his does work.
1.
return redirect()->route('posts.index')->with('error','Unauthorised!');
You redirect to the posts.index with a variable called error, and you check for a variable called errors so you could do it like this instead
#if(count($error))
<div class="alert alert-danger">
{{ $error }}
</div>
#endif
2.
You could support multiple errors like you other example and you could do it like
return redirect()->route('posts.index')->withErrors(['Unauthorised!', 'error_2', 'error_3', 'etc']);
and then you can loop through them as in your own first example (You don't need the keys in the array, but you can have them if you feel for it)
#if(count($errors))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{ $error }}
</div>
#endforeach
#endif
3.
A third option would also be to use flash sessions, which is a session that only lives for the next request
than you could do
return redirect()->route('posts.index')->session()->flash('error', 'Unauthorised!');
and in your views
#if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error') }}
</div>
#endif
which I personally prefer as it gives me the option to just include the if statement in my "main layout file", and it will be shown on all the including pages.

Laravel : when i return styled string from database its appear with the html tags

I store in the database some text with html tags using ckeditor , and when i print the text in laravel blade the text appears styled but the tags appears with the text check the process below .
This is the stored text in mysql database :
<h4>English</h4>
And this is the div section in the blade where the text printed
<div class="col-md-12">
{{$post->title_ar}}
</div>
And this is the result
<h3> English </h3>
i want the text appear without tags like this : English
It should work if you change your syntax from {{ }} to {!! !!}.
It also depends what version you are using...
V-5 Should be: {!! !!}
use {!! !!} not {{ }}, it will work with you.

Laravel5 render html::linkroute

i am using html::linkroute however the link tag and contents are being put on the screen rather than rendered, is this a bug?
The code i am using
{{ HTML::linkRoute('admin.users.edit', $user->display_name, array($user->id)) }}
the output in the browser
Prof. Trent D'Amore
In Laravel 5 {{ ... }} escapes the output, thats why you see the HTML in the browser. Instead you should use {!! ... !!} which will render the raw output to the browser. So this will work:
{!! HTML::linkRoute('admin.users.edit', $user->display_name, array($user->id)) !!}
You can read more about Laravel 5 Blade changes.

Laravel - Blade comments , blade rendering causing page to crash

I'm rendering a page that is primarily a form with view::make in Laravel and it is crashing, causing ERR_CONNECTION_RESET. After a long investigation and many red herrings, I started erasing (not commenting) random sections out of the blade file for the view and realized that if I
a) erase 2 of the {{Form}} calls inside this section of the form
b) remove the {{-- and --}} from around this section of the form
{{--
<div class="form-row">
{{ Form::label('foo', 'foo:') }}
{{ Form::text('foo') }}
</div>
<div class="form-row">
{{ Form::label('foo', 'foo:') }}
{{ Form::text('foo') }}
</div>
<div class="form-row">
{{ Form::label('foo', 'foo') }}
{{ Form::text('foo') }}
</div>
--}}
the page will render. I am not sure what exactly the cause here is. There are other blocks above and below, although this is a 3-div commented out section which none of the others are.
Anyone have a clue what is causing this? Running on WAMP if that matters.
Blade comments should only be used for simple remarks or to comment out single-line Blade functions. A single Blade comment cannot be used to comment out multiple lines of code.
Use PHP Block Comments instead. They are still usable in a blade.php file
<?php /*
{{ HTML::form("foo") }};
{{ HTML::form("bar") }};
*/ ?>
Alternatively, comment out your Blade one line at a time:
{{-- HTML::form("foo") --}};
{{-- HTML::form("bar") --}};
Examples of Valid Blade Comments:
Single Blade Function:
{{-- Form::text('foo') --}}
Remark:
{{-- Form Section 1 --}}
Examples of Invalid Blade Comments:
Incorrect syntax:
{{-- Form::text('foo') -- }}
"#" Inside of Blade comment
{{-- #Form::text('foo') --}}
Nested PHP:
{{-- <?php
echo "foo";
echo "bar
?> --}}
Nested Blade:
{{--
{{ HTML::form("foo") }};
{{ HTML::form("bar") }};
--}}
Internals:
Using the sample code from the question, Laravel's Blade Compiler will generate a temporary PHP file containing the following PHP and HTML:
<?php /*
<div class="form-row">
<?php echo Form::label('foo', 'foo:'); ?>
<?php echo Form::text('foo'); ?>
</div>
<div class="form-row">
<?php echo Form::label('foo', 'foo:'); ?>
<?php echo Form::text('foo'); ?>
</div>
<div class="form-row">
<?php echo Form::label('foo', 'foo'); ?>
<?php echo Form::text('foo'); ?>
</div>
*/ ?>
The Blade code inside of the Blade comments are still parsed into PHP. The PHP end tags inside of the PHP block-comment can cause compilation issues:
?> breaks out of PHP mode and returns to HTML mode, and // or #
cannot influence that.
Comments in Blade are very simple!
{{-- Blade comments that wil not appear in the rendered HTML output --}}
You can either do normal PHP comments:
<? /* some comment here */
// or single line comments
# or these :)
?>
I have same problem with laravel 5.1 and PHP 7 (new homestead). The work around was to use this:
<?php /* XXX */?>
instead of this:
{{-- XXX -- }}.
I have a similar symptom and it seems to be related to the length of the comment alone. I tested it with a comment that doesn't contain any PHP code or blade statements at all:
{{--
0123456789abcdef
0123456789abcdef
0123456789abcdef
--}}
I kept adding copies of the repeated line until it crashed. The comment was lexically followed by a blade #if statement, and the corresponding <php if(...): ?> did not end up in the compiled template, but the closing <?php endif; ?> did, resulting in a syntactically invalid compiled template.
It seems to be a bug in the blade compiler and I will report it.
The workaround is to split long blade comments with --}}{{--.
I have Tried the
Nested PHP:
{{-- <?php
echo "foo";
echo "bar";
?> --}}
#TonyArra
While using . It is Not Commenting the Content and prevents from Compiling as HTML
and this is the htmlsource
{{-- foobar --}}
Which i have got
Thats Because If You want To Comment the php Code inside Blade
Try this
<!-- #php echo 'hai'; #endphp -->
OR
<!-- <?php echo 'hai'; ?> -->
and try to view the page source
Blade comments like this one, were the problem in my case:
{{--
#if ($test)
<div>something</div>
#else
<div>something else</div>
#endif
--}}
Simply we have to use a double curly bracket followed by a double hyphen.
This will work for the single line as well multiple lines.
{{-- --}}
Blade Comments
{{-- This comment will not be present in the rendered HTML --}}
Referene: https://laravel-news.com/laravel-blade-comments

How To Put HTML Code In Laravel Function?

I stumbled upon this problem. I am using twitter bootstrap. I generate form elements like this:
{{ Form::button('Save', array('class'=>'btn btn-success')) }}
This is alright. But when I want to put an icon before the 'Save' like this,
{{ Form::button('<i class="icon-ok"></i> Save', array('class'=>;'btn btn-success')) }}
The <i> tag is not interpreted as it should be.Is there any workaround on this? How do I do this?
Any Body Give Some Idea
Thanks in advance.
You can use the HTML::decode method to wrap your button.
Example:
{{ HTML::decode(Form::button('<i class="icon-ok"></i> Save', array('class'=>;'btn btn-success'))) }}
HTML::decode converts entities to HTML characters according to laravel's api, found here:
laravel api
What version of Laravel are you using?
In Laravel 5, Blade has changed so that {{ }} is escaped by default (therefore not rendered as HTML by the browser) and you should use {!! !!} instead.

Categories