Helm how to copy section of complex values.yaml into a configmap - nested

I have a large values.yaml file that consists of nested objects, arrays that I am trying to render a copy of as a configmap. My code so far is:
{{- range $key, $value := index .Values "program_config" }}
{{ $key }}
{{- range $elem, $elemVal := $value }}
{{- if kindIs "invalid" $elemVal }}
{{ $elem }} =
{{- else if kindIs "string" $elemVal }}
{{ $elem }} = {{ tpl $elemVal $ }}
{{- else }}
{{ $elem }} = {{ $elemVal }}
{{- end }}
{{- end }}
{{- end }}
this gets me fairly close, but I am seeing issues with maps rendering as
object_schema : map[type:UTF8]
instead of
object_schema:
type: "UTF8"
and my arrays are showing up as:
queries :
0 : select xxx
instead of:
queries:
- >-
select xxx

After several hours worked thru it, this did it for me:
{{- range $key, $value := index .Values "program_config" }}
{{ $key }}:
{{- range $elem, $elemVal := $value }}
{{- if ne "queries" $key }}
{{- if ne "schema" $key }}
{{- if kindIs "invalid" $elemVal }}
{{ $elem }}: {{ "" }}
{{- else if kindIs "string" $elemVal }}
{{ $elem }}: {{ tpl $elemVal $ | quote}}
{{- else }}
{{ $elem }}: {{ quote $elemVal }}
{{- end }}
{{- end }}
{{- end }}
{{- if eq "queries" $key }}
- >-
{{ $elemVal }}
{{- end}}
{{- end }}
{{- end }}
{{- range $key, $value := index .Values.program_config.schema}}
{{ $key }}:
{{- range $nestedKey, $nestedVal := index $value }}
{{ $nestedKey }} : {{ $nestedVal | quote }}
{{- end }}
{{- end }}

Related

Macro to automatically generate section numbers

I am struggling with creating a macro to automatically generate section number and subsection numbers. I had thought the snippet below would work but it is failing to assign the value if not already set. I am sure this is obvious but my lack of jinja experience is showing... or perhaps lack of Python experience... or both!
I get this error.
{% set sectionnumber.value = sectionnumber.value + 1 %}
jinja2.exceptions.TemplateRuntimeError: cannot assign attribute on non-namespace object
{% macro getsectionnumber(type) -%}
{% if subsectionnumber is none %}
{% if sectionnumber is none %}
{% set sectionnumber = namespace(value=0) %}
{% endif %}
{% set subsectionnumber = namespace(value=0) %}
{% endif %}
{% if type == 'section' %}
{% if sectionnumber is none %}
{% set sectionnumber = namespace(value=0) %}
{% endif %}
{% set sectionnumber.value = sectionnumber.value + 1 %}
{{ sectionnumber.value }}
{% endif %}
{% if type == 'subsection' %}
{% set subsectionnumber.value = subsectionnumber.value + 1 %}
{{ sectionnumber.value }}.{{ subsectionnumber.value }}
{% endif %}
my template:
template = """
{% set sectionnumber = namespace(value=0) %}
{% set subsectionnumber = namespace(value=0) %}
{% macro getsectionnumber(type) -%}
{% if type == 'section' %}
{% set sectionnumber.value = sectionnumber.value + 1 %}
{{ sectionnumber.value }}
{% endif %}
{% if type == 'subsection' %}
{% set subsectionnumber.value = subsectionnumber.value + 1 %}
{{ sectionnumber.value }}.{{ subsectionnumber.value }}
{% endif %}
{% endmacro %}
{{ getsectionnumber('section') }}
{{ getsectionnumber('subsection') }}
{{ getsectionnumber('subsection') }}
{{ getsectionnumber('section') }}
{{ getsectionnumber('subsection') }}
{{ getsectionnumber('subsection') }}
"""
print the template:
print(Template(template).render())
result:
1
1.1
1.2
2
2.3
2.4

salt 3003: Loop over pillar contents

What is the Salt 3003 (Python 3) syntax to do the following
{% for group, args in pillar['groups.developers'].iteritems() %}
{{ group }}:
group.present:
- name: {{ group }}
{% if 'gid' in args %}
- gid: {{ args['gid'] }}
{% endif %}
{% endfor %}
Where developers is:
#./pillar/base/user/developers.sls
groups:
developers:
developer-a:
- fullname: Developer A
Try this one.
{% for group, args in salt['pillar.get']('groups:developers', {}).items() %}
{{ group }}:
group.present:
- name: {{ group }}
{% if 'gid' in args %}
- gid: {{ args['gid'] }}
{% endif %}
{% endfor %}
Use items() instead of iteritems().
More information about pillar

Conditionnal display with twig

