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
Related
I'm using PHP Laravel framework and I came to some code examples where {{ }} is use inside a html code, like this:
<link rel="stylesheet" href=" {{ URL::to('css/app.css') }} ">
My conclusion is that the {{ }} are used to write no-HTML code inside the HTML, is that correct?
And for what is the { } used?
Thanks for your answer.
There is no { } in Blade, {{ }} displays escaped data and {!! !!} displays unescaped data.
By default, Blade {{ }} statements are automatically sent through
PHP's htmlentities function to prevent XSS attacks. If you do not want
your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}.
https://laravel.com/docs/5.3/blade#displaying-data
That is not php but rather syntax for the blade template system that laravel comes bundeled with.
In short, {{$aPhpVariable}} is basically compiled to <?= htmlentities($aPhpVariable) ?> (or even <?php echo htmlentities($aPhpVariable); ?>), but from what I know, there is no single bracket ({}) syntax.
You can also use normal php code inside blade templates or just treat it as a normal html page, but it does have a bunch of things that makes building the views a lot easier.
Go check out the docs for more info about blade!
To escape data use
{{ $data }}
If you don't want the data to be escaped use :
{!! $data !!}
{} is part of the syntax of PHP code. It's used in functions, blocks of code and objects.
{{ }} it part of Laravel's Blade template syntax, echoing something in a Laravel project.
In my blade code, I need a counter, to give divs that are rendered by a foreach loop unique id's. For that purpose I created a variable in my blade template like this:
{{ $counter = 0 }}
I use it in the html by just outputting it with {{ $counter = 0 }}
and the later on, I increment it like this: {{ $counter++ }}
It all works like a charm, except that at {{ $counter++ }} it's not only incrementing the variable, it's also outputting it to the view.
is there any way to prevent this?
At first adding logic on blade templating is a bad practice, but sometimes we are forced to do, in that case you can just use PHP tags for it like this:
<?php $counter++; ?>
Another way to do it on Laravel 5.4 as docs indicate:
In some situations, it's useful to embed PHP code into your views. You
can use the Blade #php directive to execute a block of plain PHP
within your template:
#php
$counter++;
#endphp
If you have your positions on the array keys you can use it on the #foreach like this:
#foreach ($variable as $key => $value)
//current position = $key
# code...
#endforeach
I use this extension do set variables on blade:
https://github.com/RobinRadic/blade-extensions
You can use simple as #set($count, 1) or any value you want.
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
A couple of days ago, I decided to start using laravel for the next project, but I'm confused as I don't find the documentation very compelling and I'm still a laravel beginner .
So, I didn't find a solution for how to create a layout using PHP (and not built in blade templating engine).
How can I do that? What's the best way to organize layouts in a big project?
Thank you
There are many methodologies that deal with handling templates.
Here are few,
1. Using a regular include or require
You can include the header.php , sidebar.php and footer.php and as many files that you prefer for each sector(It depends on the size of the template)
2. Using a common file and having classes inside it
Include a single file and call the classes to render each area
like
class Head {
public function render($_page, $_data) {
extract($_data);
include($_page);
}
}
3. Use a Templating Engine
You shall prefer few templating engine like smart, raintpl etc., (I guess you don't prefer it ;) )
4. Acquiring by inc
You can include as suggested here
<html>
<head>
<title><?=$this->title</title>
</head>
<body>Hey <?=$this->name?></body>
</html>
And the php area would be
$view = new Template();
$view->title="Hello World app";
$view->properties['name'] = "Jude";
echo $view->render('hello.inc');
5. By having template segments in db
Believe me, I saw many good sites which stores the template in the database and it will be rendered each time. It might look like strange idea, but even i tried it for one of my project.
Conclusion :
But if i use Laravel, for sure i will prefer the Blading Tempalte Engine and I recommend you the same.
Update :
Few benefits of Using Blade Templates
1. Easy Setting of attributes
Set the attributes on the go
<title>App Name - #yield('title')</title>
2. Easy yielding
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
3. Simple echoing
Like this
Hello, {{ $name }}
4. Easy Condition
Like this
{{ isset($name) ? $name : 'Default' }}
5. Never Escape
Like this
Hello, {!! $name !!}.
6. Beautiful If Statements
I prefer this way to make my code more beautiful
#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif
7. Checking Authentication
The simplest way to check the authentication
#unless (Auth::check())
You are not signed in.
#endunless
8. Easy For Loop
How this for loop looks like
#for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
#endfor
9. Awesome foreach statement
Splitting the key and value can't be more easy than this
#foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
10. Include the files
How about include file like this
#include('view.name')
11. Passing parameters to views
Can Pass this array to your view
#include('view.name', ['some' => 'data'])
Source : Laravel Templates
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'); }}