I've created a file called people.yml inside of the _data folder. I have a page people.html where I'm trying to access the information from the data file. It seems like I can only get the info when I create a for loop. I'm referencing the examples found here to accomplish this. Can't figure out what I'm doing wrong.
people.yml
- id: 123
name: John Does
type: A
people.html
<h1>Person</h1>
<p class="name">{{ site.data.people.name }}</p>
<span class="type">{{ site.data.people.type }}</span>
You've made the data in your people.yml file an array by adding the -. If you want to access it not in a loop, you'll need to remove the hypen.
id: 123
name: John Does
type: A
Then calling {{ site.data.people.name }} should return "John Does".
Related
I am trying to read a variable which is defined in my javascript. It is a get request from a mongoDB. The whole database is then stored under a variable and is read by the HTML and displayed.
The get request from the MongoDB has an output like this:
(Lets say this is stored under the variable database):
[
{
0:
{
TITLE1: valueone
}
},
{
1:
{
TITLE2: valuetwo
}
}
]
My HTML looks like this:
<p> {{ database?.TITLE1 }} </p>
I get the error Cannot read property '0' of undefined. I understand this is because I need to define [0] to be able to read TITLE1.
Based on this I have tried the following:
<p> {{ database?[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Conditional expression database?[0].TITLE1 requires all 3 expressions at the end of the expression [{{ database?[0].TITLE1 }}]
<p> {{ database?.[0].TITLE1 }} </p>
This has this error: Template parse errors:
Parser Error: Unexpected token [, expected identifier or keyword at column 7 in [{{ database?.[0].TITLE1 }}]
<p> {{ database?.0.TITLE1 }} </p>
This has the same error as the one above.
What is the correct way to be able to read the values that I am after. In the HTML the output should be valueone.
Because your database is array, use need use [0] to get first item and your key is number so you need use ['0'] to get property value, you also can use ? to check object null before using TITLE1
Finally you can use {{ database[0]['0']?.TITLE1 }}
Demo https://stackblitz.com/edit/angular-o79rra
Well I mean, at some point you have to think a little about JS basics instead of trying everything until it works ...
From the code you have provided, the syntax would be
{{ data[0]['0'].TITLE1 }}
Try
<p> {{ database[0]['0']?.TITLE1 }} </p>
Do it dynamically by:
<p *ngFor="let item of database; let i = index">
{{item[i]['TITLE' + (i + 1)]}}
</p>
I am trying to build a hash in order to later output is as JSON (and ultimately import it to be reused by a a script). This is part of my static site built with jekyll.
Following the documentation on Expressions and Variables, I created a file with
---
---
{% assign aaa['bbb'] = 'xxx' %}
{{ aaa | jsonify }}
This was compiled by jekyll to a null (as if the hash was not initialized). Why is it so?
Sadly the documentation is talking about reading hash or arrays, not writing.
The only thing you can write from liquid is arrays.
create an empty array : {% assign my-array = "" | split: "/" %}{{
y-array | inspect }}
store with push or shift {% assign my-array = my-array | push: anything %}
= empty-array }}, where anything can be a string, an integer, a hash or an array.
I am currently getting familiar with salt and wonder how I could re-use the values of pillars in other places (sections) in .sls files.
In buildout, I would be able to reference a variable from another section with ${sectionname:varname} to re-use a once defined value. This is especially handy, when dealing with directories (basedir, appdir). buildout example:
['foo']
path = /highway/to/hell
['bar']
path = ${foo:path}/lastexit
When I try to reference another variable in an .sls file, even if it is in the same file, I get always None. salt example:
foo:
path: /highway/to/hell
bar:
path: {{ salt['pillar.get']('foo:path') }}/lastexit
salt-ssh minion1 pillar.get bar:path results in None/lastexit
I have the feeling, that I'm missing something here. Could someone point out, how one does re-use values in salt .sls
You can use jinja to assign a value, e.g.:
{% set base_path = salt['pillar.get']('foo:path','/highway/to/hell') %}
foo:
path: {{ base_path }}
bar:
path: {{ base_path }}/lastexit
In this case "/highway/to/hell" is set as a default in case no value is assigned in the pillar or no pillar is found. For more info, see https://docs.saltstack.com/en/latest/topics/jinja/index.html
i want to generate a list of ip addresses (using range of last 8bits so 120-190 translates to x.x.x.120 - x.x.x.190) in defaults/main.yml in my role, and later use it to create new network interfaces and generate a new config file. I tried this approach:
defaults/main.yml:
ip_list: "{%for address_end in range(50,99)%} 192.168.0.{{address_end}}{%endfor%}"
conf_list: "{%for ip in ip_list%}server {{ip}}:6666 {%endfor%}"
and then to use it in template
template.conf.j2:
{% for conf_line in conf_list %}
{{conf_line}}
{% endfor %}
and all i got in generated config file was:
s
e
r
v
e
r
:
6
6
6
6
s
e
r
v
e
r
1
:
6
so my guess is that i'm not generating a list but just a long string and when I use for loop in template.conf.j2 I iterate over single chars. I tried using answer to this problem but all i got was syntax error. Any ideas for what might help me ?
You should format your vars as JSON lists if you want them to be lists.
1.1.1.1 2.2.2.2 3.3.3.3 is a string.
['1.1.1.1', '2.2.2.2', '3.3.3.3'] will be converted to list.
But there is alternative approach for you:
ip_list: "{{ lookup('sequence', 'start=50 count=12 format=192.168.0.%d', wantlist=True) }}"
conf_list: "{{ ip_list | map('regex_replace','(.*)','server \\1:6666') | list }}"
Kostantin answer was of much help, but i found just realized that generating config entries in my case could be solved in an less complex way. Instead of trying to iterate over list or a string variable variable in jinja template file template.conf.j2 like did with :
{% for conf_line in conf_list %}
{{conf_line}}
{% endfor %}
you could just enter insert new line signs while generating string in defaults/main.yml:
conf_list: "{%for ip in ip_list%}server {{ip}}:6666\n{%endfor%}"
and then just insert that whole string into a template.conf.j2 like this:
{{conf_line}}
nevertheless i have no other idea how to generate list of ip addresses other than the one Konstantin proposed.
I have a strange looking JSON file (I think?) generated from elasticsearch.
I was wondering if anyone know how I could retrieve the data from a JSON object looking like this:
u'hits : {
u'hits : [{
u'_score' : 2.1224,
u'_source' : {u'content': u'SomethingSomething' }
}],
u'total: 8 }
u'took: 2 }
I can retrieve the total by writing {{ results.hits.hits.total }}, however, the underscore symbol (_) in front of the attribute name "_score" makes it impossible to retrieve the value of that attribute.
Any suggestions?
Try:
{{ results.hits.hits[0]._score }}
{{ results.hits.hits[0]._source }}