Spaceless tag inside script block - twig

I'm trying to inline this so the rendered page isn't 100,000 lines long.
It seems the spaceless tag does nothing here.
Input:
{% for log in logs -%}
{% spaceless %}
{
id: {{ log.id }},
description: '{{ log.description }}'
},
{% endspaceless %}
{% endfor -%}
Expected Output:
{id:17186,description:'Test Log'},
{id:17187,description:'Test Log 2'},
Output:
{
id: 17186,
description: 'Test Log'
},
{
id: 17187,
description: 'Test Log 2'
},

Like the documentation says, you can "use the spaceless tag to remove whitespace between HTML tags, not whitespace within HTML tags or whitespace in plain text."
You can instead control whitespace e.g. like this:
{% for log in logs -%}
{
{{- 'id' }}: {{ log.id }},
{{- 'description' }}: '{{ log.description }}'
{{- '},' }}
{%- endfor %}
The above produces this:
{id: 17186,description: 'Test Log'},{id: 17187,description: 'Test Log 2'},
The downside is that the Twig code is much uglier.
Here's an alternative using string interpolation:
{% for log in logs -%}
{
{{- 'id' }}: {{ log.id }},
{{- 'description' }}: {{ "'#{log.description}'" -}}
},
{%- endfor %}
{id: 17186,description: 'Test Log'},{id: 17187,description: 'Test Log 2'},
The Twig code looks now better, but notice that single quotes got converted to '. You may or may not want to do something about that.
You could also do like the documentation suggests and use a third-party library like Tidy.

Related

New Line character in email alerts

How to print new line character when sending emails? I'm sending it to gmail. The character \n prints literally. I even tried </br> tag and yaml mutliline and none of them work.
- alert: KubernetesPodImagePullBackOff
expr: kube_pod_container_status_waiting_reason{reason=~"ContainerCreating|CrashLoopBackOff|ErrImagePull|ImagePullBackOff"} > 0
for: 1s
labels:
severity: warning
annotations:
summary: "Kubernetes pod crash looping (instance {{ $labels.instance }}"
description: "Pod {{ $labels.pod }} is crash looping\n VALUE = {{ $value }}\n LABELS: {{ $labels }}"
You need to rewrite default template in Alertmanager for E-mails.
You need to replace something like
{{ .Annotations.description }}
in template by
{{ .Annotations.description | safeHtml }}
I wrote for my own email template, if you have not your own, you may create it from
https://github.com/prometheus/alertmanager/blob/master/template/default.tmpl
and edit
{{ range .Annotations.SortedPairs }} - {{ .Name }} = {{ .Value }}
in the same manner with
{{ .Value | safeHtml }}
Also read this answer
prometheus using html content in alerts annotations and using it in email template

Prometheus alertmanager - invalid leading UTF-8 octet

I am trying to configure the slack notification from Prometheus alertmanager with below yml.
global:
resolve_timeout: 1m
slack_api_url: 'https://hooks.slack.com/services/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'
route:
receiver: 'slack-notifications'
receivers:
- name: 'slack-notifications'
slack_configs:
- channel: '#monitoring-instances'
send_resolved: true
icon_url: https://avatars3.githubusercontent.com/u/3380462
title: |-
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .CommonLabels.alertname }} for {{ .CommonLabels.job }}
{{- if gt (len .CommonLabels) (len .GroupLabels) -}}
{{" "}}(
{{- with .CommonLabels.Remove .GroupLabels.Names }}
{{- range $index, $label := .SortedPairs -}}
{{ if $index }}, {{ end }}
{{- $label.Name }}="{{ $label.Value -}}"
{{- end }}
{{- end -}}
)
{{- end }}
text: >-
{{ range .Alerts -}}
*Alert:* {{ .Annotations.title }}{{ if .Labels.severity }} - `{{ .Labels.severity }}`{{ end }}
*Description:* {{ .Annotations.description }}
*Details:*
{{ range .Labels.SortedPairs }} • *{{ .Name }}:* `{{ .Value }}`
{{ end }}
{{ end }}
When i start my alert-manager container it keep on restarting and shows below error.
alertmanager | level=error ts=2021-01-12T04:08:19.040Z caller=coordinator.go:124 component=configuration msg="Loading configuration file failed" file=/etc/alertmanager/alertmanager.yml err="yaml: invalid leading UTF-8 octet"
Have validated Here it shown as valid yaml
Also checked with notepad++ the encoding already showing as UTF-8 Any other way to fix this?
Even this code also shows same error.
slack_configs:
- channel: '#monitoring-instances'
send_resolved: false
title: '[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] Monitoring Event Notification'
text: >-
{{ range .Alerts }}
*Alert:* {{ .Annotations.summary }} - `{{ .Labels.severity }}`
*Description:* {{ .Annotations.description }}
*Graph:* <{{ .GeneratorURL }}|:chart_with_upwards_trend:> *Runbook:* <{{ .Annotations.runbook }}|:spiral_note_pad:>
*Details:*
{{ range .Labels.SortedPairs }} • *{{ .Name }}:* `{{ .Value }}`
{{ end }}
{{ end }}
I am using Centos-8.2 system, is it something wrong in my system? Can anyone help me out here.
In my occasion there was an issue with the config/application.yml
It was all Gibberish so I had to deleted it and recreate it.
After that the issue was resolved.
Resolved by removing bullet • in the code and launched the container. It works now.

