Laravel: variable + parameter in view - php

I want to ask if it is possible to do something like this in View in Laravel 5.2:
<p> This is window: {{$element_ + 'window'}} </p>
<p> This is wall: {{$element_ + 'wall'}} </p>
The values for this variables are from $element_window, $element_wall.

There are couple of options.
First - is to use #php block in .blade file for dynamic output:
#php
${'window'} = ${$element_.'window'}
#endphp
Second is to write custom blade extension to output anything you need.
Third is to define custom method in your Model (if you use one).
However I should mention, that such variable assignment inside template (first option) is not recommended. It's hardly readable and could cause Exceptions if such dynamically created variables do not exist at some point. Not saying that this is not presentation logic.

If you want to dynamically name a variable.. you can do the following.
<p> This is window: {{ ${'element_'.'window'} }} </p>
<p> This is wall: {{ ${'element_'.'wall'} }} </p>
That should work.
But if just want to concatenate a string to the variable... you can use "." :-)

Related

How to pass a string to a blade component?

As an example there is this component:
resources/views/components/example.blade.php
<div>
#if($foo === "bar")
Bar
#else
Foo
#endif
</div>
that I render like
#php
$foo = "bar";
#endphp
<x-example :foo="$foo" />
what is working.
But to have my code looking cleaner, I want to pass the string directly to the component, but the following I tried are not working:
<x-example :foo="bar" />
<x-example :foo='bar' />
<x-example :foo="\'bar\'" />
<x-example :foo="'bar'" />
<x-example :foo=""bar"" />
Easy mistake to make, as mentioned in the Laravel documentation on passing data to components;
You may pass data to Blade components using HTML attributes.
Hard-coded, primitive values may be passed to the component using
simple HTML attribute strings. PHP expressions and variables should be
passed to the component via attributes that use the : character as a
prefix
So update how you use the Blade component as below:
<x-example foo="bar" />

Accessing object properties in blade when using table

I have created a form where an Admin can create text and also use objects as used in Blade. I want to store the text in a table and then display it in HTML with the objects working properly.
For example, I would have a form with this input in my view
<div class='form-group'>
<textarea placeholder="" name='comments' type='' rows='10' class='form-control' id='' value = '{{ old('comments') ?? $plansubmission->comments }}'>{{ $plansubmission->comments }}</textarea>
<div>{{ $errors->first('comments') }}</div>
</div>
In that form input, I have entered the following:
Dear Employee, {{ $plansubmission->id }}
This input validates and the input is sent to comments column in the proper table in my database.
Now, I want to return the comments column back into the view with all the spacing that was submitted into the input (therefore, I use the 'pre' tags):
<pre> {{ $plansubmission->comments}} </pre>
The plan text and spacing is maintained but the blade part simply comes out as {{ $plansubmission->id}} instead of what the actual property is.
It's a major security problem to let users submit blade templates to display data. Blade is compiled to PHP so you would be essentially allowing users to execute any PHP code they want. I would recommend you use something like mustache to let users inject variables into the output.
In the controller that passes the data to the view, you can pass the $plansubmission->comments through a mustache parser. This will treat the comments field as a template, and the second parameter sets the variables that the template has access to. This way you can explicitly set what the comment template has access to so you don't let users leak more data than is required.
$m = new Mustache_Engine;
$comments = $m->render($plansubmission->comments, $plansubmission->toArray());
Then users can put something like this in the comments field
Dear Employee, {{ id }}
Then in the view do
<pre> {{ $comments }} </pre>
and it will output
<pre> Dear Employee, 123 </pre>

Using Laravel 5 with AngularJS blade tag conflict

