I'm using Jekyll with the Jekyll-RDF extension to render some pages for RDF resources.
I know how to access the properties of the currently rendered resource in a template.
But now I want to access a property of a resource which is linked to the currently rendered resource. So to say I want to make a double hop. But how does that work?
Here is my MWE
The graph:
#prefix ex: <http://example.org/> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
ex:nabatz a foaf:Person ;
foaf:nick "Nabatz" ;
foaf:currentProject ex:project .
ex:project a foaf:Project ;
foaf:name "A Project" .
The template:
---
---
<html>
<body>
<h1>{{page.rdf | rdf_property: "<http://xmlns.com/foaf/0.1/nick>"}}</h1>
<dl>
<dt>Current Project</dt>
<dd>{{page.rdf | rdf_property: "<http://xmlns.com/foaf/0.1/currentProject>"}}</dd>
</dl>
</body>
</html>
The output:
<html>
<body>
<h1>Nabatz</h1>
<dl>
<dt>Current Project</dt>
<dd>http://example.org/project</dd>
</dl>
</body>
</html>
What I want:
<html>
<body>
<h1>Nabatz</h1>
<dl>
<dt>Current Project</dt>
<dd>A Project</dd>
</dl>
</body>
</html>
There are two options to achieve this, depending on what else you want to do with the linked resource.
Option 1
Just chain the rdf_property filter:
{{page.rdf | rdf_property: "<http://xmlns.com/foaf/0.1/currentProject>" | rdf_property: "<http://xmlns.com/foaf/0.1/name>"}}
Option 2
Assign a variable with the linked resource node:
{% assign node = pade.rdf | rdf_property: "<http://xmlns.com/foaf/0.1/currentProject>" %}
{{ node | rdf_property: "<http://xmlns.com/foaf/0.1/name>" }}
If you want to output multiple properties of the linked resource it is better to use Option 2, while Option 1 is more compact.
Related
#views.py
.
.
.
detailedStatement = {
'selectedOption1' : '90 degrees',
'correctAnswer1' : '180 degrees',
'selectedOption2' : 'angle',
'correctAnswer2' : 'side'
}
#Above dictionary contains 200 elements
diction = {
'detailedStatement' : detailedStatement
}
return render(request, "result.html", diction);
So while on an html file I wanted to access the dictionary's every element via a loop. Like every element should be listed in the html table row like following.
| Sr | Selected Option | Correct Answer |
| 1 | 90 degrees | 180 degrees |
| 2 | angle | side |
Above table is just a representation of html table not an actual table.
But the issue I am facing is... I am not able to access its index in a dynamic way.
I wrote a for loop in Django html template but
{% for dr in detailedResult.items %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{dr.option.forloop.counter}}</td>
<td>{{dr.answer.forloop.counter}}</td>
</tr>
{% endfor %}
I want my code to automatically put 1 after option and answer like option1, answer1;
How can we do this?
I think you should model this with a list instead of including an index in your variable names, e.g.
# views.py
detailed_statements = [{'option': '90 degrees', 'answer': '180 degrees'}, ... ] # contains 200 elements
Then in your template
{% for dr in detailed_statements %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{dr.option}}</td>
<td>{{dr.answer}}</td>
</tr>
{% endfor %}
I was a bit confused by the nested for loops in your template code - I think you only need one?
I'm trying to grab a specific item from a collection called 'content' based on an id using where_exp, but for the life of me I can't get it to work. Here's the code:
filter:
{% assign var = site.content | where_exp:"content", "content.id == 'testId'" | first %}
frontmatter for post in collection:
---
layout: content
title: "This is the title"
image: "assets/photos/image.jpg"
id: "testId"
---
html:
<img class="full-width-poto" src="{{ var.image }}">
I can't figure out what I'm doing wrong.
Note, I've been referring to this post: Getting a specific item from a collection in Jekyll and https://riptutorial.com/jekyll/example/28446/accessing-a-specific-collection-item
Ok, I figured out my problem, just in case someone comes across this. For some reason I can't use the key 'id' for this...it must be hardcoded for something else.
I swapped in 'myid' and it works fine now...
I have a CSV file in my _data folder and I am trying to specify the row of the CSV to access in my Front Matter and then return specific columns from the CSV based on the row specificed in the Front Matter.
Here's the CSV file:
name,description
Dallas,Big City in Texas
And here's the contents of my index.html file:
---
city: "Dallas"
---
{{ site.data.data[page.city].description }}
Per the Jekyll Docs page on using data files, I should be able to use this syntax to access data files in this way, but the compiled html file does not include any data.
I have tested other ways of accessing the contents of the CSV file and those work, so it doesn't appear to be a problem with the data file or the site itself but rather with using the [ ] array id Liquid syntax.
Looks like you have misunderstood the [] notation for a hash structure.
I will first orient you on how the [] is supposed to work..
Expanding your data.csv a bit:
name,description
Dallas,Big City in Texas
Houston,Another City in Texas
and "inspecting" your the data object obtained from the above CSV file,
{{ site.data.data | inspect }}
on building the site, you'll see that the resultant object is simply an Array of Hashes :
<p>
[
{"name"=>"Dallas", "description"=>"Big City in Texas"},
{"name"=>"Houston", "description"=>"Another City in Texas"}
]
</p>
which means you can only access individual hash entry by referencing its index number.
i.e. {{ site.data.data[0] }} will give you the first hash and {{ site.data.data[1] }} will give you the next hash.
and therefore {{ site.data.data[0].description }} will give you the result you expect to get:
<p>
Big City in Texas
</p>
Now that you know how [] works for data hashes, lets simply get to the solution.
To access elements in an Array, one can simply iterate through the Array objects and reference necessary entries:
{% for entry in site.data.data %}
<div>
<span>{{ entry.name }}</span> : <span>{{ entry.description }}</span>
</div>
{% endfor %}
will give you:
<div>
<span>Dallas</span>
<span>Big City in Texas</span>
</div>
<div>
<span>Houston</span>
<span>Another City in Texas</span>
</div>
I created a new template and want to display a list inside my newly coded infobox. To get this working I was told to change the code like in this German tutorial.
This is how my template Infobox Music currently looks like:
|-
{{#if: {{{Sänger<includeonly>|</includeonly>}}} |
{{#ifeq: {{Str left|{{{Sänger}}} }} | *
|{{!}} style="vertical-align:top;" {{!}}
'''Sänger/in'''
{{!}}
{{{Sänger}}}
|{{!}} '''Sänger/in'''
{{!}} {{{Sänger}}}
}}
}}
In order to build a new list I edited the source code in a seperate Wiki entry like this:
{{Infobox_Music
|Sänger =
* Lady Gaga
* Michael Jackson
}}
Unfortunately, when using both of these settings my list gets displayed with the first item having an * at the beginning just as usual. Here is how it looks in HTML:
<tr>
<td> <b>Sänger/in</b>
</td>
<td> * Lady Gaga
<ul><li>Michael Jackson</li></ul>
</td></tr>
Did I miss something? What does the line {{#ifeq: {{Str left|{{{Sänger}}} }} mean?
UPDATE: Here is a snippet of my previous Infobox Music template:
{{Infobox Music
|-
{{#if: {{{Sänger|}}} | {{!}} '''Sänger/in''' {{!!}} {{{Sänger}}} }}
}}
{{Str left|{{{Sänger}}} }} is the first character of the Sänger parameter (see {{Str left}}). {{#ifeq: {{Str left|{{{Sänger}}} }} | * | ... is some kind of horrible hack to use a different layout when the parameter is a list; not trying to replicate that will help you preserve your sanity.
* will only be interpreted as a list when preceded by a newline. You can prevent stripping of the leading whitespace from the template parameter by doing something like
|Sänger = <!---->
* Lady Gaga
or you can make sure that in the template {{{Sänger}}} is preceded by a newline.
I'm using assemble for prototyping a new site.
I would like to modularize my code quite drastically, much like Brad Frost is evangelizing with his pattern lab.
EXAMPLE
Basically I would like to have a title partial ("atom" in pattern-lab speak) which is used inside a hero partial ("molecule"):
title.hbs
<h1 class="{{class}}">{{text}}</h1>
hero.hbs
<section class="hero-unit">
{{!-- image and stuff --}}
<header class="hero-description">
{{> title }}
{{> subTitle }}
</header>
</section>
The hero partial is supposed to generic; I want to pass in data from JSON files per particular page. For my pages, I use a default layout that offers blocks. For example:
default.hbs
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
{{#block "hero"}}{{/block}}
{{#block "body"}}{{/block}}
</body>
</html>
myPageWithHero.hbs
{{#extend "default"}}
{{#content "hero"}}
{{ >hero }}
{{/content}}
{{#content "body"}}
{{!-- myPageContent --}}
{{/content}}
{{/extend}}
Now I'd like to set {{text}} inside the title partial which is inside the hero partial via the myPageWithHero.json file that I have. Is that at all possible? Or is my approach (which I have described in this very oversimplified example) completely deranged?
Cheers for any pointers! :-)
#polarbirke since you want to use the data from myPageWithHero.json, that data will be available on the page object when rendering myPageWithHero.hbs, so you can pass that object to the hero partial. This will setup the context for that partial and the title partial will inherit that context:
{{#extend "base"}}
{{#content "hero"}}
{{> hero page}}
{{/content}}
{{#content "body"}}
{{!-- myPageContent --}}
{{/content}}
{{/extend}}
If you have other objects in your data that you'd like to use instead, you can pass that instead:
data.json
{
"title1": {
"class": "success",
"text": "Good Title"
},
"title2": {
"class": "error",
"text": "Bad Title"
}
}
myPageWithHero.hbs
{{#extend "base"}}
{{#content "hero"}}
{{> hero title1}}
{{/content}}
{{#content "body"}}
{{!-- myPageContent --}}
{{/content}}
{{/extend}}
I hope this helps, let me know if you have any questions.