all_products[] Not working with JSON variable - json

I am trying to retrieve a metafield with all_products[] but using a locale json file content as the handle.
I have created this value in the en.default.json. If I just use {{'products.product.product_handle' | t}} I receive the proper handle so I know that I'm getting the right information but for whatever reason I can't pass that value into the all_products[] function.
I've tried the following code:
{{all_products[ 'products.product.product_handle' | t ].metafields.my_fields.cardtext}}
and
{% assign text = 'products.product.product_handle' | t %}
{{all_products[text]..metafields.my_fields.cardtext}}
Json Setup:
"product_handle": "{{ handle }}"
Can anyone tell me what I'm missing or if there is another way to display this content?

The first syntax is invalid.
The second syntax is almost valid, you have double dots before metafields => ..metafields
So when you fix the above as long as you are targeting the correct translatable string and metafield it should work:
{% assign text = 'products.product.product_handle' | t %}
{{all_products[text].metafields.my_fields.cardtext}}
You can check if you are getting the product first, for example:
{{all_products[text].id}}
If you are getting the product but not the metafield, then the issue might be in the metafield target.

Related

Go template to show text if an exact value of a refered attribute

I'm very new in go templates and honestly I did not understand yet some of the main logic of it, therefore I hope you can turn me into the right direction.
I have a simple JSON for the test (in fact this is the JSON-ized output of a Grafana alerting):
{
"_15_min_reduced": 1.3122222222222208,
"_1_min_reduced": 1.7260000000000002,
"alert": 0,
"cpu_cores_reduced": 4
}
How can I show a 'message' based on go templates only in case of 1 value of the alert key?
My basic misunderstanding is somewhere here:
{{ . }} shows me the whole map of the JSON node, it's fine.
{{ range . }}
{{ . }}
{{ end }}
shows me all the values of the keys as a sequence, but not the keys themselves.
{{ .alert }} shows me the value of the 'alert' key (in our sample case is 0).
But...
{{ if eq .alert 1 }}
There are problems...
{{ end }}
...gives me an 'error'. Seems I can not refer in such was to the defined key, and its value, what is a bit confusing me, as when I directly 'ask' for its value I get the correct result.
Please help me, what is the point where I lost in the go template structure/syntax?!
Lot of thanks for it...
It was a hard run, what I already thought and saw in at firt time, but cound not confirm and verify...
{{ printf "%T" .alert }} shows me that the type of the key is 'float64', what is really incomparable with an 'int' zero. So with a little 'trick'...
{{ if eq .alert 1.0 }}
There are problems...
{{ end }}
...is already working as in this case 1.0 is a comparable 'float' format.
I tried to convert the value of the .alert to 'int' lot of times/methods but sadly at last unsuccesfully, this is why I slipped into the hole... :(
Really basics, really lame, really my bad, but I hope it could help to anyone in the future...
----- UPDATE #1 ----
Sadly while this final syntax is fine, Grafana still can not accept it so from now I leave it alone :(

cant get inside key of an array that is numbers

I have an array that I use to create a table, however, I first need to identify the path.
my Array is saved in:
page.cart.lineitems.elements
this is the output if i dump it:
in these two keys are my table arrays but I can't get inside.
The keys are named after 2 items in a cart where I can identify which one I want to use, so I get the values from a key called:
im saving the value of "_key" here:
{% set articleKey = _key %}
so now I should be able to use the key name as such
page.cart.lineitems.elements.articleKey
instead of page.cart.lineitems.elements.120ac316d177487fa879c51e02845135
inside the articleKey.extensions.TwpSortiment.data is
so I wrote this code but it still doesn't go inside my "articleKey"
<div class="twp-sortiment-form">
{% set articleKey = _key %}
{% for key in page.cart.lineitems.elements.articleKey.extentions.TwpSortiment.data %}
//my table code
{% endfor %}
</div>
whats is the issue? why can I not go inside the articleKey? I tried it even with writing the whole name but an error occurs:
Unexpected token "name" of value "ac316d177487fa879c51e02845135" ("end of statement block" expected).
fixed it by typing the path this way page.cart.lineitems.elements[articleKey]

How to get value from object angular using *ngFor?

I have a json structure like the image above.
I just want to get the value from the key name.
I was do Like this :
<span *ngFor="let outlet of products.body | keyvalue">{{outlet.value}}</span>
but that way is to call all attributes, I only want to call attribute name. how do?
Sorry for bad grammar, any suggestion or answer will be appreciate. Thank you
If you only want to get the value of name key, there is no need to use *ngFor.
It's enough to put it directly as follows.
<span>{{ products?.body?.name }}</span>
Please bind
{{outlet.name}}
instead of {{outlet.value}}

Jekyll "where" filter with site.data doesn't seem to work as expected

I am trying to use what seems like a straight-forward construct to get a single "record" out of one of my data files by using the where filter, without much luck.
I am able to use most of the liquid features/constructs successfully but this one has me stumped. It does not produce an error in the build console, and I know for certain that the data in the file is good because I can accomplish the same thing via a less optimal route of using a for loop and only using the one record I need. Using this assign with the where filter seems to build fine but the place where I expect to see the output is blank.
Here is the important code:
products.yml:
-
id: FL01
name: Standard Kit
title: Scoring Area
price: $80
svg: ScoringAreasSvg
-
id: FL05
name: Full-field Kit
title: Full Field
price: $220
svg: FullFieldSvg
template:
<div>
{% assign theproduct = site.data.products | where:"productId", "FL01" %}
<p>{{ theproduct.name }}</p>
</div>
I would expect to see Standard Kit in the <p> but it is just empty.
I think you should write:
{% assign theproduct = site.data.products | where:"id", "FL01" | first %}

Putting HTML in a hidden form field in Django

I'm having a problem with a template: I'm trying to display a form for changing a value, in which the user enters the current value in a textarea and the old value is kept inside a hidden field for auditing purposes. This value is generally some HTML, and when I render the page this HTML in the hidden field seems to get partially rendered: the value attribute of my hidden field gets closed by the first quotation marks inside the entered HTML, and the rest of the HTML spews out onto my page. I've tried using the escape decorator but that hasn't changed anything.
Firstly, a better solution might be to keep the audit value in a separate model field defined with editable=False. You can still perform checks against the value in a form's clean method:
def clean(self):
cleaned_data = super(SomeForm, self).clean()
if instance.the_audit_field == cleaned_data['the_editable_field']:
...raise a validation error?
You can also modify the value of the audit field from within the model's save method.
Secondly, assuming you must do it the way you are now, let me address the non-escaped value in your template. I assume you're using something like the following:
<textarea value="{{ form.the_audit_field.value }}"></textarea>
You should instead use the following:
<textarea>{{ form.the_audit_field.value }}</textarea>
Note, the value goes inside the textarea, instead of in the value attribute of it.
An even better way to do it is to simply allow Django to render the field for you like the following:
{{ form.the_audit_field }}