Edit element.style in HTML code in Shopify - html
I would like to add a style to a banner in the Shopify-Code. (A padding and a backgroudn color)
This banner has no deticated css that only applies to these kind of banners (or i can not find it) so i have to edit the element.style.
Could you help me how to edit this?
I attached an image with the changes i want to make
I think this is the part of the code where the changes should be made:
{{ 'section-image-banner.css' | asset_url | stylesheet_tag }}
{%- if section.settings.adapt_height_first_image and section.settings.image != blank -%}
{%- style -%}
#media screen and (max-width: 749px) {
#Banner-{{ section.id }}::before,
#Banner-{{ section.id }} .banner__media::before,
#Banner-{{ section.id }}:not(.banner--mobile-bottom) .banner__content::before {
padding-bottom: {{ 1 | divided_by: section.settings.image.aspect_ratio | times: 100 }}%;
content: '';
display: block;
}
}
#media screen and (min-width: 750px) {
#Banner-{{ section.id }}::before,
#Banner-{{ section.id }} .banner__media::before {
padding-bottom: {{ 1 | divided_by: section.settings.image.aspect_ratio | times: 100 }}%;
content: '';
display: block;
}
}
{%- endstyle -%}
{%- endif -%}
{%- style -%}
#Banner-{{ section.id }}::after {
opacity: {{ section.settings.image_overlay_opacity | divided_by: 100.0 }};
}
{%- endstyle -%}
<div id="Banner-{{ section.id }}" class="banner banner--content-align-{{ section.settings.desktop_content_alignment }} banner--content-align-mobile-{{ section.settings.mobile_content_alignment }} banner--{{ section.settings.image_height }}{% if section.settings.stack_images_on_mobile and section.settings.image != blank and section.settings.image_2 != blank %} banner--stacked{% endif %}{% if section.settings.adapt_height_first_image and section.settings.image != blank %} banner--adapt{% endif %}{% if section.settings.show_text_below %} banner--mobile-bottom{%- endif -%}{% if section.settings.show_text_box == false %} banner--desktop-transparent{% endif %}">
{%- if section.settings.image != blank -%}
<div class="banner__media media{% if section.settings.image == blank and section.settings.image_2 == blank %} placeholder{% endif %}{% if section.settings.image_2 != blank %} banner__media-half{% endif %}">
{%-liquid
assign image_height = section.settings.image.width | divided_by: section.settings.image.aspect_ratio
if section.settings.image_2 != blank
assign image_class = "banner__media-image-half"
endif
if section.settings.image_2 != blank and section.settings.stack_images_on_mobile
assign sizes = "(min-width: 750px) 50vw, 100vw"
elsif section.settings.image_2 != blank
assign sizes = "50vw"
else
assign sizes = "100vw"
endif
-%}
{{ section.settings.image | image_url: width: 1500 | image_tag:
loading: 'lazy',
width: section.settings.image.width,
height: image_height,
class: image_class,
sizes: sizes,
widths: '375, 550, 750, 1100, 1500, 1780, 2000, 3000, 3840',
alt: section.settings.image.alt | escape
} </div>
{%- elsif section.settings.image_2 == blank -%}
<div class="banner__media media{% if section.settings.image == blank and section.settings.image_2 == blank %} placeholder{% endif %}{% if section.settings.image_2 != blank %} banner__media-half{% endif %}">
{{ 'lifestyle-2' | placeholder_svg_tag: 'placeholder-svg' }}
</div>
{%- endif -%}
{%- if section.settings.image_2 != blank -%}
<div class="banner__media media{% if section.settings.image != blank %} banner__media-half{% endif %}">
{%-liquid
assign image_height_2 = section.settings.image_2.width | divided_by: section.settings.image_2.aspect_ratio
if section.settings.image != blank
assign image_class_2 = "banner__media-image-half"
endif
if section.settings.image != blank and section.settings.stack_images_on_mobile
assign sizes = "(min-width: 750px) 50vw, 100vw"
elsif section.settings.image != blank
assign sizes = "50vw"
else
assign sizes = "100vw"
endif
-%}
{{ section.settings.image_2 | image_url: width: 1500 | image_tag:
loading: 'lazy',
width: section.settings.image_2.width,
height: image_height_2,
class: image_class_2,
sizes: sizes,
widths: '375, 550, 750, 1100, 1500, 1780, 2000, 3000, 3840',
alt: section.settings.image_2.alt | escape
}}
</div>
{%- endif -%}
<div class="banner__content banner__content--{{ section.settings.desktop_content_position }} page-width">
<div class="banner__box content-container content-container--full-width-mobile color-{{ section.settings.color_scheme }} gradient">
{%- for block in section.blocks -%}
{%- case block.type -%}
{%- when 'heading' -%}
<h2 class="banner__heading {{ block.settings.heading_size }}" {{ block.shopify_attributes }}>
<span>{{ block.settings.heading | escape }}</span>
</h2>
{%- when 'text' -%}
<div class="banner__text {{ block.settings.text_style }}" {{ block.shopify_attributes }}>
<span>{{ block.settings.text | escape }}</span>
</div>
{%- when 'buttons' -%}
<div class="banner__buttons{% if block.settings.button_label_1 != blank and block.settings.button_label_2 != blank %} banner__buttons--multiple{% endif %}" {{ block.shopify_attributes }}>
{%- if block.settings.button_label_1 != blank -%}
<a{% if block.settings.button_link_1 == blank %} role="link" aria-disabled="true"{% else %} href="{{ block.settings.button_link_1 }}"{% endif %} class="button{% if block.settings.button_style_secondary_1 %} button--secondary{% else %} button--primary{% endif %}">{{ block.settings.button_label_1 | escape }}</a>
{%- endif -%}
{%- if block.settings.button_label_2 != blank -%}
<a{% if block.settings.button_link_2 == blank %} role="link" aria-disabled="true"{% else %} href="{{ block.settings.button_link_2 }}"{% endif %} class="button{% if block.settings.button_style_secondary_2 %} button--secondary{% else %} button--primary{% endif %}">{{ block.settings.button_label_2 | escape }}</a>
{%- endif -%}
</div>
{%- endcase -%}
{%- endfor -%}
</div>
</div>
</div>
{% schema %}
{
"name": "t:sections.image-banner.name",
"tag": "section",
"class": "section",
"settings": [
{
"type": "image_picker",
"id": "image",
"label": "t:sections.image-banner.settings.image.label"
},
{
"type": "image_picker",
"id": "image_2",
"label": "t:sections.image-banner.settings.image_2.label"
},
{
"type": "range",
"id": "image_overlay_opacity",
"min": 0,
"max": 100,
"step": 10,
"unit": "%",
"label": "t:sections.image-banner.settings.image_overlay_opacity.label",
"default": 0
},
{
"type": "select",
"id": "image_height",
"options": [
{
"value": "small",
"label": "t:sections.image-banner.settings.image_height.options__1.label"
},
{
"value": "medium",
"label": "t:sections.image-banner.settings.image_height.options__2.label"
},
{
"value": "large",
"label": "t:sections.image-banner.settings.image_height.options__3.label"
}
],
"default": "medium",
"label": "t:sections.image-banner.settings.image_height.label",
"info": "t:sections.image-banner.settings.image_height.info"
},
{
"type": "checkbox",
"id": "adapt_height_first_image",
"default": false,
"label": "t:sections.image-banner.settings.adapt_height_first_image.label",
"info": "t:sections.image-banner.settings.adapt_height_first_image.info"
},
{
"type": "select",
"id": "desktop_content_position",
"options": [
{
"value": "top-left",
"label": "t:sections.image-banner.settings.desktop_content_position.options__1.label"
},
{
"value": "top-center",
"label": "t:sections.image-banner.settings.desktop_content_position.options__2.label"
},
{
"value": "top-right",
"label": "t:sections.image-banner.settings.desktop_content_position.options__3.label"
},
{
"value": "middle-left",
"label": "t:sections.image-banner.settings.desktop_content_position.options__4.label"
},
{
"value": "middle-center",
"label": "t:sections.image-banner.settings.desktop_content_position.options__5.label"
},
{
"value": "middle-right",
"label": "t:sections.image-banner.settings.desktop_content_position.options__6.label"
},
{
"value": "bottom-left",
"label": "t:sections.image-banner.settings.desktop_content_position.options__7.label"
},
{
"value": "bottom-center",
"label": "t:sections.image-banner.settings.desktop_content_position.options__8.label"
},
{
"value": "bottom-right",
"label": "t:sections.image-banner.settings.desktop_content_position.options__9.label"
}
],
"default": "middle-center",
"label": "t:sections.image-banner.settings.desktop_content_position.label"
},
{
"type": "checkbox",
"id": "show_text_box",
"default": true,
"label": "t:sections.image-banner.settings.show_text_box.label"
},
{
"type": "select",
"id": "desktop_content_alignment",
"options": [
{
"value": "left",
"label": "t:sections.image-banner.settings.desktop_content_alignment.options__1.label"
},
{
"value": "center",
"label": "t:sections.image-banner.settings.desktop_content_alignment.options__2.label"
},
{
"value": "right",
"label": "t:sections.image-banner.settings.desktop_content_alignment.options__3.label"
}
],
"default": "center",
"label": "t:sections.image-banner.settings.desktop_content_alignment.label"
},
{
"type": "select",
"id": "color_scheme",
"options": [
{
"value": "accent-1",
"label": "t:sections.all.colors.accent_1.label"
},
{
"value": "accent-2",
"label": "t:sections.all.colors.accent_2.label"
},
{
"value": "background-1",
"label": "t:sections.all.colors.background_1.label"
},
{
"value": "background-2",
"label": "t:sections.all.colors.background_2.label"
},
{
"value": "inverse",
"label": "t:sections.all.colors.inverse.label"
}
],
"default": "background-1",
"label": "t:sections.all.colors.label",
"info": "t:sections.image-banner.settings.color_scheme.info"
},
{
"type": "header",
"content": "t:sections.image-banner.settings.header.content"
},
{
"type": "select",
"id": "mobile_content_alignment",
"options": [
{
"value": "left",
"label": "t:sections.image-banner.settings.mobile_content_alignment.options__1.label"
},
{
"value": "center",
"label": "t:sections.image-banner.settings.mobile_content_alignment.options__2.label"
},
{
"value": "right",
"label": "t:sections.image-banner.settings.mobile_content_alignment.options__3.label"
}
],
"default": "center",
"label": "t:sections.image-banner.settings.mobile_content_alignment.label"
},
{
"type": "checkbox",
"id": "stack_images_on_mobile",
"default": true,
"label": "t:sections.image-banner.settings.stack_images_on_mobile.label"
},
{
"type": "checkbox",
"id": "show_text_below",
"default": true,
"label": "t:sections.image-banner.settings.show_text_below.label"
}
],
"blocks": [
{
"type": "heading",
"name": "t:sections.image-banner.blocks.heading.name",
"limit": 1,
"settings": [
{
"type": "text",
"id": "heading",
"default": "Image banner",
"label": "t:sections.image-banner.blocks.heading.settings.heading.label"
},
{
"type": "select",
"id": "heading_size",
"options": [
{
"value": "h2",
"label": "t:sections.all.heading_size.options__1.label"
},
{
"value": "h1",
"label": "t:sections.all.heading_size.options__2.label"
},
{
"value": "h0",
"label": "t:sections.all.heading_size.options__3.label"
}
],
"default": "h1",
"label": "t:sections.all.heading_size.label"
}
]
},
{
"type": "text",
"name": "t:sections.image-banner.blocks.text.name",
"limit": 1,
"settings": [
{
"type": "text",
"id": "text",
"default": "Give customers details about the banner image(s) or content on the template.",
"label": "t:sections.image-banner.blocks.text.settings.text.label"
},
{
"type": "select",
"id": "text_style",
"options": [
{
"value": "body",
"label": "t:sections.image-banner.blocks.text.settings.text_style.options__1.label"
},
{
"value": "subtitle",
"label": "t:sections.image-banner.blocks.text.settings.text_style.options__2.label"
},
{
"value": "caption-with-letter-spacing",
"label": "t:sections.image-banner.blocks.text.settings.text_style.options__3.label"
}
],
"default": "body",
"label": "t:sections.image-banner.blocks.text.settings.text_style.label"
}
]
},
{
"type": "buttons",
"name": "t:sections.image-banner.blocks.buttons.name",
"limit": 1,
"settings": [
{
"type": "text",
"id": "button_label_1",
"default": "Button label",
"label": "t:sections.image-banner.blocks.buttons.settings.button_label_1.label",
"info": "t:sections.image-banner.blocks.buttons.settings.button_label_1.info"
},
{
"type": "url",
"id": "button_link_1",
"label": "t:sections.image-banner.blocks.buttons.settings.button_link_1.label"
},
{
"type": "checkbox",
"id": "button_style_secondary_1",
"default": false,
"label": "t:sections.image-banner.blocks.buttons.settings.button_style_secondary_1.label"
},
{
"type": "text",
"id": "button_label_2",
"default": "Button label",
"label": "t:sections.image-banner.blocks.buttons.settings.button_label_2.label",
"info": "t:sections.image-banner.blocks.buttons.settings.button_label_2.info"
},
{
"type": "url",
"id": "button_link_2",
"label": "t:sections.image-banner.blocks.buttons.settings.button_link_2.label"
},
{
"type": "checkbox",
"id": "button_style_secondary_2",
"default": false,
"label": "t:sections.image-banner.blocks.buttons.settings.button_style_secondary_2.label"
}
]
}
],
"presets": [
{
"name": "t:sections.image-banner.presets.name",
"blocks": [
{
"type": "heading"
},
{
"type": "text"
},
{
"type": "buttons"
}
]
}
]
}
{% endschema %}
I attached an image with the changes i want to make.
Basically i addeted
padding: 0 5rem;
background-color: #FFFFFF;
to the element.style in google Chrome DevTool
Related
Recursion in jinja template
I need to provide a JSON formatted information from a CMS into a web page. There may be deep nested structures in the page input like: { "input": [ { "header": "sample header text", "description": "sample description text", "content": { "json": { "children": [ { "type": "paragraph", "children": [ { "text": "sample child text " }, { "href": "https://myorg/tilasto/kony", "type": "link", "title": "Linkki ", "children": [ { "text": "sample child text" } ] }, { "text": "sample child text" } ] }, { "type": "table", "children": [ { "type": "table_body", "children": [ { "type": "table_row", "children": [ { "type": "table_cell", "children": [ { "type": "paragraph", "children": [ { "text": "sample text" } ] }, { "type": "table", "children": [ { "text": "sample text" } ] }, { "type": "paragraph", "children": [ { "text": "sample text" } ] } ] }, { "type": "table_cell", "children": [ { "type": "paragraph", "children": [ { "text": "sample text" } ] } ] } ] } ] } ] } ] }, "references": [ ] } } ] } How can I recursively do the same thing as in the below HTML? {% for tiedote in input %} <br> <h2> {{tiedote.header}}</h2> <p> {{tiedote.description}} </p> {% for children_list in tiedote.content.json.children %} {% for child in children_list.children %} <p> {{child.text}} </p> <a href="{{child.href}}"> {% for chil in child.children %} <p> {{chil.text}} </p> <a href="{{chil.href}}"> {% for chi in chil.children %} <p> {{chi.text}} </p> <a href="{{chi.href}}"> {% for ch in chi.children %} <p> {{ch.text}} </p> <a href="{{ch.href}}"> {% endfor %} {% endfor %} {% endfor %} {% endfor %} {% endfor %} {% endfor %} Also href may be present in some level of the input, how can I check if it is present or not? Updated: In order to use macro (as in answer) in jinja template do I need to install django-macros or would there be any other means for that?
How to create one json file with multiple entries using jinja2 template in ansible?
I am trying to create a json file with multiple secret (dictionary) and storage class. But the template is created in such a way that it will create multiple json file with single secret and a storage class in each file. my template (resources.j2) is look below "secret": [ { "name": "secret{{ user_name }}", "label": "{{ user_name }}", "backend": "{{ local_array_ip }}", "username": "{{ user_name }}", "password": "{{password | default('test123')}}", "namespace": "{{ default_namespace }}" } ], "storageclass": [ { "name": "sc{{ user_name }}", "secret": "{{ user_name }}", "label": "sc-{{ user_name }}", "limitMbps": "800", "fstype": "xfs", "accessProtocol": "{{ protocol }}" } ] } I am creating json inside a library file which is called in loop - name: Create config files for mulitple users include: lib/create_config_for_multiple_users.yaml vars: user_name: "{{ userName }}{{iterationCount}}" with_sequence: count="5" loop_control: loop_var: iterationCount In the Create_config_for_multiple_users.yml , it is creating json file for each user (so totally 5 json files are getting created). Creation of json file from template is given below. - set_fact: tmp_dynamic_json_file: "/tmp/temp-json-{{User}}-{{ lookup('pipe', 'date +%Y-%m-%d-%H-%M-%s') }}.json" - name: Create dynamic Json file with user template: dest: "{{tmp_dynamic_json_file}}" src: "resources.j2" delegate_to: localhost Actually i wanted one single json file with multiple entries like below. "secret": [ { "name": "secrettest1", "label": "test1", "backend": "{{ local_array_ip }}", "username": "test1", "password": "test123", "namespace": "kube-system" }, { "name": "secrettest2", "label": "test2", "backend": "{{ local_array_ip }}", "username": "test2", "password": "test123", "namespace": "kube-system" } ], "storageclass": [ { "name": "sctest1", "secret": "test1", "label": "sc-test1", "limitMbps": "800", "fstype": "xfs", "accessProtocol": "iscsi" }, { "name": "sctest2", "secret": "test2", "label": "sc-test2", "limitMbps": "800", "fstype": "xfs", "accessProtocol": "iscsi" } ] } I don't know how to create template in such a way that it will create only one json.
You are including create_config_for_multiple_users.yaml tasks which effectively iterates over the template: task. If the template task runs multiple times, it creates multiple templates. The iteration in this case, should be within the template. The template task should be only used/invoked once. Please see the example playbook below. I am including a task file as you do, but it doesn't matter. vars: userName: 'test' local_array_ip: '192.168.1.1' default_namespace: 'kube-system' protocol: 'iscsi' tasks: - name: Create config files for mulitple users include: create_config.yml Then in my create_config.yml, I use the template module: - name: Create dynamic Json file with user template: dest: '/tmp/resources.json' src: 'resources.json.j2' Then comes the iteration for user numbers in the template resources.json.j2: { "secret": [ {% for user_num in range(1, 6) %} { "name": "secret{{ userName }}{{ user_num }}", "label": "{{ userName }}{{ user_num }}", "backend": "{{ local_array_ip }}", "username": "{{ userName }}{{ user_num }}", "password": "{{ password | default('test123') }}", "namespace": "{{ default_namespace }}" }{% if user_num < 5 %},{% endif %} {% endfor %} ], "storageclass": [ {% for user_num in range(1, 6) %} { "name": "sc{{ userName }}{{ user_num }}", "secret": "{{ userName }}{{ user_num }}", "label": "sc-{{ userName }}{{ user_num }}", "limitMbps": "800", "fstype": "xfs", "accessProtocol": "{{ protocol }}" }{% if user_num < 5 %},{% endif %} {% endfor %} ] } I am creating a destination JSON file with a simple name /tmp/resources.json (abridged). { "secret": [ { "name": "secrettest1", "label": "test1", "backend": "192.168.1.1", "username": "test1", "password": "test123", "namespace": "kube-system" }, { "name": "secrettest2", "label": "test2", "backend": "192.168.1.1", "username": "test2", "password": "test123", "namespace": "kube-system" }, "storageclass": [ { "name": "sctest1", "secret": "test1", "label": "sc-test1", "limitMbps": "800", "fstype": "xfs", "accessProtocol": "iscsi" }, { "name": "sctest2", "secret": "test2", "label": "sc-test2", "limitMbps": "800", "fstype": "xfs", "accessProtocol": "iscsi" },
An error prevented faq.liquid from being saved
I am trying to add a FAQ section with blocks to a Shopify theme. I am able to dynamically add the section without blocks, but once I try to add blocks I keep getting the error message "An error prevented faq.liquid from being saved." I am not sure exactly what the problem is or why its not working since that is a very vague error message. Here is my code: <hr> <div id="section-faq"> <div class="section-header text-center"> <h3> {{ section.settings.text-box }} </h3> </div> {% for block in section.blocks %} <div class="btn" id="faq"> <a href="{{ block.settings.text-box }}" class="btn">{{ block.settings.text }}</a> </div> {% endfor %} </div> <hr> {% schema %} { "name": "faq", "max_blocks": 6, "settings": [ { "id": "text-box", "type": "text", "label": "Heading", "default": "FAQ" } ], "blocks": [ { "type": "QA", "name": "QA", "settings": [ { "id": "text-box", "type": "text", "label": "Question", "default": "Add Question" }, { "id": "text", "type": "richtext", "label": "Answer", "default": "<p>Add your answer here</p>" } ] } ], "presets": [ { "name": "FAQ", "category": "Faq" } ] } {% endschema %}
I have checked and created new section and add your code and its working fine for me.
Errors: Invalid JSON in tag 'schema' (Shopify Slick Sider)
I am building a slick slider using Shopify new feature section. All want to is add the image that has the arrow going left and right. With a Title, Description, and Button that user can change and add the link to everything with be on top of Carousel and change with everything some click on arrow or ghost buttons at the bottom Here a ScreenShot. {% if section.blocks.size > 0 %} <!-- Slick Slider Wrapper --> <div class="carousel-wrapper" style="background-color: red;"> <div class="carousel-info"> <!-- Title Slider Wrapper --> <h3>{{ section.settings.carousel_title }}</h3> <!-- Description Wrapper --> <p>{{ section.settings.carousel_product_description }}</p> <!-- Button Wrapper --> <button>{{ section.settings.carousel_button_title }}</button> </div> <!-- Slick Slider --> <div id="carousel-{{ section.id }}" class="carousel" data-slick='{"autoplay": {{ section.settings.carousel_autoplay }}}'> {% for block in section.blocks %} <div class="carousel-slide--{{ block.id }}" {{ block.shopify_attributes }} style="width:240px"> {% if block.settings.carousel_image != blank %} <img class="carousel_image--{{ block.id }}" src="{{ block.settings.carousel_image | img_url: '240x' }}" style="max-width:240px;display:inline-block;padding:40px"> {% endif %} </div> {% endfor %} </div> <!-- ended of Slick Slider --> </div> </div> {% endif %} {% if section.blocks.size == 0 %} <div class="placeholder-noblocks"> {{ 'homepage.onboarding.no_content' | t }} </div> {% endif %} <!-- Slick Slider Wrapper --> {% schema %} { "name": "Carousel", "max_blocks": 8, "settings": [ { "type":"header", "content":"Carousel option" }, { "type":"text", "id":"carousel_title", "label":"Carousel title", "default":"Carousel main title" }, "type": "text", "id": "carousel_title", "label":"Carousel title", "default":"Carousel main title" }, { "type" "text", "id": "carousel_product_description", "label": "Carousel Description", "default: Carousel Main Description" }, { "type": "url", "id": "carousel_link", "label": "Carousel title link" }, { "type":"header", "content":"In depth carousel option" }, { "type": "checkbox", "id": "carousel_autoplay", "label": "Auto-rotate slides", "default": false }, { "type":"color", "id":"carousel_bg", "label":"Carousel background", "default":"#fff" } ], "blocks": [ { "type": "image", "name": "Image slide", "settings": [ { "type": "image_picker", "id": "carousel_image", "label": "Image" }, { "type": "url", "id": "slide_link", "label": "Slide link" } ] } ], "presets": [{ "name": "Carousel", "category": "Image", "settings": { "carousel_autoplay": false }, "blocks": [ { "type": "image" }, { "type": "image" }, { "type": "image" }, { "type": "image" }, { "type": "image" }, { "type": "image" } ] }] } {% endschema %}
You were missing an open curly brace before the word type and a colon after the second type object. { "name": "Carousel", "max_blocks": 8, "settings": [ { "type":"header", "content":"Carousel option" }, { "type":"text", "id":"carousel_title", "label":"Carousel title", "default":"Carousel main title" }, { "type": "text", "id": "carousel_title", "label":"Carousel title", "default":"Carousel main title" }, { "type": "text", "id": "carousel_product_description", "label": "Carousel Description", "default: Carousel Main Description" }, { "type": "url", "id": "carousel_link", "label": "Carousel title link" }, { "type":"header", "content":"In depth carousel option" }, { "type": "checkbox", "id": "carousel_autoplay", "label": "Auto-rotate slides", "default": false }, { "type":"color", "id":"carousel_bg", "label":"Carousel background", "default":"#fff" } ], "blocks": [ { "type": "image", "name": "Image slide", "settings": [ { "type": "image_picker", "id": "carousel_image", "label": "Image" }, { "type": "url", "id": "slide_link", "label": "Slide link" } ] } ], "presets": [{ "name": "Carousel", "category": "Image", "settings": { "carousel_autoplay": false }, "blocks": [ { "type": "image" }, { "type": "image" }, { "type": "image" }, { "type": "image" }, { "type": "image" }, { "type": "image" } ] }] }
Jinja2 - How to loop a json list?
How can I loop a json list with jinja2? I have this json list, [ { "first_name": "John", "last_name": "Smith", "user_id": 4, "address": null }, { "first_name": "Jane", "last_name": "Heart", "user_id": 5, "address": null }, { "first_name": "Dom", "last_name": "Robinsons", "user_id": 6, "address": null }, { "first_name": "Pete", "last_name": "Hand", "user_id": 7, "address": null } ] page.html, <table> {% for user in users %} <tr><td>{{ user.first_name }}</td></tr> {% endfor %} </table> Result, <table> <tr><td></td></tr> <tr><td></td></tr> <tr><td></td></tr> <tr><td></td></tr> ... Any ideas?
your json list contains dictionaries; you need to access the dictionary elements differently than you would class members; try: <tr><td>{{ user['first_name'] }}</td></tr> this works for me (python 3.4 and python 2.7) import json from jinja2 import Template json_str = '''[{"first_name": "John", "last_name": "Smith", "user_id": 4, "address": null}, {"first_name": "Jane", "last_name": "Heart", "user_id": 5, "address": null}, {"first_name": "Dom", "last_name": "Robinsons", "user_id": 6, "address": null}, {"first_name": "Pete", "last_name": "Hand", "user_id": 7, "address": null}]''' users = json.loads(json_str) tmpl = Template(''' <table> {% for user in users %} <tr><td>{{ user['first_name'] }}</td></tr> {% endfor %} </table> ''') print(tmpl.render(users = users)) output: <table> <tr><td>John</td></tr> <tr><td>Jane</td></tr> <tr><td>Dom</td></tr> <tr><td>Pete</td></tr> </table>
simple json iteration in jinja2 <table> <tr> {% for key in users[0] %} <th>{{ key }}</th> {% endfor %} </tr> {% for user in users %} <tr> {% for key in user %} <td>{{ user[key] }}</td> {% endfor %} </tr> {% endfor %} </table>