hi sorry for my language.
im using laravel 5.6 to create a simple article site.
i have a problem with show article comments as string. the problem is : laravel try to render php codes and the page give me some errors.
imagine a user wants to send his laravel code as comment to me:
(this is just an example)
#foreach($article->tags as $tag)
<a class="tags" href="/tags/{{ $tag->slug }}">{{ $tag->title }}</a>
#endforeach
if user send this codes as comment to me, my page not loading and give me errors
my code to show comments :
{!!$comment->body !!}
help me please . thank you
Replace {!!$comment->body !!} with {{$comment->body }}.
When you use {!! !!}}, the template engine tries to parse the contents within the braces. Use {{ }} so that the contents are displayed as it is escaping all html.
i found the problem :
problem is for laravel blade codes.
like: #foreach . #if . #auth and ....
i have to scape this codes with space :
# foreach . # if and ....
Related
I have a mcqs website, the problem is that when i try to load a quiz page with multiple questions and their answers, it loads fine.
Working code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{{$answer->content}}
</span>
#endforeach
Not Working Code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{!! $answer->content !!}
</span>
#endforeach
{!! $answer->content !!} is the same as echo $answer->content
however for the same code when i try to render the answers with HTML formatting using <?php echo , the page stops rendering midway. forexample if say the page was to load 30 questions , it will only load 10 and stops rendering . Even when i tried using {!!$answer->content!!} , the problem remains the same.
Note: i am returning set of questions using the below code:
$questions = Question::all()->where('exam_id',$exam_id)->random($questionsCount)->shuffle();
return view('quiz',compact('exam','questions','questionsCount','t'));
I have one to many relationship setup so i can extract the answers of the question using $question->answers
it works fine when i don't echo any answer content.
Anyhelp will be greatly appreciated. Thanks!
Looks like an issue with unescaped text. When you use the blade template's escape method ({{ }}) it is successful. When you use the unescaped method, ({!! !!}) it fails. This is the same when using the unescaped php echo.
There is likely some type of character in one of your questions that is causing a break of the loop or php echo.
To fix, use the blade escape {{ }} or php escape (htmlentities(), htmlspecialchars() etc) in the echo.
htmlentities-vs-htmlspecialchars is worth checking out as well.
I am trying to encode my Laravel project but unfortunately, Laravel blade templates are not pure PHP .. so the ioncube encoder/reader is unable to encode it properly.
I have tried these ways mentioned here and here, but my views files are not encoded fully ..or not working the way I want (or I have not understood it properly).
so can anyone please help me and tell me to step by step and clear.
These are some of my files inside the blade.php files which are not encodable.
#php
// alignment direction according to language
$dir = "ltr";
$rtlLang = ['ar'];
if(in_array(getOption('language'),$rtlLang)):
$dir="rtl";
endif;
#endphp
{!! getOption('home_page_meta') !!}
<title>#yield('title')</title>
#endif
{{ csrf_field() }}
{{ getOption('currency_symbol') . number_format(Auth::user()->funds,2, getOption('currency_separator'), '') }}
Finally I got an idea and its worked (no one suggested)
you can easily encode your blade by its original code like an example : {{ getOption() }} to <?php echo e(getOption()); ?> . and #if as <php if; ?>
and #section('title', getOption('app_name') . ' - Login') as <?php $__env->startSection('title', getOption('app_name') . ' - login'); ?> and like so. And now you can encoded any blade templates files or laravel projects.
Hope this helpful. Now i have saved my templates file from thief.
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.
I am new to Laravel so my problem is that I am trying to add multiple script files to my blade.php page using this code:
{{
HTML::script('js/bootstrap.min.js');
HTML::script('js/Chart.js');
}}
without any results , am I doing anything wrong or misunderstood some concept, please specify the best way to achieve my goal
only first include is working, the second one is not including
Thanks
You can't have line breaks inside Blade tags (at least not in Laravel 3). What you need to do is to add {{ ... }} for every HTML:: you have.
{{ HTML::script('js/bootstrap.min.js'); }}
{{ HTML::script('js/Chart.js'); }}
I am really new to Laravel as well as PHP.
I have some PHP code that I want to put it in Laravel.
But when I check the .blade files there are no PHP codes contained in them.
So, my question is: When can I put my PHP code in a Laravel application.
The PHP code relates to single.blade.
Thank you for your guidance.
Blade is the Laravel template, you should not put your code inside the blade file, it is not a good practice (not recommended), just things you are going to show on the final page.
You should put your code inside the controller.
So if you want to pass things to Blade you should do like:
View::make('single', array('var1' => $var1, 'var2' => $var2));
Everything between {{ }} is tranformed to an echo by Blade, so you can use any PHP code like this {{ date('d/m/y') }} on your Blade files.
So with this example above you should do {{ $var1 }} on your Blade file.
But with Blade you have flow control:
If-else:
#if ($var1 == $var2)
<p> equal </p>
#else
<p> not equal </p>
#endif
For-each:
#foreach($vars as $var)
<p>{{ $var }}</p>
#endforeach
For:
#for($i=0 ; $i<999 ; $i++)
<p>Number: {{ $i }}</p>
#endfor
While:
#while(isTrue($var))
<p>Loop forever</p>
#endwhile
Unless:
#unless(isRunning())
<p>keep</p>
#endunless
With this you can control what is printed on screen and you can use Laravel code like this (this code is adding an HTML class if the current route is equal to 'getFoo' to the element LI):
<li #if(URL::current() == URL::route('getFoo'))class="active"#endif>
This is a good start on how to use Blade. http://laravel.com/docs/templates