I am converting the PHP based template to TWIG based template in FuelPHP.
echo Html::anchor('categories/create', 'Add', array('class' => 'btn'));
I found out with some help that the equivalent tag for TWIG is html_anchor which works fine. But as in this case, there is an third parameter passed as an array. How that can be converted for TWIG?
I tried the below found 2 lines and both failed with error so I assume its not the correct way.
{{ html_anchor('categories/create', 'Add', array('class' => 'btn')) }}
Twig arrays has [] format, so I tried this too.
{{ html_anchor('categories/create', 'Add', ['class' : 'btn']) }}
What's the correct way of handling this?
Here's how you define an associative array in twig:
{{ html_anchor('categories/create', 'Add', { 'class': 'btn' }) }}
Related
Suppose we have Symfony form type and a row adding field
$builder->add('name', 'text', ['attr' => ['class' => 'firstName', placeholder => 'first name']]);
I want this to be merged with attributes set in twig template:
{{ form_row(form.name, {'attr':{'class':'newClass'}}) }}
Currently it does replace. What is the best way to solve it?
You can use form variables to get form class attr, then concatene another class attr:
{{ form_row(form.name, {'attr':{'class':'newClass ' ~ form.name.vars.attr.class|default('')}}) }}
EDIT:
Corrected my previous answer after reading https://symfony.com/doc/current/reference/forms/twig_reference.html#reference-form-twig-variables
I simply want to give the text element of my edit-form a class.
{{ Form::text('first_name') }}
I know that i have to use an array with the class of the field as third argument. But i do NOT want to give a second argument. This one
{{ Form::text('first_name' , ' ' , array('class' => 'form-control')) }}
applys the class correctly, but sets the dafault value to an empty string. This is a problem, because now laravels auto-complete function doesn't fill the form with the correct data from the database. Is there a way to give no second argument or setting it to default like this?
{{ Form::text('first_name' , default , array('class' => 'form-control')) }}
Thanks!
Okay, i solved it myself. It is as easy as it always is. Simply use
{{ Form::text('first_name' , null , array('class' => 'form-control')) }}
I am trying to use the url Twig function with Silex to generate a route, but when I use the variable name that I have passed to the template it generates a warning that I have not supplied the parameter.
This is the array I am passing to the template:
[
"total_pages" => $pages,
"current_page" => $page,
"route_name" => "gallery_album",
"route_parameter" => "groupname",
"route_value" => $groupname
]
And in the template I am trying to use:
{{ url(route_name, {route_parameter: route_value, 'page': page} ) }}
(The page variable value is worked out in the template)
This is part of a pagination template that I am building so I need the parameter to be a variable so it can be applied to different pages. This is the error I get when I run this:
I feel this is something that is very simple, I am just missing something fundamental.
It thinks that route_parameter is a string key name and not a variable:
You can do for example:
{% set params = {'page': page, (route_parameter): route_value } %}
{{ url(route_name, params) }}
You can use {{ app->path}} or {{ app->url }}
if you using Silex\Application\UrlGeneratorTrait in you Application class
or alternative using this
{{ app.url_generator.generate('homepage') }}
Trying to render a dropdown in Symfony 2.7.0 but I am having some issues when rendering the choices the view.
$form = $this->createFormBuilder(null)
->add('timespan', 'choice', array(
'choices' => array(90 => "3 months", 30 => "1 month")
))
->getForm();
...
return array(
'form' => $form->createView(),
);
...
Doing var_dump after this will display the values:
var_dump($form->get('timespan')->getConfig()->getOption('choices'));
But when rendering it in the view like this:
{{ form_widget(form.timespan, {'class': 'span2'}) }}
The select box becomes empty.
<select id="form_timespan" name="form[timespan]" required="required" class="span2"></select>
Any ideas why this might occur? Am I missing something?
The problem is obviously in Twig. You can debug this by editing the form theme, to see what values comes in and what is expected. The theme can be found at:
vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig
(Or you can configure Symfony to use your own.)
You're looking for {% block choice_widget %} specifically to render this.
In this case it looks like you've forgotten to use the attr key for your HTML class:
{{ form_widget(form.timespan, {'attr': {'class': 'span2'}}) }}
You forgot the attr key in your call :
{{ form_widget(form.timespan, {'attr': {'class': 'span2'}}) }}
I have a form:
{ Form::open(array('action' => 'RatesController#postUserRate', $client->id)) }}
{{ Form::text('rate', '', array('placeholder' => 'Enter new custom client rate...')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
How do I pass my $client->id value through the form to my controller method?
I currently have a controller method that looks like this:
public function postUserRate($id)
{
$currentUser = User::find(Sentry::getUser()->id);
$userRate = DB::table('users_rates')->where('user_id', $currentUser->id)->where('client_id', $id)->pluck('rate');
if(is_null($userRate))
{
...
}else{
....
}
}
And the error log says "Missing argument 1 for RatesController::postUserRate()"
Any ideas on how to pass this $client->id into my controller so I can use it as I want to above?
Add {{ Form::hidden('id', $client->id) }}to the form. Then, when it's posted, you can fetch its value per usual with Input::get('id').
Also, remove the postUserRate method's argument.
You simply use :
Form::open(array('action' => array('Controller#method', $user->id)))
the variable $user->id is passed as argument to the method method, also this last one should recieve an argument as well, like so : method($userId)
Source : Laravel documentation