python flask serve several pictures by list - html

Ok, so let's say I use listdir to make an object of pictures, their names, sizes and their file names. The object is parsed so that each file name is item.values()[0]['item']. I am trying to do:
{% for item in banners %}
<h3>{{item.keys()[0]}} size {{item.values()[0]['size']}}:</h3>
<div>
<img src="{{url_for( 'static', filename= 'banners/{{item.values()[0]["item"]}}' )}}" />
</div>
{% endfor %}
But it is not working, replacing {{item.values()[0]["item"]}} with a specific file name is working and I can see that the file names are in the object, yet it will not generate the image, instead it says it failed to load the given url and the urls are weird, for example one shows up like:
<img src="/static/banners/%7B%7Bitem.values%28%29%5B0%5D%5B%22item%22%5D%7D%7D%7Csafe">
Something to do with it interpreting the characters as is instead of what they are to represent, but I cannot figure out the syntax. Thanks.

You are already in a {{ }} block, so nesting more of those is incorrect. I think it should be something in the lines of :
filename = 'banners/' + item.values()[0]["item"]

Related

Can a liquid for loop contain a page variable in Jekyll?

Let's say I have a bunch of _data files that I use to create a list for specific pages. All of the pages that have these lists have a custom front matter variable of pageName. The value of pageName just so happens to match the _data file with the list.
Rather than pasting the html blocks of code into each page, I'd like to use an include to pull in a generic block of html. To do that, the include has to be dynamic or contain liquid markup to become dynamic.
That might be more context than you need, but my question boils down to this, is something like this possible:
{% for item in site.data.{{page.pageName}} %}
{{ item.label }}
{% endfor %}
Unless I'm missing something, this doesn't work so I'm wanting to know if there's a way of achieving similar functionality.
I found this similar question, but it doesn't seem to answer whether it can be used to access a page variable to build a file name.
You can use the bracket notation like this :
{% for item in site.data[page.pageName] %}

How do I inspect Pelican variables

I'm modifying a Pelican template and I have the code below which adds url every time a page is found. I can see that the p object has the attributes url and title.
However I only knew this because I copied the code from another template shown below. Is there any way to inspect objects in jinja2 or Pelican to understand what information is contained within them?
{% for p in pages %}
<h1 class = "sidebar-title">
<a href="{{ SITEURL }}/{{ p.url }}">
{{ p.title }}
</a>
</h1>
https://github.com/getpelican/pelican-themes/blob/master/backdrop/templates/base.html
<li{% if p == page %} class="active"{% endif %}>{{ p.title }}</li>
I am not aware of an official resource explaining all variables, objects, attributes and properties in detail.
But for a start, I think the following start points suffice:
Common variables available for the standard templates
pelican.contents.py:
This is the module which contains (most of) the data structures pelican uses and which are made available in the templates. Have a look for properties (#property, these are functions which act like they are attributes) and attributes. At lines 367ff there are some very simple subclass definitions which could be of use.
pelican.writers.py: This module brings together the templating engine jinja2, the templates and the data to be inserted in the templates. Of special interest for you could be lines 138ff, as this seems like a good point to simply insert some small debug prints to see the real data which is present in the data structures.

import template with context doesn't seem to be working

I've recently been trying to get to grips with nunjucks, I've used handlebars before so the learning curve hasn't been that bad. However there is one thing I can't seem to get working:
I've got a JSON file which has the following data in it:
"contentIntro" : {
"componentClass" : "c-global-header",
"title" : "Welcome Firstname Surname,",
"subtitle" : "New applications:",
"bodyCopy" : "You can create, edit and save the draft application as many times as you need before submitting it for assessment.",
"ctaType" : "text",
"ctaText" : "View the list",
"ctaURL" : "#"
}
This exists in a JSON file which includes other page elements but it's only the contentIntro context I'm interested in.
If I include a partial like this:
{% include "components/c-headed-text.nunjucks" %}
then I can access the JSON data using dot notation within that partial (eg {{ contentIntro.title }}), however this isn't very flexible and it precludes me having more than one of the same partial type on the page.
Ideally what I'd want to do is import the partial like this:
{% import "components/c-headed-text.nunjucks" as contentIntro %}
and then in the template I can just access the JSON data using {{ title }} as it will already know the context from the import.
Annoyingly though it doesn't work and I can't see why. The Jinja2 documentation seems to suggest it would but I can't get it to work using Nunjucks, is this even possible?
Edit: I should also add that the templates are being prerendered in gulp so the end result is just flat HTML.
Ok I've found a way around it. it's not as clean as I'd like but it does the job.
I just a normal include statement:
{% include "components/c-headed-text.nunjucks" %}
but above it a set a variable as follows:
{% set context = contentIntro %}
Then inside the partial I can just use {{ context.title }} and it works like a charm!

Page variable in for loop file name

For a bilingual website, i have yaml data files for 2 languages.
files example:
en_services.yml
fr_services.yml
Variable example in my page:
---
lang: en
---
I want to loop trough the file with the lang as the prefix, something like that:
{% for service in site.data.{{ page.lang }}_services %}
{% endfor %}
This doesn't work, is there a way I can do that?
By the way, I don't think I can add subfolders in the _data folder, right?
Thanks.
While that doesn't work, if you are able to put them both in the same file (grouped under the appropriate language code) there is a solution.
This gist was for another example based on post authors, but your should be able to use the same setup using language codes instead of author names.
You should use:
site.data[{{ page.lang }} + '_services']
data[i] has key/value entries for all the files in your _data directory, and passing the string in, like so (for /_data/book.yaml):
site.data['book']
...totally works and opens up the possibility of concatenating strings inside the brackets with whatever variable you want. :)
{% capture thefile %}{{ page.lang }}_services{% end capture %}
{% for service in site.data[thefile] %}
...
{% endfor %}
Should do the trick.

Is there a way to evaluate string with liquid tags

I need to provide page content reference list (it should contain references on sections on page).
The only way which I can see is to use page.content and parse it, but I stumbled on problem with data evaluation. For example I can pull out this string from page.content: {{site.data.sdk.language}} SDK but there is no way to make jekyll process it, it outputs as is.
Also I want to make it possible to create cross-pages links (on specific section on page, but that link generated by another inclusion and doesn't persist in page.content in HTML form).
Is there any way to make it evaluate values from page.content?
P.S. I'm including piece of code which should build page content and return HTML with list (so there is no recursions).
P.P.S. I can't use submodules, because I need to run this pages on github pages.
Thanks.
Shouldn't {{ site.data.sdk.language | strip_html }} do it? I don't know, most probably I didn't understand the problem. Can you elaborate more? Maybe provide a link to a post you're referring to?
Thinking about the similar
{% assign title = site.data.sdk.language %}
which is a stock Liquid tag and does the job well, so instead of
{% section title={{site.data.sdk.language}} %}
write your code as
{% section title = site.data.sdk.language %}
The key here is that once you enter {%, you're in Liquid. Don't expect Liquid to go "Inception" all over itself. {{ is just a shorthand for "print this to output", but an parameter passing is not output, it's just reading a variable.
You should be able to also go wild and do:
{% section title = site.data.sdk.language | capitalize %}
For more re-read the docs: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers