{%- if section_loop is defined -%}
    {%- set section_id = "section-"~section_loop.index -%}
{%- else -%}
    {%- set section_id = None -%}
{%- endif -%}
{% if content_block_loop is defined -%}
    {%- if section_loop is defined -%}
        {%- set content_block_id = "section-"~section_loop.index~"-content-block-"~content_block_loop.index -%}
    {%- else -%}
        {%- set content_block_id = "content-block-"~content_block_loop.index -%}
    {%- endif -%}
{%- else -%}
    {%- set content_block_id = "content-block" -%}
{% endif %}
{% if "styling" in content_block and "header" in content_block["styling"] -%}
    {% set content_block_header_styling = content_block["styling"]["header"] | render_styling -%}
{% else -%}
    {% set content_block_header_styling = "" -%}
{% endif -%}

{% if "styling" in content_block and "sub_header" in content_block["styling"] -%}
    {% set content_block_subheader_styling = content_block["styling"]["sub_header"] | render_styling -%}
{% else -%}
    {% set content_block_subheader_styling = "" -%}
{% endif -%}

{% if "styling" in content_block and "body" in content_block["styling"] -%}
    {% set content_block_body_styling = content_block["styling"]["body"] | render_styling -%}
{% else -%}
    {% set content_block_body_styling = "" -%}
{% endif -%}
<div id="{{content_block_id}}" {{ content_block | render_styling_from_string_template }}>
    {% if ("header" in content_block or "sub_header" in content_block) and content_block["content_block_type"] != "header" -%}
        <div id="{{content_block_id}}-header" {{ content_block_header_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
            <h4>
                {{ content_block["header"] | render_string_template }}
            </h4>
            {%- if "sub_header" in content_block and content_block["content_block_type"] != "header" -%}
            <h5 id="{{content_block_id}}-subheader" {{ content_block_subheader_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
                {{ content_block["sub_header"] | render_string_template }}
            </h5>
            {% endif -%}
        </div>
    {% endif -%}
    {%- if content_block["content_block_type"] == "header" -%}
    <div id="{{content_block_id}}-header" {{ content_block_header_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
        <h3>
            {{ content_block["header"] | render_string_template}}
        </h3>
        {%- if "sub_header" in content_block -%}
        <h5 id="{{content_block_id}}-subheader" {{ content_block_subheader_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
            {{ content_block["sub_header"] | render_string_template }}
        </h5>
        {% endif -%}
    </div>
    {%- elif content_block["content_block_type"] == "text" -%}
        <p id="{{content_block_id}}-body" {{ content_block_body_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
            {{content_block["content"][0]}}
        </p>
    {%- elif content_block["content_block_type"] == "value_list" -%}
        <p id="{{content_block_id}}-body" {{ content_block_body_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
        {% for value in content_block["value_list"] -%}
            {{ value | render_string_template }}
        {% endfor -%}
        </p>
    {%- elif content_block["content_block_type"] == "bullet_list" -%}
        <ul id="{{content_block_id}}-body" {{ content_block_body_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
            {% for bullet_point in content_block["bullet_list"] -%}
            <li {{ bullet_point | render_styling_from_string_template }}>{{ bullet_point | render_string_template }}</li>
            {% endfor %}
        </ul>
    {%- elif content_block["content_block_type"] == "graph" -%}
        <div id="{{content_block_id}}-body" {{content_block_body_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id)}}></div>
        <script>
            // Assign the specification to a local variable vlSpec.
            vlSpec = {{content_block["graph"]}};
            // Embed the visualization in the container with id `vis`
            vegaEmbed('#{{content_block_id}}-body', vlSpec, {
                actions: false
            }).then(result=>console.log(result)).catch(console.warn);
        </script>
    {%- elif content_block["content_block_type"] == "table" -%}
        <table id="{{content_block_id}}-body" {{ content_block_body_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) }}>
        {% for row in content_block["table_rows"] -%}
            <tr>
            {% set rowloop = loop -%}
            {% for cell in row -%}
                {% if cell is mapping and "styling" in cell -%}
                    {% set cell_styling = cell["styling"] | render_styling | replace("{{section_id}}", section_id) | replace("{{content_block_id}}", content_block_id) -%}
                {% else -%}
                    {% set cell_styling = "" -%}
                {% endif -%}

                <td id="{{content_block_id}}-cell-{{ rowloop.index }}-{{ loop.index }}" {{ cell_styling }}>{{ cell | render_string_template }}</td>
            {%- endfor -%}
            </tr>
        {%- endfor -%}
        </table>
    {%- elif content_block["content_block_type"] == "string_template" -%}
        {{ content_block["string_template"] | render_string_template }}
    {% endif %}
</div>