Jekyll page rating calculator with meta from Front Mater area - ruby

I am editing include/span with the following code pretend to be as "Jekyll page rating calculator with meta from Front Mater area" but it seems like some syntax missing an extra heading/lines to let calculation to display in left bottom round quotes. Now there is no value
See the sampl page
<span class="indexmod">
{% assign number_of_words = content | strip_html | number_of_words %}
{% assign social = page.facebook | plus: page.instagram %}
{% comment %}Two lines for readability, but can be chained on one line{%
endcomment %}
{% assign Indexmod = social | times: page.age | divided_by: 1000000.0 %}
{% assign Indexmod = Indexmod | times: number_of_words | divided_by: 100
%}
{% endunless %}
</span>

There are two reasons why this doesn't work:
The {% endunless %} in this include is missing a matching unless clause. Either add one or remove the endunless line.
The value of Indexmod is not printed. You can do that by adding {{ Indexmod }} after your calculations are done.

Related

Liquid filter for not included in array?

I'm trying to list all pages that does not have the tag 'app'
This works for items with the tag app:
{% assign pages = site.pages | where:"tags",page.list_tag | where_exp:"page", "page.tags contains 'app'" | sort:"order_number" %}
but I can't use "unless":
{% assign pages = site.pages | where:"tags",page.list_tag | where_exp:"page", "unless page.tags contains 'app'" | sort:"order_number" %}
I get this error:
Liquid Exception: Liquid syntax error (line 2): Expected end_of_string but found id in /_layouts/list.html
I don't believe this is possible yet. In this case, I think you'll have to loop through the pages and include an unless condition inside your loop. So something like:
{% for page in site.pages %}
{% unless page.tags contains 'app' %}
{{ page.title }}
{% endunless %}
{% endfor %}

Transform Jekyll "reading time" into "page rating calculation"

I want to transform following Jekyll "reading time" calculator
<span class="reading-time">
{% capture words %}
{{ content | number_of_words | minus: 180 }}
{% endcapture %}
{% unless words contains “-” %}
{{ words | plus: 180 | divided_by: 180 |
append: “minutes to read” }}
{% endunless %}
</span>
into "page rating" with the following meta taken from page Front matter using following formula
{{ page.facebook }}
plus
{{ page.instagram }}
multiply
{{ page.age }}
divide
1000 000
multiply {{ content | number_of_words }}
divide
100 =
More details here
This can do the trick :
{% assign number_of_words = content | strip_html | number_of_words %}
{% assign social = page.facebook | plus: page.instagram %}
{% comment %}Two lines for readability, but can be chained on one line{% endcomment %}
{% assign Indexmod = social | times: page.age | divided_by: 1000000.0 %}
{% assign Indexmod = Indexmod | times: number_of_words | divided_by: 100 %}
Note :
the use of strip_html, this avoid counting html as words.
the use of 'divided_by: 1000000.0' to cast result to a Float

how do I concat page variables in jekyll loops

I'm accessing a page variable in a jekyll loop like below:
```ruby
{% assign kind = page.categories | first %}
{% for post in site.categories.[kind] | limit: 5 %}
{% unless post.url == page.url %}
<a href="{{ site.baseurl }}{{ post.url }}" class = 'post-url'>
<img src = '{{ site.baseurl }}/assets/{{ post.image }}.jpg>
<h2 itemprop="name headline">{{ page.title | escape }}</h2>
<time >{{ post.date | date: date_format }}</time>
</a>
{% endunless %}
{% endfor %}
```
Although the page builds successfully it outputs an error saying Expected page id but found open_square in "post in site.categories.[kind] | limit: 5"
How can I avoid this add still be able to use the page.categories | first variable?
Use site.categories[kind] instead of site.categories.[kind]
There shouldn't be any dot after "categories"

Liquid Warning: Liquid syntax error: Expected end_of_string but found id in

i'm using this line of code
<img data-animate="zoomIn" srcset="{{ 'device1.png' | asset_path | magick:resize:549x395 magick:quality:100 }} 1024w, {{ 'device1.png' | asset_path | magick:resize:280x201magick:quality:100 }} 640w" src="{{ 'device1.png' | asset_path | magick:resize:549x395 magick:quality:100 }}" alt="Mac" style="width: 100%; top: 0; left: 0;">
but i'm getting a liquid error like this
Liquid Warning: Liquid syntax error: Expected end_of_string but found
id in "{{ 'device1.png' | asset_path | magick:resize:549x395
magick:quality
Can you help me with the right syntax of this ?
Thanks in advance.
Carlos Vieira
I ran into the same issue. It seems a newer version of Liquid doesn't expect the pipe. I was able to fix it by removing the pipe altogether. Here was my issue:
Error: {% for post in site.posts | limit: 5 %}
Fixed: {% for post in site.posts limit: 5 %}
This page may help with proper liquid syntax http://jekyll.tips/jekyll-cheat-sheet/
I ran into a similar issue today with the following code:
{%- if title_case contains ' ' -%}
{%- assign all_strings = title_case | split: ' ' -%}
{%- assign the_string = '' -%}
{%- for str in all_strings -%}
{% assign new_string = str | capitalize %}
{% assign the_string = the_string | append: new_string | append: ' ' %}
{%- endfor -%}
{%- assign title_case = the_string | strip-%}
{%- endif -%}
{{ title _case }}
The problem was an extra space in the word 'title_case' - because it was a space followed by an underscore, Shopify interpreted it as an id!
According to this:
Sorting a for loop directly doesn’t generally work in my experience.
Workaround: use assign & apply sort to that array first, then use the sorted array in the for loop as below:
{% assign sortedPosts = site.posts | sort: 'last_modified_at' %}
{% for post in sortedPosts %}
...
{% endfor %}
The right answer is:
First use this plugin:
require "jekyll-assets"
class Jekyll::ImagePath < Jekyll::Assets::Liquid::Tag
def initialize(tag, args, tokens)
super("img", args, tokens)
end
private
def build_html(args, sprockets, asset, path = get_path(sprockets, asset))
path
end
end
Liquid::Template.register_tag('image_path', Jekyll::ImagePath)
then in image use
src="{% image_path 'customize-template-image.png' magick:resize: 549x375 magick:quality:100 %}"
This would fix for sure

how to pad a string in jekyll?

Say I'm incrementing an integer like this
{% capture page %}{{ page | plus: '1' }}{% endcapture %}
How could I pad it like this?
{% capture paddedPage %}{{ page | pad '0', 4 }}{% endcapture %}
where 4 is number of padding places, and '0' is the padding string? The end result would look like this: 0001 where the value of page is 1. How might I do this inline or in a filter?
I guess it could be represented like sprintf( '%d4', page ) but obviously this syntax does not work with liquid/jekyll.
I'm growing really disappointed in jekyll/liquid syntax (does not even have native modulus!) That aside, how might I pad a string with leading characters?
Use liquid filter prepend and slice, like this:
{{ page | prepend: '0000' | slice: -4, 4 }}
With Liquid you can do :
{% assign pad = 4 %}
{% assign padStr = '0' %}
{% assign numbers = "1|12|123|1234" | split: "|" %}
{% for number in numbers %}
{% assign strLength = number | size %}
{% assign padLength = pad | minus: strLength %}
{% if padLength > 0 %}
{% assign padded = number %}
{% for position in (1..padLength) %}
{% assign padded = padded | prepend: padStr %}
{% endfor %}
<p>{{ number}} padded to {{ padded }}</p>
{% else %}
<p>{{ number}} no padding needed</p>
{% endif %}
{% endfor %}
Note : Liquid modulus filter is {{ 12 | modulo: 5 }}

Resources