Uncaught Twig_Error_Syntax: Unknown "json_encode" function opencart

I have an issue with a twig file in my opencart 3.0.2.0 webshop. The error that I got is:
Uncaught Twig_Error_Syntax: Unknown "json_encode" function in
"default/template/extension/module/notification.twig" at line 22. in
/home/mk4design/public_html/system/library/template/Twig/ExpressionParser.php:574
Stack trace:#0
The complete twig file is below and there are 3 lines with the json_encode that are not correct.
Can anyone help me to give me a lead to solve this problem?
<div id="fnotification-{{ module }}" class="f-notification animated">
<div class="notification-block">
{% if (notification['close_status']) %}<button class="fclose-notification">×</button>{% endif %}
<div class="notification-image">
{% if (custom_icons) %}
{% if (thumb) %}
<img src="{{ thumb }}" title="{{ title }}" alt="{{ title }}">
{% endif %}
{% else %}
<i class="fas fa {{ font_icon }}"></i>
{% endif %}
</div>
<div class="notification-text-block">
<div class="notification-title">{{ title }}</div>
<div class="notification-text"></div>
</div>
</div>
</div>
<script type="text/javascript">
$('#fnotification-{{ module }}').fNotification({
uId : {{ module }},
Cities : {{ json_encode(city) }},
Names : {{ json_encode(cname) }},
Products : {{ json_encode(products) }},
Amount : [{{ min_amount }}, {{ max_amount }}],
Text : '{{ text }}',
// Time Duration
DelayFirstMin : '{{ delay_first }}',
DisplayTime : '{{ display_time }}',
DelayMin : '{{ delay_between }}',
AnimationEffectOpen : '{{ in_animation }}',
AnimationEffectClose : '{{ out_animation }}',
DisplayTimes : '{{ cutomer_session > 0 ? cutomer_session : 0 }}',
CloseLifetime : '{{ cookie_time }}',
// Position
position: '{{ position }}',
postionTop : {{ top != '' ? top : '' }},
postionBottom : '{{ bottom != '' ? bottom : '' }}',
postionLeft : '{{ left != '' ? left : '' }}',
postionRight : '{{ right != '' ? right : '' }}',
NotificationSound :'',
// Style
borderRadius : '{{ border_radius }}',
borderWidth : '{{ border_width }}',
borderColor : '{{ border_color }}',
backGround : '{{ bg_color }}',
TextColor : '{{ text_color }}',
LinkColor : '{{ link_color }}',
Width : '{{ popup_width }}',
Height : '{{ popup_height }}',
// Close Button Color
BtnCloseColor : '{{ close_color }}',
BtnCloseHoverColor : '{{ close_hover_color }}',
});
</script>
Twig syntax for json is data|json_encode()
Just change:
Cities : {{ json_encode(city) }},
Names : {{ json_encode(cname) }},
Products : {{ json_encode(products) }},
To:
Cities : {{ city|json_encode() }},
Names : {{ cname|json_encode() }},
Products : {{ products|json_encode() }},

Grav - Using shortcodes in twig

Following this question, I have found that there is a plugin named shortcodes which might be helpful here.
I have installed the plugin and created a new page:
---
title: slide1
media_order: slider1.jpeg
taxonomy:
category:
- 'Home Slider'
process:
markdown: true
twig: true
routable: false
cache_enable: false
visible: false
---
[section name="slide_title"]
Precisión y Calidad
[/section]
[section name="slide_subtitle"]
La máxima calidad y seguridad para nuestros clientes
[/section]
[section name="slide_link"]
www.google.es
[/section]
Then I have an invoked partial which makes:
{% for slide in taxonomy.findTaxonomy({'category': 'Home Slider'}) %}
<span>TEST</span>
<span>{{ slide.title }}</span>
<span>{{ slide.shortcode.section.slide_title }}</span>
<span>{{ slide.shortcode.section.slide_subtitle }}</span>
<span>{{ slide.shortcode.section.slide_link }}</span>
{% endfor %}
But this generates just:
TEST
slide1
And it does not output the shortcode section contents.
The documentation you linked to shows the correct syntax when you are not dealing directly with the page variable:
{{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_title }}
{{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_subtitle }}
{{ slide.contentMeta.shortcodeMeta.shortcode.section.slide_link }}

How to add a style to a {{ form_label(form.name) }}

I'm using Symfony 3.0, in this moment I have this code:
{{ form_label(form.name, 'Name of the card', { 'attr': {'style': 'color:white'}}) }}
Thank you :) !
You need to use label_attr for labels, attr is for the widget function.
{{ form_label(form.name, 'Name of the card', { 'label_attr': {'style': 'color:white'}}) }}
According to the documentation rendering form row involves three functions
{{ form_label(form.name) }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
You can replace form_label(form.name) with your layout
<div class="form_label">Name of the card</div>
<div class ="form_errors">{{ form_errors(form.name) }}</div>
{{ form_widget(form.name) }}

Resources