laravel & blade templating #section error - laravel

I want to display full name as title:
#section('title', ['title' => $userdetail->fullName])
I have used the above code and it shows the error exception of string to object conversion.

modify your #section to this
#section('title', {{$userdetail->fullName}})

#section('title', $userdetail->fullName)
You don't need to specify it as an associative array, just put variable and section will be the value of this variable

Do like this --
#section('title', "$userdetail->fullName")
Wrap the second parameter inside double quotes since it renders it automatically renders a variable and normal text, whereas single quotes only render text and not variable.
And remember that you cannot use blade echo {{ }} inside a blade syntax.
like - #section('title', {{ $userdetail->fullName }})

Related

How to get route parameter from blade to vue

I have a route:
Route::get('/{my_parameter}/home', function($my_parameter) {
return view('home_view')->with('my_parameter', $my_parameter);
}
In my home_view.blade.php file:
<div>
{{$my_parameter}} {{-- displays properly --}}
<vue-component #click="doSomething( {{$my_parameter}} )">Click me</vue-component> {{-- does not work--}}
</div>
I've tried numerous variants as suggested in my searches, including #{{$my_parameter}}. When I use a hard-coded string, #click=doSomething('my_value'), the function works properly.
How do I successfully get a route parameter from blade for use in a vue component? Thank you.
I think you just forgot to add quotes around the parameter. If you have doSomething({{ $my_parameter }}) and the parameter is broccoli, it's going to turn into doSomething(broccoli) rather than doSomething('broccoli').

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

Blade templating conditional encoding apostrophes

Using Lumen 5.4.5.
I'm attempting to use a Blade templating conditional to only display a value if it is populated. Here is my syntax:
{{ ($board->a1 != '') ? "a1: '$board->a1'" : '' }}
Should render like this in browser:
a1: 'wR'
Instead it's rendering the HTML encoding string like this:
a1: 'wR'
How can I successfully render the uncoded apostrophes in my Blade conditional?
You can avoid the the call to htmlspecialchars automatically done with {{ }} using {!! !!} instead. https://laravel.com/docs/5.4/blade

Finding a blade variable inside string

I'm passing a $emailtemplate variable into a view.
This $emailtemplate is a model with the following properties
(string) 'from_name', (string) 'from_email', (string) 'subject', (string) 'body', (string) 'email_type', (integer) 'status'
User's can create new instances of the $emailtemplate via a FORM on the site, where they can populate each of the fields above.
An example of the body as populated by users into the FORM would be exactly the following line (yes users will be writing HTML code into the form to be stored in the body property of the $emailtemplate):
<strong>Dear {{$user->first_name}} </strong>
I have created a view which will allow users to 'Preview' the email. The variables $emailtemplate and $user are both passed to the view (for the case of the preview the $user = Auth::user())
resources/views/emails/preview.blade.php:
{!! $emailtemplate->body !}}
This 'preview' view correctly renders the strong styling however does not identify the blade variable reference as it is stored within a literal string.
i.e. the HTML that is rendered is exactly
Dear {{$user->first_name}}
What is going wrong here? I want to allow Users to develop their own email templates via a form. I am passing both the $emailtemplate and $user into the view however because the $emailtemplate->body is a string datatype Laravel does not recognise the use of the blade variable between the parenthesis.
How can I fix this?
Thanks for your help
You can't do this since Laravel wont Re-render the blade output, to see and replace any variables that is injected by another variable!
Take a look at https://github.com/TerrePorter/StringBladeCompiler
So, you should evaluate the user defined template first, e.g.
<strong>Dear {{$user->first_name}} </strong>
will become:
<strong>Dear Foo </strong>
This evaluated <strong>Dear Foo </strong> will be stored in $evaluatedTemplate
Then you can use this in your main blade template:
{!! $evaluatedTemplate !!}
Also see:
https://github.com/Flynsarmy/laravel-db-blade-compiler

Laravel blade #include into a Javascript variable?

In a Laravel 5 blade template I have a <script> section.
In this section I need to set a variable to a string that is coming from another blade template.
I tried something like :
<script>
var a = "#include 'sometext.blade.php' ";
</script>
but this obviously doesn't work.
Also, the included blade can contain both single and double quotes, so these need to be escaped somehow, or the Javascript will not be valid.
Any ideas?
Ended up needing similar functionality when working with DataTables, and the additional actions HTML needs to be injected after the fact via jQuery.
First I created a helper function with this (from Pass a PHP string to a JavaScript variable (and escape newlines)):
function includeAsJsString($template)
{
$string = view($template);
return str_replace("\n", '\n', str_replace('"', '\"', addcslashes(str_replace("\r", '', (string)$string), "\0..\37'\\")));
}
Then in your template you could do something like:
$('div.datatable-toolbar').html("{!! includeAsJsString('sometext') !!}");
to include the sometext.blade.php blade template with escaping of quotes and removal of newlines.

Resources