Laravel language handler in templates blade - php

How can i use something like {{#something}} and it will run a controller that checks for "something" so i can return it to translteable text?
My current blade template looks like following:
#layout("layouts.default")
#section("inner")
<h1>Velkommen til pornobiksen</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
I mean, how can i change the "Velkommen til pornobiksen" tekst? I know i can make something like
View::make("template")->with("h1_text","Velkommen til pornobiksen");
But is there not a module/plugin to make it easier? By making like {{#h1_text}} and it will takes from my database or something?
What is the easists way to make this?

You need to use {{ $h1_text }} to put the variable into your blade template.
#layout("layouts.default")
#section("inner")
<h1>{{ $h1_text }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
EDIT
I think I misunderstood you, it seems you are looking for localization
#layout("layouts.default")
#section("inner")
<h1>{{ Lang::get('messages.welcome') }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection

For localization , you can use helper function : trans
home.php
return [
'welcome' => 'Velkommen til pornobiksen'
];
View
#layout("layouts.default")
#section("inner")
<h1>{{trans('home.welcome')}}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection

Related

Laravel - bug with echoing var in view

This code outputs exactly what I want to type on page
{{ dd($users->where('id', $user_id)->first()->avatar) }}
So exactly:
It looks like:
<div class="col-lg-1 section-count text-center my-auto small-text p-margin0">
<b>{{ $topic->views }}</b>
<p>WYŚWIETLEŃ {{ dd($users->where('id', $user_id)->first()->avatar) }}
</div>
But when I remove laravel's "dd" to look it like:
<p>WYŚWIETLEŃ {{ $users->where('id', $user_id)->first()->avatar }}
Suddenly... there is crash splash bum error...
Before I tried to do it even like:
<p>WYŚWIETLEŃ {{ dd($users->where('id', $user_id)->values()[0]->avatar) }}
Works again.
On Page
Okay, so let's delete dd :)!
<p>WYŚWIETLEŃ {{ $users->where('id', $user_id)->values()[0]->avatar }}
crash splash bum error... :<
Now this;
What is this??? It's everything correct when I dump it by dd, and without errors :<.
edit
$users var is collection, looks like this
Okay the first thing you need to do, is that you have to make a check if the user actually exists, so always before calling this, make something like:
#if($user = $users->where('id', $user_id)->first())
<p>WYŚWIETLEŃ {{ $user->avatar }}</p>
#endif
Otherwise, would be good if you can check before this what is this query actually giving you ( might give you null when you use this on collection and not Builder ), so like this:
{{ dd($users->where('id', $user_id)->first()) }}

Laravel get div without ajax

Hi I am trying to get the content within a div element that also happens to be within a form into my controller. I dont want to use ajax. How may I get that done ?
<div id="editorcontents" name="editorcontents">
</div>
Then in controller
Use Input;
$content = Input::get('editorcontents');
In your controller, do something like this. Look up the correct way in the docs (https://laravel.com/docs/5.4/eloquent#retrieving-models). For example, if you want ALL input, you would do Input::all();, instead of Input::where('editorcontents')->get();
public function index() {
$content = Input::where('editorcontents')->get();
return view('your_view.blade.file', compact('content'));
}
Then in your view your would now have $content, that you passed from your controller.
start of by looking at it, add this at top of your view: {{ dd($content) }}. This will die dump $content.
Go ahead and remove that line and do something like (docs here https://laravel.com/docs/5.4/blade#loops):
<div id="editorcontents" name="editorcontents">
#forelse ($content as $value)
<li>{{ $value->body }}</li>
#empty
<p>No content</p>
#endforelse
</div>

How do I use the POST of Laravel's Route::resource?

Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}

How to include a controller method's view as a part of another controller view in Laravel PHP Framework

Halo here is my need;
i want to include different view as apart of different view in laravel php frame work.
class DashboardController extends BaseController {
public function comments( $level_1=''){
// process data according to $lavel_1
return View::make('dashboard.comments', $array_of_all_comments);
}
public function replys( $level_2=''){
// process data according to $lavel_1
return View::make('dashboard.replys', $array_of_all_replys);
}
these both data can now accessed from
www.abc.com/dashboard/comments
www.abc.com/dashboard/replys
And in my view what i need is to generate replys according to the comments id ($lavel_2)
// dashboard/comments.blade.php
#extends('layout.main')
#section('content')
#foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//here is what i need to load reply according to the current data;
//need to do something like this below
#include('dashboard.replys', $comment->lavel_2) //<--just for demo
.................
#stop
and in replys also got
#extends('layout.main')
#section('content')
// dashboard/replys.blade.php
#foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
#stop
is there any way i can achieve this on laravel 4?
Please help me i wanted to load both comments and replays in one go and later need to access them individually via ajax also
please help me thank you very much in advance
halo i found the solution here
all we need is to use App::make('DashboardController')->reply();
and remove all #extends and #sections from including view file
the change are like this
// dashboard/comments.blade.php
#extends('layout.main')
#section('content')
#foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//<-- here is the hack to include them
{{-- */echo App::make('DashboardController')->reply($comment->lavel_2);/* --}}
.................
#stop
.............
and in replys is now changed to
// dashboard/replys.blade.php
#foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
#endforeach
-------------
thanks
You probably want to rework your views and normalise your data. Comments and replies are (probably) the same.
If you make a Comment model which belongsTo "parent" (another Comment model) and hasMany "children" (many Comment models), then simply set parent_id to 0 for a top-level Comment, and set it to the ID of another Comment to make it a Reply.
Then your Blade views do something like:
comments.blade.php
#foreach ($comments AS $comment)
#include( 'comment', [ 'comment' => $comment ] )
#endforeach
comment.blade.php
<div>
<p>{{{ $comment->message }}}</p>
#if( $comment->children->count() )
<ul>
#foreach( $comment->children AS $comment )
<li>
#include( 'comment', [ 'comment' => $comment ] )
</li>
#endforeach
</ul>
#endif
</div>

Laravel - Check if #yield empty or not

Is it possible to check into a blade view if #yield have content or not?
I am trying to assign the page titles in the views:
#section("title", "hi world")
So I would like to check in the main layout view... something like:
<title> Sitename.com {{ #yield('title') ? ' - '.#yield('title') : '' }} </title>
For those looking on it now (2018+), you can use :
#hasSection('name')
#yield('name')
#endif
See : https://laravel.com/docs/5.6/blade#control-structures
In Laravel 5 we now have a hasSection method we can call on a View facade.
You can use View::hasSection to check if #yeild is empty or not:
<title>
#if(View::hasSection('title'))
#yield('title')
#else
Static Website Title Here
#endif
</title>
This conditional is checking if a section with the name of title was set in our view.
Tip: I see a lot of new artisans set up their title sections like this:
#section('title')
Your Title Here
#stop
but you can simplify this by just passing in a default value as the second argument:
#section('title', 'Your Title Here')
The hasSectionmethod was added April 15, 2015.
There is probably a prettier way to do this. But this does the trick.
#if (trim($__env->yieldContent('title')))
<h1>#yield('title')</h1>
#endif
Given from the docs:
#yield('section', 'Default Content');
Type in your main layout e.g. "app.blade.php", "main.blade.php", or "master.blade.php"
<title>{{ config('app.name') }} - #yield('title', 'Otherwise, DEFAULT here')</title>
And in the specific view page (blade file) type as follows:
#section('title')
My custom title for a specific page
#endsection
#hasSection('content')
#yield('content')
#else
\\Something else
#endif
see "Section Directives" in If Statements - Laravel docs
You can simply check if the section exists:
if (isset($__env->getSections()['title'])) {
#yield('title');
}
And you can even go a step further and pack this little piece of code into a Blade extension: http://laravel.com/docs/templates#extending-blade
Complete simple answer
<title> Sitename.com #hasSection('title') - #yield('title') #endif </title>
I have a similar problem with the solution:
#section('bar', '')
#hasSection('bar')
<div>#yield('bar')</div>
#endif
//Output
<div></div>
The result will be the empty <div></div>
Now, my suggestion, to fix this, is
#if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
<div>#yield('bar')</div>
#endif
New in Laravel 7.x -- sectionMissing():
#hasSection('name')
#yield('name')
#else
#yield('alternative')
#endif
Check if section is missing:
#sectionMissing('name')
#yield('alternative')
#endif
#if (View::hasSection('my_section'))
<!--Do something-->
#endif
Use View::hasSection to check if a section is defined and View::getSection to get the section contents without using the #yield Blade directive.
<title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>
I don't think you can, but you have options, like using a view composer to always provide a $title to your views:
View::composer('*', function($view)
{
$title = Config::get('app.title');
$view->with('title', $title ? " - $title" : '');
});
why not pass the title as a variable View::make('home')->with('title', 'Your Title') this will make your title available in $title
Can you not do:
layout.blade.php
<title> Sitename.com #section("title") Default #show </title>
And in subtemplate.blade.php:
#extends("layout")
#section("title") My new title #stop
The way to check is to not use the shortcut '#' but to use the long form: Section.
<?php
$title = Section::yield('title');
if(empty($title))
{
$title = 'EMPTY';
}
echo '<h1>' . $title . '</h1>';
?>
Building on Collin Jame's answer, if it is not obvious, I would recommend something like this:
<title>
{{ Config::get('site.title') }}
#if (trim($__env->yieldContent('title')))
- #yield('title')
#endif
</title>
Sometimes you have an enclosing code, which you only want to have included in that section is not empty. For this problem I just found this solution:
#if (filled(View::yieldContent('sub-title')))
<h2>#yield('sub-title')</h2>
#endif
The title H2 gets only displayed it the section really contains any value. Otherwise it won't be printed...

Categories