I'm using PHP and Twig for an application in which I want to be able to have consistent form inputs across the site that are all rendered from one place in the code.
That is, I want all the form input generation markup to be in one place so that if/when I want to make a change to how the form inputs look and function across the entire site, I can change one thing in one place and have it take effect everywhere.
What's the best way to go about accomplishing this with Twig?
Thank you.
Use twig's macro
https://twig.symfony.com/doc/2.x/tags/macro.html
forms.html:
{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}
usage:
{% import "forms.html" as forms %}
<p>{{ forms.input('password', null, 'password') }}</p>
Related
For the first time, i am working on Symfony widgets.
{% render "EducateToolsBundle:Shared:selectMenu" with {'entity': 'Stores'} %}
its Meaning is SharedController -->selectMenuAction
{% form_theme form 'EducateToolsBundle:shared:_form_theme.html.twig' %}
What is the meaning of this.
From where i am getting the values to this form .?
You must look at selectMenuAction() method in the Shared controller. It is that function that set what template is used. if using the default Symfony coding standards it should be something like selectMenu.html.twig in a Shared sub folder in the Resources/views of the bundle.
You should looks at the documentation first :
https://symfony.com/doc/current/form/form_customization.html
Because we won't teach you symfony here, it's very well explain in the documentation and it is a task too big for us.
In your case theForm.id is the field of the formType we want to displays.
If we want to display the <input> of an form of user lastname we would use
form_widget(userForm.lastname)
form_widget tells symfony to display only the widget (the ) of the id given.
if you want to have a label + input + the errors of the field you could use
form_row(userForm.lastname)
wich is almost equal to
<div>
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
(it depends of the form theme, but you should read the doc for the details ;) )
Just started using OcteberCMS and now trying to figure out how to translate with rainlab.translate plug-in partial (no, I can't use here {{ ''|_ }} for some reason).
For example, code of layout:
{% partial "footer" %}
I have 2 files in "partials" dir:
footer.htm and footer.fr.htm
but always footer.htm including, but not footer.fr.htm when I'm switch language to fr.
Or maybe is there some way to pass translated variable to partial?
{% partial "sidebar-contacts" city="Vancouver" country="Canada" %}
{% partial "sidebar-contacts" city="{{ 'Vancouver'|_ }}" country="Canada" %}
Thanks in advance.
It's very simple: just pipe variable |_ into translate plug-in like this:
{% partial "sidebar-contacts" city="Vancouver" country="Canada" %}
In file sidebar-contacts:
<div class="sidebar-contacts">
{{ city|_ }}, {{ country|_ }}
</div>
I'm building a themed view in Drupal 8 using twig. I'd like to use twig filters on the row.content variable in the views-view-unformatted.html.twig template.
It seems that row.content is an array so twig's string manipulation doesn't work. However, it prints onto the page as a string (view is just a list of taxonomy terms).
What I'd like to do is slugify the output so taxonomy terms with spaces can have valid hrefs. See the replace filter in the code below.
<ul class="nav nav-tabs" role="tablist">
{% for row in rows %}
<li role="presentation" class="{{loop.first ? 'active' : ''}}">
{{row.content}}
</li>
{% endfor %}
</ul>
This will just output <a href="#">. Does anyone know how to access the raw text value that is output during twigs interpolation?
Thanks!
I ran into this myself it was difficult because kint and dump crashes on views.
there is a quick workaround to get to the bits though, put this under {% for row in rows %} in your twig views style template.
<ol>
{% for key, value in row.content %}
<li>{{ key }}</li>
{% endfor %}
</ol>
load the page with that & gives you the keys to see, I checked them each with the following dump command, just added underneath to test.
{{ dump(row.content['#row']) }}
The above dump showed all the goods in #row, where I dug in and found the field I wanted inside _entity (may be different for you), then I wanted to replace spaces with dash and force lowercase.
Everything past row.content['#row'] is likely different for you, you'll need to dig in the array a bit with the dump command mentioned above.
Below is the line that got me what I wanted.
{{ row.content['#row']._entity.title[0].value|replace(' ', '-')|lower }}
Below is twig template example.
For filename change viewname and block-3 to your setup.
views-view-unformatted--viewname--block-3.html.twig
{% for row in rows %}
{%
set row_classes = [
default_row_class ? 'views-row',
'something',
'kint-cant',
]
%}
{# My field value unformatted #}
<!-- {{ row.content['#row']._entity.title[0].value }} -->
<section{{ row.attributes.addClass(row_classes) }} id="{{ row.content['#row']._entity.title[0].value|replace(' ', '-')|lower }}">
{{ row.content }}
</section>
{% endfor %}
Im sure there are plenty of other ways to do this, but it worked for me as a quick solution to print out views fields in the style template, which is really useful.
I have a field in a Symfony form which contains three constraints.
For one of the three constraints, if validation fails, I want to trigger a popup notice on the form once the page has reloaded. This looks beyond the scope of Twig's form_errors() function.
Is there any way of finding out if a specific constraint on a single field has failed in my Twig template? I'm struggling to find a way to access this information right now. (I'm not opposed to passing a flag to the template from my controller if necessary.)
(Might be worth adding that this is a Silex project.)
You can check form.field.vars.errors like this:
<input type="text" name="msisdn"
class="{{ form.msisdn.vars.errors|length ? 'error' : '' }}"
value="{{ form.msisdn.vars.value }}" />
The actual errors are in an array, you can loop and output them individually like this:
<span class="errorMessage">
{% for error in form.msisdn.vars.errors %}
{{ error.message }}{% if not loop.last %},{% endif %}
{% endfor %}
</span>
I have a Team to which I would like to add players (objects of Player). I've set up the form "type" classes correctly. Here's the relevant part of my view:
{% for index, player in form.players %}
<div id="template" data-prototype="{{ form_widget(form.players.get('prototype')) | e }}">
<span class="title">{{ index }}</span>
{{ form_row(player.name) }}
</div>
{% endfor %}
My problem is that data-prototype attribute doesn't include the span tag; it only contains the output of {{ form_row(player.name) }}. Is there a way to include the whole content of div#template in the data-prototype attribute?
This was bugging me too. I customized a field type and even made simple form types with a reduced fieldset (for more complex objects), but it just didn't feel right. I've come up with a solution that should do the trick :)
First - I think your <div id="team" data-prototype="..."> should be outside your loop.
Next, your prototype is just a form view object, so if you pass it to a template you can render fields by hand and customize the output. Note: autoescape has no effect on included content, so use 'filter escape'.
<div id="playerFields" data-prototype="{% filter escape %}{% include 'AcmeTeamBundle:Team:prototypePlayer.html.twig' with {'form': form.players.get('prototype')} %}{% endfilter %}">
Then in your prototypePlayer.html.twig, just render each field as you normally would in a form view.
<div>
<span class="title">{{ form_label(form.name) }}</span>
{{ form_row(form.name) }}
<span class="age">{{ form_label(form.age) }}</span>
{{ form_row(form.age) }}
{# render whatever else you like from your Form/PlayerType class... #}
<div>
My template has a different data structure to your question, but it should help :)
Cheers,
Chris
Deprecation Warning:
As of Symfony 2.1, .get() is deprecated and the above will not work. Replace form.players.get('prototype') with form.players.vars.prototype to fix this in later versions of Symfony.