Adjustable Forloop Range - HTML - html

I have an html file in my django templates that contains a forloop. I need to to have a range on the forloop that basically says the forloop will only loop through the first 10 attributes. The complication arrises when I need the range to be adujstable. I need something like a number input that keeps a varaible and then only loops through that many attributes in the forloop. I have been on it for a few days and havent made too much progress. I have worked with simple forloop ranges but havent found a way to make them adjustable. This is the forloop I have been working with.
{% for post in filter.qs %}
{% if post.image %}
<img class="pdf_field_img" src="{{ post.image.url }}"/>
{% else %}
{% endif %}
<div class="title">Morgan Hall</div> <div class="inspection_num">Inspection #</div> <div
class="inspect_num">{{ post.count_building }}</div>
{% endfor %}

Related

How to check if an objects attribute is given?

I am trying to make a little website with django when I ran into a problem:
I have a site where I want to look at a post in detail, but not every post has an image attribute - so I am getting an error when I try to display images, since some arent existent.
So the solution would be to check if an image is given, but... How do I do that?
I tried something like this but it did not work:
</div>
<p>{{ object.content }}</p>
{% if object.image.url == True %} <!-- In no case an image is displayed -->
<p>
<img src="{{ object.image.url }}">
</p>
{% endif %}
</div>
You should check the truthiness object.image attribute, not its URL, so:
{% if object.image %}
<p><img src="{{ object.image.url }}"></p>
{% endif %}

Jekyll: Previous/Next navigation when using custom navigation

The official Jekyll tutorial has an entire section on using a YAML file to define a custom sequence of pages: https://jekyllrb.com/tutorials/navigation/
But it doesn't mention anywhere how one might create previous/next navigation buttons on the pages within that sequence, which is particularly ironic considering that the tutorial itself has them.
I've come up with some Liquid to determine the index of the current page:
{% for item in site.data.encore.docs %}
{% if item.url != page.url %}
{{ item.title }}: {{ forloop.index }}
{% else %}
<strong> This page index: {{ forloop.index }}</strong>
{% assign this_page_index = forloop.index %}
{% break %}
{% endif %}
{% endfor %}
but getting the index of the previous page via {% decrement this_page_index %} always returns -1 for some reason, and something like {% assign previous = this_page_index - 1 %} isn't valid Liquid. Same goes for trying to get the next page with similar methods.
What's the ideal way to accomplish this? I've searched every way I can think of and not found anything.
You can find the code for Jekyll's own navigation on their tutorials page by sifting through their GitHub repo until you get to their section_nav_tutorials.html, but it appears the way to do it is very close to what you have.
Liquid doesn't respect you doing math directly, you have to use a filter. For you, you'd use {% assign previous = this_page_index | minus: 1 %}.

Jekyll and modular/atomic design

