I'm using Silex, and need to alter a Twig file based on the location its loaded from.
My first thought was checking the url, but unfortunately this returns the location of the rendered Twig location, and not the parent url as expected.
{{ path(app.request.attributes.get('_route')) }}
Another attempt was to pass some vars with the render, however I'm not certain how to do this from within Twig (and if it's possible at all).
{{ render(path('cart')) }}
Can someone possible help me with a solution?
Thanks in advance
This is indeed a problem with silex core. I have submitted a pull request that introduces a new renderRoute twig function that should fix the issue. Hopefully it will be merged soon.
Well i didn't get perfectly what are you asking, but if you want to pass variables in render method you can do this:
{% render "ProjectTesteBundle::ControllerName::actionName" with { 'variableNameToPass' : variableValue } %}
if you want get route name in twig template you can do do something like this:
{% set routeName = app.request.attributes.get('_route') %}
Related
For example, I have a link to an other post like this in my template
My post
I want to be able to use the post object within the route helper to use the id.
Is there any syntax for this use case?
Or is there an other way of doing this?
Actually I had similar problems to solve this kind of cases. Your question related with Vue exactly, but the method below you can use for your case as well. Anyway you can't execute JS and PHP at the same time, cuz they're working at different sides. But as I also liked to have all routes with their aliases, I thought this approach.. You can imitate something like this:
Route::get('/', 'PostController#all')->name('all'); // all posts page
Route::get('post', 'PostController#all')->name('all_page'); // THIS IS THE THING (one additional route), WHICH WILL TAKE RESPONSIBILITY ON CASE, WHEN post_id WILL BE EMPTY
Route::get('post/{post_id}', 'PostController#post')->name('post');
This method will allow you to use the 1st and 3rd routes as normally, and as mixed too in the different places on your app like this:
{{ route('post.all') }}
{{ route('post', ['post' => $post_id]) }}
{{ route('post') }}/#{{ post.id }}
In scripts you can implement the approach like this:
let someUrl = "{{ route('post') }}/" + postObj.id;
In the view you can implement the method like this:
My post #{{ post.id }}
I am building a form using Form Model Binding via the Laravel Collective HTML package. The documentation (https://laravelcollective.com/docs/5.2/html#form-model-binding) boasts how the model's value is conveniently available with the value being set in the priority order of:
Session Flash Data (Old Input)
Explicitly Passed Value
Model Attribute Data
This is super useful because if a user has changed multiple fields, but 1 of them fails validation, you don't want all the other fields being reset to original value when they get thrown back to the form page with a helpful Message Bag of errors to give them a chance to correct their invalid input.
It's all very well using the Form::label, Form::text and Form::select helpers to leverage this lovely feature but what if you just want to access that convenient variable directly to do something a bit left-field?
Here's what I have tried...
{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
{{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
{{ Form::text('first_name') }}
{{ Form::value('first_name') }} <-- No
{{ Form::session('first_name') }} <-- Nope
{{ Form::input('first_name') }} <-- Still no
{{ Form::attribute('first_name') }} <-- Absolutely not
{{ Form::close() }}
Help me, what is the method I am looking for?
The documentation hints at the ability to get the thing I want here: https://laravelcollective.com/docs/5.2/html#custom-macros where it says it's "easy" to define your own custom Form class helpers called "macros" but it doesn't actually tell you where you would write the code for this or where in the code you can look if you want to poke around and learn more about the FormBuilder class?
The method for doing this is Form::getValueAttribute('first_name') and the location for poking around to learn more is /vendor/laravelcollective/html/src/FormBuilder.php
I am having a problem passing a property using Vuejs ~1.0 to a child component from a Laravel Blade template. If I pass something in plain text it works just fine, but when I try to pass a js property, array, or object it doesn't work at all. I currently have a blade file with a custom component which looks like this:
<my-component video="#{{ stuff }}"></my-component>
If I leave out the #{{ }} the only thing that will be passed is the string stuff, and if I leave out the #, I obviously get a blade error, but if I use #{{ stuff }}, then all I get is the string {{ stuff }}. I'm obviously missing something, but can't tell where I'm going wrong. Thanks in advance.
Look like I just figured it out, it seems that I was missing the colon before video, so it should have appeared like so:
<my-component :video="stuff"></my-component>
If you are passing a variable to the component, then use:
<my-component :video= "{{ json_encode($stuff) }}" ></my-component>
Don't forget the double quotes or the result would be unpredictable for things like objects.
If you are passing model then do this:
<my-component :video="{{ $stuff->toJson() }}" inline-template></my-component>
Is it possible to offload frontmatter to an include in some manner? My Jekyll site uses a frontmatter variable for the page title, however I have some pages that share an include due to code repetition. Putting the frontmatter for the page title in the include treats it like raw HTML. Is there a way to simulate this and set variables in an include?
According to the Jekyll docs on passing parameter variables to includes, you need to capture your front-matter as a variable first.
Their example uses capture, but you can use assign if you're just passing the variable through. Here's my tested example of how to do this:
include
<div>
{{ include.title }}
</div>
include in content
{% assign include-title = page.title %}
{% include example.html title=include-title %}
There is no front matter in includes.
But you can reach page's title from inside an include by using page.title, and any other page's variable with page.myVarName.
You can pass a parameter to an include. Try:
{% include name.html var={{page.title}} %}
Then in the include, access the parameter using include.var.
See "Pass Parameters to Includes" at https://jekyllrb.com/docs/includes/
I have a Django forms dilemma. When rendering a form in a template, I usually prefer to print the form fields individually, in stead of using the form.as_p annotation. However, in the form I am working on I have a set of fields that are repeated an unknown number of times. For example, if there are 3 items being edited the fields would be:
field_name_1
field_email_1
field_name_2
field_email_2
field_name_3
field_email_3
But the number of items in not known to me beforehand. To solve this I am creating the fields in a loop in the init function, no problems there. But I can't figure out a way to print the fields individually in the template, since I don't know the whole name of each field. If this were my only problem I could just use the form.as_p syntax, no biggie. But I would also like to add some html to the beginning of each set of fields, so that the result would be:
Add values 1:
field_name_1
field_email_1
Add values 2:
field_name_2
field_email_2
Add values 3:
field_name_3
field_email_3
However, I haven't found a way to insert arbitrary html into a form in Django code, similar to #markup in Drupal forms. So:
Is there a way to print the fields individually so I can put the extra html in the template? (What I am missing here is the exact syntax)
If not, how do I insert the extra html into the form?
Do you mean something like this in the template?:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
(from the Django documentation: https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-hidden-and-visible-fields )
You could render the header with something like:
Add values {{forloop.counter}}: