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" />
Related
I need to pass a variable from a blade to a vue component, usually I am able to do something like
<vue-component :the-prop="{{ blah }}" />
when passing prop values. However in this case I'm trying to pass a value from $_SESSION, and I am getting an invalid token error and am not sure how to get around it.
I've tried:
<vue-component :the-prop="{{ $_SESSION['data'] }}" />
and
<vue-component :the-prop="{{ session('data') }}" />
and it seems never to even get to the "session" portion and gives me an error:
Invalid expression: unexpected token '{'
the code works fine if i hard-code an id like so:
<vue-component :the-prop="3" />
and typing out v-bind:the-prop makes no difference
If you're doing this from a blade template file, you should remove the colon so that the value is not binded. Blade itself should plug in the variable value from your {{ }} syntax, so there is no binding needed on the vue side of things.
In a component, prefixing an attribute with a colon means you want it handled directly by PHP, as described in the documentation. So instead of this:
<vue-component :the-prop="{{ $_SESSION['data'] }}" />
Try this:
<vue-component :the-prop="$_SESSION['data']" />
Or, even better, this:
<vue-component :the-prop="session('data')" />
I have the following route in routes/web.php
Route::get('search/{model}/{lang}/{id}', ['as'=>'tag','uses'=>'SearchController#tag']);
In the view I have a foreach with the tags:
<li>
<a href="{{ route('tag', ['model'=>'new', 'lang'=>$lang, $tag->id]) }}">
{{$tag_lang->name}}
</a>
</li>
And what I want is that in the browser only appears the "search" url without the parameters model, lang and id (domain.com/search instead of domain.com/search/model/lang/id), but I donĀ“t want to use a form. Is there any way to use a post route without a form or to hide the url? Thanks.
You can make a post look like an anchor:
<style type="text/css">
form.link_mimic {display:inline}
form.link_mimic input {display:inline;padding:0;border-width:0;margin:0;background:none;color:blue}
form.link_mimic input:hover {text-decoration:underline}
</style>
Click <form class="link_mimic" method="post" action="my_url">
<input type="hidden" name="lang" value=" {{$tag->id}}">
<input type="submit" value="{{$tag_lang->name}}">
</form> to change the language.
Give it a go, maybe it is what you are looking for.
I would suggest using a POST route instead as then the parameters will not be visible as querystrings. An example route would be as follows:
Route::post('search', ['as'=>'tag','uses'=>'SearchController#tag']);
You would however need to convert your href into a form.
If you do need to use GET then look at converting query strings using hashing or encryption to obfuscate them from the user and then decode the result in the controller.
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 "." :-)
I just got into Symfony2 as an ASP.NET programmer. I have this very simple form that I want to put in. Since it only contains a label and an input, I figured I didn't want to make a separate form class for it. But as soon as I tried to preform that, I ran into some trouble:
<form action="{{ path('search', [{'src':'bla'}]) }}" method="get">
<label for="src"></label>
<input type="text" name="src" id="src" class="form-control search-input" placeholder="Type a group or song name: ie. Metallica, Fly me to the moon etc.">
</form>
How do I set the path to mydomain.com/search/{src}, I'm trying to at least build a path for now, so I'm using 'bla' as some placeholder valye. Right now from the code above, I receive the following error upon loading the page that contains the form:
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("src") to generate a URL for route "search".") in :home:index.html.twig
Some methods I also tried:
path('search', 'bla') <-- and --> path('search', 'bla', [])
brings error:
An exception has been thrown during the rendering of a template ("Warning: array_replace(): Argument #3 is not an array") in :home:index.html.twig
path('search', ['src':'bla'])
and some more (probably) nonsense that didn't get me close to the result.
Is this even doable, or do I have to do it the 'normal' way with {{ form_start() }} and form classes?
Don't try, just read documentation, it's more effective and faster :)
http://symfony.com/doc/current/book/templating.html#book-templating-pages
{{ path('search', {'src':'bla'}) }}
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('%>');
});