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
Related
I makes website by laravel as multi language
and I make full switches to change the language and it work as the flowing code
<?php
return [
'home_menu'=>'home',
'about_menu'=>'about',
'contact_menu'=>'contact',
'message'=>'<h1> welcoome</h1>',
];
Now I need to show text from contact.blade.php from resource/views Instead of
welcoome.
There are many ways you can show your translations in Laravel
{{ trans(filename.arrayIndex) }}
#lang(filename.arrayIndex)
and in case you have html in your translations then
{!! trans(filename.arrayIndex) !!}
I hope this will help you but you must check all the Laravel documentation before starting the project. It will really help you understanding the Lara Concept better
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 dont have knowledge in laravel Blade and I have this code :
<span v-bind:class="{ 'total': (listing.price_per_week), 'total total-center': (!listing.price_per_week)}">#{{ listing.price_view }}*</span>
I want to pass that price value to this function
<?php echo removeFrom( #{{ listing.price_view }} ); ?>
but it doesnt work this way
how can pass this
Thanks
Please check this out: Blade & JavaScript Frameworks
Since many JavaScript frameworks also use "curly" braces to indicate a
given expression should be displayed in the browser, you may use the #
symbol to inform the Blade rendering engine an expression should
remain untouched. For example:
<h1>Laravel</h1>
Hello, #{{ name }}.
In this example, the # symbol will be removed by Blade; however, {{
name }} expression will remain untouched by the Blade engine, allowing
it to instead be rendered by your JavaScript framework.
I want to show the result when search a keyword successful.
In routes\web.php:
Route::get('tim-kiem', 'Frontend\ListBaiVietController#timkiemBaiViet');
In controller ListBaiVietController, I have a function:
public function timkiemBaiViet() {
$tukhoa = \Request::get('tukhoa');
$ketquatimkiems = Post::where('title','like','%'.$tukhoa.'%')
->orderBy('title')
->paginate(20);
// var_dump($ketquatimkiems);
return view('post/searchresult',compact('ketquatimkiems'));
}
I am using var_dump($ketquatimkiems), it shows 2 results.
In post/index.php I am calling content:
<body>
#yield('content')
</body>
And post/searchresult.php:
#extends('post.index')
#section('content')
#foreach($ketquatimkiems as $ketqua)
<div class="container-artical">
<div class="list-excerpt">
{!! $ketqua->excerpt !!}
</div>
</div>
#endforeach
<nav class="blog-pag">
{{ $ketquatimkiems->links() }}
</nav>
</div>
#endsection
When I am typing text quận 8. It is only showing code, not result.
your file name must have .blade extension
post/searchresult.php: to post/searchresult.blade.php:
post/index.php to post/index.blade.php
for more information
Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file
Ref: https://laravel.com/docs/5.5/blade
you are not using laravel blade engine. Rename view
files like so index.blade.php
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'); }}