NUNJUCKS setting a variable - nunjucks

Hi all Im calling data from a json file. Whe I create the following loop:
{% for item in resume.applications %}
<span class="expanded uppercase">{{ item.fields.Job }}</span>
{{ item.fields.intro | safe }}
{% endfor %}
The span renders with the correct html content. However if I try to set a variable without a loop:
{% set job = resume.applications %}
<span>{{ job.fields.intro }}</span>
I get nothing? Not sure what I'm doing wrong. Thanks.
I get nothing

Related

Shopify Help Returning Line Item Properties

I am having an issue retrieving the line tem properties of an order. The problem is that the code I am using is not displaying anything.
I am able to get the order line items, but the properties of the line item (like if I have a form field name properties[SomeText] or properties[Color])
Here is a simplified version of what I am using:
{% for item in order.line_items %}
Sku: {{item.item.sku}}
Product Title: {{item.title}}
{% for prop in item.properties %}
Properties: {{ prop.first }} = {{ prop.last }}
{% endfor %}
{% endfor %}
In the example above, the values for Sku and Product Title are working, but I am not getting any values returned for the Properties. I know they exist because they show when I go an view an order.
So, I'm not sure what I've done incorrectly. Any help will be greatly appreciated.
After looking at the Order's raw XML, I noticed that instead of using prop.first and prop.last, I changed it to prop.name and prop.value and it works.
{% for item in order.line_items %}
Sku: {{item.item.sku}}
Product Title: {{item.title}}
{% for prop in item.properties %}
Properties: {{ prop.name }} = {{ prop.value }}
{% endfor %}
{% endfor %}

How to filter Taxonomies using Rust-based Zola / Tera?

I have recently discovered Zola and Tera (Rust frameworks for statically-generated websites) and found them amazing.
I'm trying to filter specific category pages to display in a section on the same page. To illustrate, I wrote some code like this:
<div class="content">
{% block content %}
<div class="list-posts">
{% for page in section.pages %}
{% for key, taxonomy in page.taxonomies %}
{% if key == "categories" %}
{% set categories = taxonomy %}
{% for category in categories %}
{% if category == "rust" %}
<article>
<h3 class="post__title">{{ page.title }}</h3>
</article>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endblock content %}
</div>
There should be MULTIPLE sections of the code above for different categories, e.g. "rust", "java", etc.
I wrote the code to explain my question, but it isn't the way I want it (and it doesn't work when the sections are duplicated).
How do I do the filtering of the particular category when the sections/pages are loaded?
The front-matter metadata in the content file is:
title = "A web page title"
[taxonomies]
categories = ["rust"]
If you see my example code above, I have to access it first via a hash map, then an array, in order to filter all pages which is "rust".
The filter below doesn't work:
for page in section.pages | filter(attribute="taxonomies.categories", value="rust"
I managed to resolve it. First, I did tests like this:
HTML test print output
{% set categories = get_taxonomy(kind="categories") %}
{% set rustItems = categories.items | filter(attribute="name", value="rust") %}
{% set javaItems = categories.items | filter(attribute="name", value="java") %}
{{ rustItems[0].pages | length }}
<br>
{{ rustItems[0].pages[0].title }}
<br>
{{ rustItems[0].pages[1].title }}
<br>
I was able to pick up the title as set in the .md file.
So I moved on further and I did:
{% set categories = get_taxonomy(kind="categories") %}
{% set category = categories.items | filter(attribute="name", value="business") | first %}
{% for page in category.pages %}
{{ page.title }}
... etc.
The above code will filter the pages for category taxonomy.

Content types of my images doesn't display

Why is it the images will not display if I'm going to loop them like this?
{% for item in content.field_room_gallery_pictures['#items'].getValue() %}
<div class="col-xs-6 col-md-4 image-item">
{{item.value}}
</div>
{% endfor %}
And when I fetched the data if there is or not using kint.. it will display an array with the value of 3 using this code..So it means in my field_room_gallery_pictures there is an array of data.
{{kint(content.field_room_gallery_pictures['#items'].getValue())}}
have your tried this:
{% for i in 0..content.field_room_gallery_pictures|length %}
{%if content.field_room_gallery_pictures[i]['#item'].entity.uri.value != "" and content.field_room_gallery_pictures[i]['#item'].entity.uri.value is not empty %}
<div>
{{ content.field_room_gallery_pictures[i]}}
</div>
{%endif%}
{%endfor%}
works fine for me.

Twig include finding set when in a file.

I am trying to use twig / include to pull content from two files where the second file passes as a variable the 'set' content from the first. The problem is that the 'set' content cannot be seen when it comes from a file. As an example, this works
{% set localContent %}
<div> someContent </div>
{% endset %}
{% include 'MyBundle:Templates:some.html.twig' %}
{% include 'MyBundle:Templates:main.html.twig' with {
'includedContent': localContent,
} %}
where main.html.twig is simply:
{% block form_row %}
<div> mainContent </div>
{{includedContent}}
{% endblock form_row %}
and some.html.twig contains:
{% set fileContent %}
<div> someContent </div>
{% endset %}
When I change the includedContent variable to fileContent which is defined in a file I get an exception that indicates fileContent cannot be located.
Is what I am trying to do possible ?
Can anyone help me, I would like to get this application finished before the world ends in a few days :-).
I found another mechanism to accomplish the same task. If I pass the include path into the main twig file and use that to access the fileContent everything works as expected.

Symfony2 + CreateFormBuilder how to render an image in a form

I'm Symfony2 and CreateFormBuilder to create my form.
Currently I'm using {{ form_widget(form) }} to display the form.
My entity have path property that is the path of an image save on filesystem.
I want to display the image in the form (with a <img> html tag), how can I achieve this result? Should I handle the form in my template field by field? Or is there a way to own only one field in the template and render the other ones with {{ form_widget(form) }} ?
What you could do is handling the form field by field, and display the image if the value is set.
Let's say you have a field name image. In Twig, you can access its value through form.vars.value.image. Then, it's quite easy to display an img tag:
{% if form.vars.value.image is not null %}
<img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}
Here, upload/dir/ is a path where you store your images. If you have a constant for this path, you can use it in Twig:
{{ asset(constant('Acme\\DemoBundle\\Model\\Object::UPLOAD_DIR') ~ '/' ~ form.vars.value.image) }}
An alternative could be to create your own type with its own template:
http://symfony.com/doc/current/book/forms.html#form-theming
http://symfony.com/doc/current/cookbook/form/form_customization.html
Edit: I forgot an interesting alternative. You can customize an individual field: http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field. Here is a draft of what you could do:
{% form_theme form _self %}
{% block _object_image_row %}
<div class="name_row">
{{ form_label(form) }}
{{ form_errors(form) }}
{{ form_widget(form) }}
{% if form.vars.value.image is not null %}
<img src="{{ asset('upload/dir/' ~ form.vars.value.image) }}" />
{% endif %}
</div>
{% endblock %}

Resources