I got a custom taxonomy of events called "typology".
Taxonomy :
slug : typology
name : Typologies
- type 1
- type 2
- type 3
I created a dynamic selector that displays each type in order to filter a list of post based on that specific taxonomy.
This part is OK with :
$context['terms-typology'] = Timber::get_terms('typology');
and in twig
<select>
{% for term in typology %}
<option value="{{ term }}">{{ term }}</option>
{% endfor %}
</select>
But i would like to add a first option (selected by default) which would be an unfiltered option and where the text should be : "All Typologies".
I tried to add this option and get back the parent {{ term.name }}
I tried to pass it in the context with another line :
$context['term'] = new Timber\Term('typology');
And use
<option selected value="{{ term.name }}">All {{ term.name }}</option>
I've tried many other things. But I just don't manage to do it and I don't get it.
Related
I am building a form which contains a series of categories used to search some content, defined as a ChoiceType within my Symfony FormType.
I pass the category list along with some other data (per-category count) (defined as the variable "aggs" in my controller) into a Twig template and create a form theme, overriding the choice_widget_options block for my categories drop-down so that I can display the extra data at render time, thus:
{% form_theme form.categories _self %}
{% block choice_widget_options %}
{% if choices is defined %}
{% for choice in choices %}
<option value="{{ choice.value }}">{{ choice.label }} {{ aggs }}</option>
{% endfor %}
{% endif %}
{% endblock choice_widget_options %}
Why is it that my block here cannot access the top-level variables defined in my controller? It can see the "app" global variable, but not the controller-defined ones.
Thanks!
More elegant solution is to override the buildView method of AbstractType class.
Just add:
$view->vars[‘aggs’] = YOUR AGGS VAR;
And the use it in your form as:
<option value="{{ choice.value }}">{{ choice.label }} {{ form.vars.aggs }}</option>
Basically you pass the variable to your form and then you use it in twig. The way you pass it to the form type is up to you. It can be a dependency injection or via the form options from the controller.
As i said in title i have this
<select id="form_test" oninput="loadTemplate()">
<option>Template list</option>
{% for template in templates %}
<option id="test" value="{{ template.getReport }}">
{{ template.getTemplateName }}
</option>
{% endfor %}
</select>
It shows me all template names from {{ template.getTemplateName }} from database. However value="{{ template.getReport }}" this value always returns first report from database, as If for loop won't work inside tag.
I am using doctrine to fetch this entities from database, like this:
$templates = $this->getDoctrine()->getRepository(ReportTemplate::class)->findAll();
return $this->render('report_form/reportForm.html.twig', [
'templates' => $templates,
]);`
After I deployed site on live server it started working. It's strage I still don't know what was wrong.
I'm trying to setup a filter in my collection pages.
So far I manage to setup a great custom tag filter like below:
<div class="collection-sort">
{% assign tags = 'Black, Slate, Military Green' | replace: ' ,', ',' | replace: ', ', ',' | split: ',' %}
<select id="FilterBy" class="collection-sort__input">
<option value="/collections/{{ collection.handle }}">Choose Color</option>
{% for tag in tags %}
{% if current_tags contains tag %}
<option value="/collections/{{ collection.handle }}/{{tag}} " selected>{{ tag }}</option>
{% elsif collection.all_tags contains tag %}
<option value="/collections/{{ collection.handle }}/{{tag}}">{{ tag }}</option>
{% endif %}
{% endfor %}
</select>
</div>
However, I'm looking to have a dynamic Size filter ( using Variant)
For this I tried the following:
<div class="collection-sort">
<span value="">Choose Size</span>
{% for variant in collection.variants %}
{% if variant.available %}
<span value="{{ variant.id }}" >{{ variant.size}}</span>
{% else %}
<span value="{{ variant.id }}" >{{ variant.size }}</span>
{% endif %}
{% endfor %}
</div>
But nothing appear i my dropdown . . . all my product have size entered as product option / variant . . .
Anyone managed to make this work ? It will be very helpful !
Thanks a lot
There are too many issue with your code in order for it to output anything.
Lets disect them one by one.
Collections does not have variants
With this line of code:
{% for variant in collection.variants %}
You are targeting the variants inside a collection but collections doesn't have variants. Products do have variants.
So the logic here is not correct.
Variants options are stored in different way
With the following code: {{ variant.size }} you are trying to get the variant option called size but it doesn't work that way.
You will have to get the option using the objects option1 , option2 or option3. If your variant option size is the first one you will get it this way: variant.option1.
The bad part
What you are trying to achive is not possible with liquid because of Shopify limitation.
In order to achieve this you will need to loop all of the products and take their variant options and filter only the unique ones and because of the hard limit ot 50 products per request this is a lost fight.
Workarounds ( there are a few )
All of the workarounds will require you to create a link_list that will hold all the available sizes that you will have to enter manually.
1) The most common one is to use a tag for the size and filter by tag, since the collections can filter products directly by tag.
2) Use collections that will store products based on the size and redirect to them when you filter a specific size.
3) Create a infinite AJAX requests that pull products based on your filter by checking if each product have the selected value and using the pagination as a way to load the next page.
These are the main options without using an App.
Good luck!
On a custom taxonomy archive (taxonomy-strategy.php), I would like to display all custom posts that have a taxonomy called strategy.
As per the TimberTerm documentation, I am using $context['term_page'] = new TimberTerm(); and then, in taxonomy-strategy.twig:
{% for post in term_page.posts %}
{{ post.title }}
{% endfor %}
The problem is that only 10 of the posts with that taxonomy are being outputted. I have changed my reading settings to +25 posts. I have tried passing TimberTerm() parameters to to unrestrict the number of posts, but unlike get_terms or other similar functions, TimberTerm() does not accept such a parameter.
I would prefer to solve this issue on the taxonomy archive page, since the user gets here by following the term name.
I have resolved through directly through the view:
{% for post in term_page.posts('numberposts_or_args=-1') %}
{{ post.etcetc }}
{% endfor %}
where term_page is defined by $context['term_page'] = new TimberTerm();, making the term in question defined by the template hierarchy: taxonomy-[taxname].php
I would like know how can I change this:
<select id="mifid-proprio" name="mifid-proprio">
<option value="{{ personne.id }}">{{ personne.prenom }} {{ personne.nom }}</option>
{% for key, proprietaire in proprietaires %}
<option value="{{ proprietaire.destination.id }}">{{ proprietaire.destination.prenom }} {{ proprietaire.destination.nom }}</option>
{% endfor %}
</select>
into Twig + Formbuilder (Symfony 2).
I think you want to build that widget in the FormBuilder. If I am right you should take a look at the documentation of the choice Field Type. When the data you want to display in that select box comes from a database you should take a look at the entity Field Type. If you have never worked with the FormBuilder before, you should also read how to create form classes.
If you build that widget in the FormBuilder you can output it in Twig with:
{{ form_widget(form.field_name) }}
When you are not satisfied with the generated HTML code you can overwrite it, either on a per-template or global basis. You can read more about form theming.