I am trying to setup Angular with Laravel 5.
I have tried doing in appServiceProvider:
public function boot()
{
\Blade::setRawTags("[[", "]]");
\Blade::setContentTags('<%', '%>'); // for variables and all things Blade
\Blade::setEscapedContentTags('<%%', '%%>'); // for escaped data
}
With:
<div>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<h1>Hello, {{ yourName }}!</h1>
</div>
But I'm getting:
Use of undefined constant yourName - assumed 'yourName'...
The easiest way to do this is to simply use # in front of your Angular code:
<div>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<h1>Hello, #{{ yourName }}!</h1>
</div>
source
When doing changes that have to do with Blade (Extending Blade, changing the tags etc) always make sure to delete the cached views.
They're located in storage/framework/views.
Just delete all files (except the .gitignore)
If you want something a bit more practical you can create a command to do that. Like this one
Laravel >= 5.3
While adding # in front of the braces does still work, Laravel has also included another handy blade utility that lets you mark an entire block as-is:
#verbatim
<div>
{{ ctl.variable1 }}
{{ ctl.variable2 }}
</div>
#endverbatim
Particularly helpful if you're not doing much with the Blade template rendering
Else you modify angular syntax...
var sampleApp = angular.module('sampleApp', [], function($interpolateProvider) {
$interpolateProvider.startSymbol('<%');
$interpolateProvider.endSymbol('%>');
});

Laravel Form methods VS traditional coding

I am currently learning Laravel and finding it really useful and interesting.
At the moment I am making a simple online application form.
What are the biggest advantages to doing things using the Laravel syntax like:
{{ Form::open(array('url' => 'foo/bar')) }}
As opposed to simply:
<form action="foo/bar">
Or:
echo Form::text('username');
Instead of:
<input type="text" name="username" />
The Laravel way must be better, I just wish to know why exactly?
Using built-in HTML helpers have many benefits:
Using Form::open you add CSRF protection input hidden (by default)
Using form elements (inputs/textarea etc.) and withInput method for Redirection allows you to easily fill in the form with the same data with almost no coding
If you use Redirect::route('form'->withInput(); and have input
text {{Form::text('username')}} it will automatically set input's value the old data - you don't need to code it yourself checking it
Also if you want to match fields with labels its much easier:
{{ Form::label('username', 'Enter username') }}
{{ Form::text('username') }}
it will generate the following code:
<label for="username">Enter username</label>
<input name="username" type="text" id="username">
so as you see id will be created automatically
Probably there are some more. However the main disadvantage is that you need to learn and it's not portable in case you want to move your site to other Framework but each solution has pros and cons.
There are so many advantages of using Laravel's Form component but one useful advantage is that, when you just use this:
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::close() }}
It automatically appends a hidden _token field which is useful for CSRF protection. otherwise you have to manually create the _token field using echo Form::token() or other way maybe. Also, when you use RESTful routes then Laravel's Form component appends the corresponding hidden _method field as well. Following note is taken from Laravel website:
Note: Since HTML forms only support POST and GET, PUT and DELETE
methods will be spoofed by automatically adding a _method hidden field
to your form.
There are also other advantages like Form Model Binding, generating form elements (specially select) easily and many more. Read more about Form on documentation.
BTW, the Redirect::back()->withInput() doesn't deppend only on use of Form component, if you use something like this, for example:
<input type='text' name='username' value='<?php echo Input::old('username') ?>' />
This will still work, the field will be repopulated on redirect back with inputs.

Laravel get route with parameter in view

I need to get the route in my view for redirecting.
Right now I'm doing this:
Laravel 4 - Get Current Route Name on Hidden Input to use for search
{{ Form::hidden('route', Route::current()->getUri()) }}
Problem is, it looks like this when I get on a page with an id:
<input name="route" type="hidden" value="recipes/details/{id}">
How can I parse the {id} variable?
You should use:
Request::url();
Instead of Route::current()->getUri(), but it's not proper way to redirect from the View, you should redirect from your Controller instead.
It should be in your case (for full url):
// 'http://example.com/recipes/details/10'
{{ Form::hidden('route', Request::url()) }}
or use this (only for path):
// 'recipes/details/10'
{{ Form::hidden('route', Request::path()) }}

Categories