I'm using Twig in Views to rewrite output with condition.
{{ field_illus_lycee }}
{% if field_titre_pour_views is defined %}
{% if field_titre_pour_views is not empty %}
{{ field_titre_pour_views }}
{% endif %}
{% else %}
{{ title }}
{% endif %}
<span class="accroche-admin">{{ body }}</span>
I want to display field_titre_pour_views only if it exists and isn't empty, otherwise the regular title should be displayed. But at this point the regular title isn't displayed.Inspired by this
I don't understand which mistake I've made.
EDIT: correct code
{{ field_illus_lycee }}
{% if field_titre_pour_views is defined %}
{% if field_titre_pour_views is not empty %}
{{ field_titre_pour_views }}
{% else %}
{{ title }}
{% endif %}
{% else %}
{{ title }}
{% endif %}
<span class="accroche-admin">{{ body }}</span>
Sometimes, to ask is to find...this code do the trick:
{% if field_titre_pour_views |default %}
{{ field_titre_pour_views }}
{% else %}
{{ title }}
{% endif %}
Auto fixed :)
Hope it would help someone else.

Twig extract FOR loop variables

Say I have a collection of items
$collection = array(
'item1' => array(
'post' => $post,
'category' => $category,
// ...
),
'item2' => array(...)
);
And I have a template:
{% for item in collection %}
Now I can use item data
- {{ item.post.title }}
- {{ item.category.id }}
- {{ item.var1 }}
- {{ item.var2 }}
- and another 20 vars
I want to extract those vars into more global FOR context, and use them as:
{{ post.title }}
{{ category.id }}
{{ var1 }}
... etc
{% endfor %}
Is this possible?
I was thining of defining the loop as a template block and then iterating it with Twig_Template::renderBlock(). But the docs say renderBlock is for 'internal' use only :) So not sure.
EDIT:
Another idea I had:
{% for item in collection %}
{% do extract(item) %}
// extract() would work similar to extract function from php
{% endfor %}
However, it seems that context is passed to twig functions by value, so this would not work.
Lastly I could write a TokenParser and do:
{% for item in collection %}
{% extract item %}
// would probably get direct access to the context, but haven't tried it
{% endfor %}
But this is quite a bit of work.. I am just hoping that twig can already do this natively :)
You can use a macro :
http://twig.sensiolabs.org/doc/tags/macro.html
{% import _self as macro %}
{% macro render(item) %}
{{ item.post.title }}
{{ item.category.id }}
{{ item.var1 }}
{{ item.var2 }}
...
{% endmacro %}
{% for item in collection %}
{{ macro.render(item) }}
{% endfor %}
If you really want to assign variables in the global context :
{% for item in collection %}
{% for var, value in item %}
{% set _context[var] = value %}
{% endfor %}
{{ post.title }}
{{ category.id }}
{{ var1 }}
... etc
{% endfor %}

Render all fields except the submit in symfony 2.3

I'm overriding Skeleton templates of SensioGeneratorBundle as describred in:
http://symfony.com/doc/current/bundles/SensioGeneratorBundle/index.html#overriding-skeleton-templates
So until here is everything fine.
In one of the templates of SensioGeneratorBundle I have:
# app/resources/SensioGeneratorBundle/skeleton/crud/views/new.html.twig.twig
{% block body %}
{{ "{% block page_title 'Incluir " ~ entity ~ "'%}" }}
{{ "{% block body -%}" }}
{{ '{{ form(form) }}' }}
{% set hide_edit, hide_delete = true, true %}
{% include 'crud/views/others/record_actions.html.twig.twig' %}
{{ "{% endblock %}" }}
{% endblock body %}
This works, but {{ form(form) }} is rendering the submit button, and I want to render the submit button in the record_actions.html.twig.twig.
So my question is: How to render a form without render the submit button? Remembering that i'm trying to do this in the skeleton template, in this moment I don't have the fiels of the form to iterate over it.
Thanks!
The solution for this problem is as follows:
# app/resources/SensioGeneratorBundle/skeleton/crud/views/new.html.twig
{% block body %}
{{ "{% block page_title 'Incluir " ~ entity ~ "'%}" }}
{{ "{% block body -%}" }}
{{ " {{ form_start(child) }}" }}
{{ " {% for child in form %}" }}
{{ " {% if child.vars.name != 'submit' %}" }}
{{ " {{ form_row(child) }}" }}
{{ " {% endif %}" }}
{{ " {% endfor %}" }}
{% set hide_edit, hide_delete = true, true %}
{% include 'crud/views/others/record_actions.html.twig.twig' %}
{{ "{% endblock %}" }}
{% endblock body %}
And inside record_actions.html.twig.twig
# app/resources/SensioGeneratorBundle/skeleton/crud/views/record_actions.html.twig
{{ " {{ form_row(form.submit) }}" }}
{{ " {{ form_end(form) }}" }}
<ul class="record_actions">
<li>
<a href="{{ "{{ path('" ~ route_name_prefix ~ "') }}" }}">
Back to the list
</a>
</li>
{% if ('edit' in actions) and (not hide_edit) %}
<li>
<a href="{{ "{{ path('" ~ route_name_prefix ~ "_edit', { 'id': entity.id }) }}" }}">
Edit
</a>
</li>
{% endif %}
{% if ('delete' in actions) and (not hide_delete) %}
<li>
<form action="{{ "{{ path('" ~ route_name_prefix ~ "_delete', { 'id': entity.id }) }}" }}" method="post">
<input type="hidden" name="_method" value="DELETE" />
{{ '{{ form_widget(delete_form) }}' }}
<button type="submit">Delete</button>
</form>
</li>
{% endif %}
</ul>

Resources