I have a custom twig template and within it I have a div that I only want to display if the value of another field on the page (weight) is -5.
I know how to show the field if there is a 'value' but im not sure how to write it for a specific value...?
What I have now:
{% if node.field_position_on_page.value %}
<div class="profile-pic">
{{ content.field_profile_picture }}
</div>
{% endif %}
I need it to be something like none.field_position_on_page[-5] but im not sure on the correct way to write that?
Using {% if node.field_position_on_page.value == -5 %} will compare the field to your specific value.
Related
i have a Node which has a field node_field (entity reference revision) which points to a paragraph (can be added 3 times). That paragraph has only 1 field in it, which itself is a entity reference field that points to a node bundle.
I'm trying to loop through each paragraph and each node inside so i can render the entire node (not individual fields), i only want the nodes, don't need to render the paragraph.
Here's my approach in the node.twig.html via the content variable and it works, i get the desired result.
{% if content.my_field|render %}
{% for key, item in content.my_field if key|first != '#' %}
<div class="col-lg-4 col-md-6">
{{ item }}
</div>
{% endfor %}
{% endif %}
Now i want to do the exact same thing but use the node variable instead of content but i just can't seem to get it to print. Any ideas?
Realy it's depend of the content that you have into the item. If you have devel installed, you could use kint to see the difference between the content of both, the differents variables, etc...
Depends of the element where you are iterating you can render each elements, other times you need get the value of each item (item.getValue()[0]).
I hope that my answer could be enought.
Is there a way to check if a current page belongs to a collection? If so, can I check what’s the label of the collection?
Background story:
What I’m trying to achieve is to display a different layout for all pages that belong to a collection. With this new layout I want to display a side navigation (some sort of „See also”) with listing of all pages in the collection that the current site belongs to.
In case my question turns out to be dumb – forgive me, I’m new to jekyll and I really tried to find out the answer by myself.
page.collection returns the label of the collection to which the document belongs. So I guess you want to do something like:
{% if page.collection == 'COLLECTION_LABEL' %}
NEW LAYOUT
{% else %}
OLD LAYOUT
{% endif %}
To access the pages in a collection, you can use the label with site.COLLECTION_LABEL or site[COLLECTION_LABEL] (you need [] if it is a variable). Something like:
{% for page in site[page.collection] %}
{{ page.title }}
{% endfor %}
Any collection document has a page.collection variable which is the collection label.
But, if you want to associate a specific layout to a collection, you can use default configuration :
defaults:
-
scope:
type: mycollection
values:
layout: a_custom_layout
anyvar: a_custom_var
I'm looking for a method to show or hide a navigation menu item depending on if the user is logged in or not. How can I accomplish this with Drupal 8? Should this be done with CSS?
You can do this using twig Template :
{% if logged_in %}
PASSED YOUR CODE HERE ..
{% endif %}
I have setup the following:
{% include '_elements/page-heading.html' with {'shortTitle': 'entry.shortTitle','title':'entry.title'} %}
However, when the page renders this template it is actually outputting "entry.shortTitle" in the page when it should surely actually know what the entry.title is for this page.
e.g. it should say "Latest News" not "entry.title".
Just wondering why this would be?
Thanks!
Enclosing entry.shortTitle with (single) quotes tells Twig that this simply is a string. When you want to pass the value of a variable, you have to leave away the quotes:
{% include '_elements/page-heading.html' with { 'shortTitle': entry.shortTitle, 'title': entry.title } %}
I want to check if a form is new in my template, something like $form->isNew() in symfony 1.4, but i did not find a solution.
Currently, i'm using this technique:
{% null != entity.getId %}
it works, but are there other solutions which are more clean?
I use this:
{% if form.vars.submitted == false %}DO WHATEVER HERE{% endif %}
More info in form functions and variables here:
https://symfony.com/doc/current/reference/forms/twig_reference.html
Symfony2 forms are less coupled to the model than symfony1 forms. Form in Symfony2 cannot be "new" or "old". It's the entity which is persisted, not the form. Forms job is to put submitted data to the model object.
Remember that model class doesn't have to be a doctrine entity (can be really an object of any class). It's up to you to define what does it mean that object is new or not (persisted or not). Again, it's not a form which is persisted.
Btw, you could check for id like this:
{% if entity.id %}{% endif %}
A better solution nowadays (SF3) will be:
{% if form.vars.data.id %}{% endif %}