In a Hugo partial template, how do I access secondary/additional parameters? - go

Say I have {{ partial "li.html" $test $root.Data.Term }}.
With this I can can access the first parameter, or $test, by simply refering to . within the li.html template, but how do I access the second or additional parameter ($root.Data.Term) from within the same template?

I would suggest using the hugo dict function. It allows you to use key/value pairs to pass information. The documentation states that for your use case.
{{ partial "yourPartial" (dict "test" "yourTestData" "term" "yourTerm") }}
You can then access the values by just using {{ .test }} and {{ .term }}.
Alternatively you can use the scratch function, which is a more "global" approach.

Related

Laravel resource give extra parameter to route

I'm trying to access function in controller using wild cards in route through get call.
The route is defined on this way:
Route::get('/somefunc/{alias1}/{alias2}', 'uses'=>'MyController#myfunction']);
The route link I'm trying to access is defined here:
{{ __('Click') }}
But when I click on the link it gives me extra parameter in the route:
items/somefunc/somealiashere/1
because of the previous resource define in the web.php.
How to skip that 'items' parameter in the route.
Thank you.
Try to use url()
{{ __('Click') }}
Another way I recommend you is give name route.
Route::get('/somefunc/{alias1}/{alias2}', 'uses'=>'MyController#myfunction'])->name('somefunc');
and call it below.
{{ __('Click') }}

How to pass Vue.js variable to Laravel Blade route helper

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 }}

In a Form Model Binding form, how do I access the variable without using a helper?

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

Twig render with vars

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') %}

pyrocms files plugin variables

I want to pull a url segment variable into the pyrocms file plugin call. it would look like
{{files:listing folder="[segment(2)]"}}
or something of the sort. What is the trick for embedding
{{url:segments..}}
inside
{{files:listing folder="…}}
I am trying to setup this up for a conditional query for a photo gallery
If you take a look at the PyroCMS Tags documentation you will see this clearly documented in the "Tag Attributes" section.
You may also use the output from other tags as attribute values in your tags. For example if you wanted the url segment to default to the slug of the currently viewed page you could do this:
{{ url:segments segment="1" default=page:slug }}
Here is an example showing the proper use of quotes and braces when the tag used as the attribute value has an attribute itself.
{{ url:segments segment="1" default={foo:bar value="baz"} }}
Tip: Omit quotes and braces when using tags as attribute values. The only exception is when the tag you are using as the attribute value has its own attributes.
So you can do that easily with:
{{ files:listing folder={url:segments segment="2"} }}
Basically you don't need to pretend it's a string if it's not. You can just send the foo:bar arguments through, but it if has attributes you can "group" the attributes with the call via a single { and }.
Simple right? :)

Resources