I am currently looking at developing a "static" website, few pages only. However, by design, I can tell there is going to be repetitive layouts/patterns. I am thinking doing a data-oriented approach, with my HTMLs being as reusable as possible. Here is an example:
index.html:
<div>
{% include organisms/topBanner.html
tp-title=site.data.home.topbanner.title
tp-select-blurb=site.data.home.topbanner.select.blurb
button-text=site.data.generic.buttons.getstarted
button-link=site.data.generic.links.gosomewhere
%}
</div>
then my organisms/topBanner.html:
<div class="tb">
<h1>
{{ include.tp-title }}
</h1>
<div>
<h2>{{ include.tp-select-blurb }}</h2>
<div>
{% include atoms/button.html
%}
</div>
</div>
</div>
finally my atoms/button.html:
<a class="button" href="{{ include.button-link }}">{{ include.button-text }}</a>
I have multiple JSON file under _data that basically hold the texts. An example for the button would be a _data/generic/buttons.json:
{
"getstarted": "GET STARTED",
"completesurvey": "COMPLETE THE SURVEY"
}
or links.json:
{
"gosomewhere": "/go-somwhere",
"surveypage": "/survey"
}
So this means you need to pass all your data from the top level include of the organism so every bits in it would have its data. That way the example of that button is that the HTML is defined only once and the data is bound to it. And for a second button to be in the topBanner you could do something like this:
index.html:
<div>
{% include organisms/topBanner.html
tp-title=site.data.home.topbanner.title
tp-select-blurb=site.data.home.topbanner.select.blurb
b-getstarted-text=site.data.generic.buttons.getstarted
b-getstarted-link=site.data.generic.links.gosomewhere
b-survey-text=site.data.generic.buttons.completesurvey
b-survey-link=site.data.generic.links.surveypage
%}
</div>
and in the topBanner.html, you rebind the data to the dedicated button:
<div class="tb">
<h1>
{{ include.tp-title }}
</h1>
<div>
<h2>{{ include.tp-select-blurb }}</h2>
<div id="getstarted">
{% include atoms/button.html
button-text=include.b-getstarted-text
button-link=include.b-getstarted-link
%}
</div>
<div id="survey">
{% include atoms/button.html
button-text=include.b-survey-text
button-link=include.b-survey-link
%}
</div>
</div>
</div>
This approach means everything is data driven, there is no repetition/'copy/paste' of HTML, it all works through includes and you can apply atomic design pattern (http://patternlab.io/).
Wanna change the text of the button from 'GET STARTED' to 'LET'S START'? Go to the data/generic/buttons.json and change it there. The whole website now has the text changed.
The drawback is the fact that all the data has to trickle down from top level. Readability might be bad.
First use of Jekyll for me, and waned to have your opinion on this. What is good practice for static website dev like this? Is it easier to have a buttonGetStarted.html that includes a more generic button.html, and pass the data to button.html from buttonGetStarted.html? Like:
buttonGetStarted.html:
{% include atoms/button.html
button.text=site.data.generic.buttons.getstarted
button.text=site.data.generic.links.gosomewhere
%}
and then include buttonGetStarted every time I need it on the page? But then if I need a new button for the survey, I need to create another html buttonSurvey.html and so on... Sure on the code you see an {% include buttonSurvey.html %} which is easy to read and understandable straight away what this button is about. So this:
{% include button.html button.text=site.data.generic.buttons.getstarted %}
with only one file button for all the buttons, or
{% include buttonGetStarted.html %}
with creation of a new HTML file everytime I need a new button?
Thanks
F.
Disclaimer : As this question is primarily opinion-based (see SO help on this), I've voted to close it.
However, I can give my two cents. Quote are from Atomic Design Methodology.
Atom
[...] elements that can’t be broken down any further without ceasing to be functional
atom/buttons.html
<a class="button" href="{{ include.datas.button-link }}">
{{ include.dats.button-text }}
</a>
Molecule
[...] molecules are relatively simple groups of UI elements functioning together as a unit.
Here the question is : "do we need datas from organism / page for our molecule to work ?"
Yes : Datas will be passed by the parent organism. molecule/buttonGetStarded.html looks like (Note : this molecule is Homonuclear, but is functionnal.)
{% include button.html datas=include.buttonDatas %}
No : Datas will be set from inside the molecule (imaginary data structure)
{% include button.html datas=site.data.buttonDatas.getStarted %}
So in your case, I think that organism/topBanner.html can be composed like this (simplified for readability) :
{{ include.tp-title }}
<h2>{{ include.tp-select-blurb }}</h2>
<div id="getstarted"> {% include molecules/buttonGetStarted.html %}</div>
<div id="survey"> {% include molecules/buttonSurvey.html %}</div>
As I guess that your data files can be used for Internationalization (I18n) purpose. The molecule language doesn't need to be passed all the way down. It can be guessed by the molecule itself.
{% if page.language == nil %}
// if no language variable in page's front matter
// we default to site language set in _config.yml
{% assign language = site.language %}
{% else %}
// language variable present in front matter
{% assign language = page.language %}
{% endif %}
// get datas depending on guessed language
{% assign datas = site.data[language] %}
// this can even be more atomic with
{% assign datas = site.data[language]['buttonSurvey'] %}
// include the atom with correct language datas
{% include atom/button.html datas=datas %}
Note that this logic can even be factorized.

Get page variable from post variable in Jekyll?

I'm iterating over all the posts in my site like so
{% for post in site.posts %}
// code
{% endfor %}
I want to access some variable that I have stored at the page level. How can access it? I wasn't able to find anything after googling for awhile. I want to do something like
{% for post in site.posts %}
post.page.special_var
{% endfor %}
Jekyll support both post and page, so it is depend on you, which type of variable you want to access.
For example here is your post front matter.
---
layout:post
title: jekyll test
categories: jekyll
---
So in head.html, I am using this.
<title>{% if page.title %}{{ page.title }}{% endif %}</title>
I am using page to access that variable because there are too many pages like about or contact or privacy policy that does not belongs to jekyll post,so there you can't use post for example post.title to access that variable.
Now, look out these codes
{% for post in site.categories.jekyll reversed limit:10 %}
<span><a href="{{ post.url }}">{{ post.title}}<a/></span>
{% endfor %}
Here you note that, I am using loop, because I want to access same variable from multiple post, and that variable was jekyll .
Here I am using post.title but you can even use page.title, because it is globally accessible.
For fun :
I am using reversed, so you can order your post in which date you are created, older post will show at first.
I am using limit:10 because, I want to show only 10 post per page.
If you have a special_var variable defined in a post front matter you can get it like this :
{% for post in site.posts %}
<p>This is my var {{ post.special_var }}.</p>
{% endfor %}
See Jekyll documentation here.

Jekyll break for loop [duplicate]

This question already has an answer here:
Is there a "break" tag to escape a loop in Liquid?
(1 answer)
Closed 9 years ago.
I'm processing site.posts to compare post.categories against page.categories to create a related posts sidebar but if there are multiple common categories I get multiple links.
I want to break the inner loop but can't find anything to suggest this is possible.
Roughly (because on train and phone) the code I have is
{% for post in site.posts %}
{% for postcat in post.categories %}
{% for cat in page.categories %}
{% if cat == postcat %}
<p> {{ post.title }} </p>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
Not sure this is even doable
This is more of a Liquid Templating engine question, than a Jekyll one. It appears that Liquid has support for a {% break %} tag which is what you are looking for.
I would suggest making sure your Liquid gem is updated and then try using {% break %} in your code as suggested here.
Possible Duplicate