Uncaught Twig_Error_Syntax: Unknown "json_encode" function opencart - twig

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() }},

Related

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.

Spaceless tag inside script block

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.

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 }}

Showing category thumbnails in opencart 3 menu

I'm looking for a way to show the category thumbnail in the menu. Is this possible?
Specifically for OpenCart 3.
OpenCart 3.0.2.0, Default theme
Open this file:
catalog\controller\common\menu.php
Find:
$this->load->model('catalog/product');
Add after it:
$this->load->model('tool/image');
Find:
$data['categories'][] = array(
Replace with:
if($category['image']){
$image = $this->model_tool_image->resize($category['image'], 30, 30);
} else {
$image = false;
}
$data['categories'][] = array(
'image' => $image,
Then open this file:
catalog\view\theme\default\template\common\menu.twig
Find:
{{ category.name }}
There are three occurrence, add before first and last one:
<img src="{{ category.image }}" alt="{{ category.name }}"/>{% endif %}
Here is the result:
for submenu use:
$children_data[] = array(
'image' => $child['image'] ? $this->model_tool_image->resize($child['image'], 20, 20) : false,
or
'image' => $child['image'] ? $this->model_tool_image->resize($child['image'], 20, 20) : $this->model_tool_image->resize('your-default-image.jpg', 20, 20),
in template
{% for child in children %}
<li>{% if child.image %}<img src="{{ child.image }}" alt="{{ child.name }}" />{% endif %}{{ child.name }}</li>
{% endfor %}
If missed something, follow here

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