How to compare string size and length in Jekyll Liquid templates?

I use Jekyll on GitHub pages to create a blog and want to get the length of the page.title string passed to the Liquid template in the YAML control in each post. I could not find an easy way to do this. Looking at the Guide for Liquid For Designers , I could see that it supports two types of markup:

  • Markup output . Separated by double curly braces {{ }} , you can display the variables that are passed to your template, either in the front of the YAML, for example page.title in Jekyll, or global site-level variables in _config.yml . To display the title of a message or page, you must use {{ page.title }} .

  • Tagging Indicates braces and percentages {% %} , they are used for logic in your templates. If statements, loops, this type of thing.

There are apparently a lot of filters that you can use with Output Markup, and you can output the length of the string passed to the template using {{ page.title | size }} {{ page.title | size }} .

However, what I would like to do in my template displays the page title using the header <h1> , <h2> or <h3> depending on the length of the header.

I still can't figure out tagging and output markup.

I can display the size of page.title on a page with {{ page.title | size }} {{ page.title | size }} , but I cannot, however, figure out how to use length in an if statement. It also returns a string representation, not a number.

Does anyone with more experience with Liquid know how to do this?

Ideally, what I would like to do is something like this:

 {% if page.title | size > 5 %} 
+6
source share
5 answers

I am going to post this solution that I found on someone's blog. This is the only way that I have found so far, so reliably get the length of the passed in string and compare using anything other than direct equality. To make a comparison, you must do a subtraction and use the difference. This method is described in this blog post written by Ben Dunlap . This is still a kind of workaround, but it is smart and it seems that it will always work. It may not be so effective if you want to do if, elsif, else with several sentences, but you can still use several differences and make it work. Basically you would do this in my case:

 {% capture difference %}{{ page.title | size | minus:20 }}{% endcapture %} {% unless difference contains '-' %} // 20 characters or less <h3>{{ page.title }}</h3> // show smaller header {% else %} // More than 20 characters <h2>{{ page.title }}</h2> // show larger header {% endunless %} 

Kind of smart, but also a kind of hack. If anyone comes up with a better way, let me know.

+10
source

I studied this in order to make links in the footers using fluid syntax, and it was simple.

 {% assign thesize = variable.size %} {% if thesize > 5 %} Do stuff here. {% endif %} 

Works for what I do, at least I just thought that I would throw it there. I had problems using capture because it was automatically stored as a string.

Yet again,

 {% if variable.size > 5 %} Do stuff here. {% endif %} 

should work the same way.

+16
source

You want to capture the value in the variable first:

 {% capture title_size %}{{ page.title | size }}{% endcapture %} {% if title_size > 5 %} // do your stuff here... {% endif %} 

capture described in detail at the very end of this page: https://github.com/shopify/liquid/wiki/liquid-for-designers

+6
source

As a side note, a compass of length is also used:

 {% if site.posts.length > 0 %} blablabla... {% endif %} 
+3
source

Although there is no obvious way to drop a string to an integer in the Liquid pattern, you can do it the other way around (i.e. pour an integer into a string) using the {% capture %} tag, which only outputs the integer variable. This is due to the fact that the capture will always return the sketch.

With that in mind, you can actually trick Jekyll into comparing between integer and string, at least if you want to do a simple equality test.

This does not solve the problem of this question, but is useful enough for those looking here, so I will post it anyway. In this example, we want to compare the length of the page title with the size of the array. Do not ask why :)

 {% capture title_size %}{{ page.title | size }}{% endcapture %} {% if title_size == some_array.size %} This will never be reached, because some_array.size is an integer and title_size a string {% endif %} {% capture some_array_size_str %}{{ some_array.size }}{% endcapture %} {% if title_size == some_array_size_str %} But this will! {% endif %} 
+2
source

Source: https://habr.com/ru/post/945887/


All Articles