I have product name, which contains only one number.
Product.name="Pack of apples (12 pcs).";
Product.price=6;
What I'm trying to do - is to put 12 to variable, to do a little math later (ex: show how many costs 1 apple).
I was tried to do following:
{{ set apple = attribute(product, 'title'~lang)|replace('/[^0-9]/', '') }}
but with no luck.
I only have access to modifying .twig files, so any workaround would be appreciated
I would not reccommend using this way, however in this circumstance, the required data could be retrieved using simple split commands.
{% set bar = "Pack of apples (12 pcs)." %}
{% set foo = bar|split('(')[1]|split(' p')[0] %}
{{ foo }}
This would lead to foo containing 12.
However again, this would not be the best way to approach this and the use of an amount variable would be more approproate.
Related
i want to know what this symbol | mean and when we use it
{{ entity.date|date('d-m-Y')}
Could someone explain it for me?
This is called a filter. It applies a filter to a variable or expression to the left. For example
{{ name|striptags }}
will apply a striptags filter to a name variable.
In your case a date formatting filter is applied, to make the date look according to a certain format.
The full list of builtin filters can be found here.
Filters can be chained, for example
{{ name|striptags|title }}
will apply a striptags filter to a name variable and then apply a title filter to the resulting value.
I created my website using Jekyll, using Beatiful-Jekyll theme to be precise.
For the syntax highlighting I used Rouge. When I don't show line numbers everything work great. When I show line numbers, sometimes the line numbers do not start from the first line of code (same at the end, they stop some lines before the end). And sometimes they are aligned with the code, sometimes not.
Here is an example where everything works fine:
works_fine
Here is an example where line numbers are aligned with line codes but first and last line numbers are missing (in another example, the first three and last three are missing).
lines_missing
And the last example, is where line numbers are not aligned with line codes:
lines_not_aligned
I believe that the problem comes from linenos. To show line numbers I use
{% highlight <language> linenos %}
<code>
{% endhighlight %}
Because I tried the following configuration in the _config.yml file:
kramdown:
input: GFM
syntax_highlighter: rouge
syntax_highlighter_opts:
css_class: 'highlight'
span:
line_numbers: false
block:
line_numbers: true
start_line: 1
And in this case, line numbers shown by default are shown correctly, but line numbers shown using {% highlight linenos %} are still bad.
default_line_numbering_without_linenos
Thanks in advance
I know this is an old post, but I had difficulties finding an answer so I'm posting my findings here.
TL;DR set minify_html to false and minify html pages with liquid below. This worked in Jekyll version 4.0.0
Edit 1.
Further investigation revealed that trailing space inside code brackets also causes similar behaviour.
text
text
the last empty line breaks the pre tags while minifying the html. This happens when the output html is captured and new line characters transformed to br and the whole html string is splitted using the br tag, hence the trailing empty line gets lost. (would probably work with a whitespace instead of just an empty line but I didn't try)
End edit 1.
I was having this exact same issue. My local "jekyll serve" server was fine but Gitlab pages showed it just like the examples in the question. This lead me to realize that I usually set "minify_html" to false while running the site locally and as it turns out the local version also broke when I set minify_html to true. I removed jekyll default minify_html completely and used the code below.
Create a new layout, doesn't matter what you call it.
paste the following code into it.
{%- capture to-compress -%}
{{ content }}
{%- endcapture -%}
{%- assign inside-code-block = false -%}
{%- capture to-remove-br -%}
{%- assign lines = to-compress | newline_to_br | split: '<br />' -%}
{%- for line in lines -%}
{%- if line contains "<code>" -%}
{%- assign inside-code-block = true -%}
{%- elsif line contains "</code>" -%}
{%- assign inside-code-block = false -%}
{%- endif -%}
{%- if inside-code-block == true -%}
{{ line }}
{%- else -%}
{{ line | lstrip }}
{%- endif -%}
{%- endfor -%}
{%- endcapture -%}
{{ to-remove-br }}
Go to your existing layouts and add the layout you just created to their front matter as follows:
layout: <name of the layout you made>
the code is my personal experiment to minify html and it seems to work. The code ignores everything inside <code> tags, so the ``` markdown syntax should work just fine.
I got the idea while trying to get https://jch.penibelst.de/ layout to work (which I wasn't able to do).
Liquid syntax help:
- https://shopify.github.io/liquid/basics/whitespace/
- https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
I just ran into this with these options:
kramdown:
syntax_highlighter_opts:
block:
line_numbers: true
I was using a code block that had an empty line at the beginning. The empty line was the issue. Because I wanted the empty line (for a post about code formatting issues) I found that simply putting a space on the line would fix the alignment issue.
There are two Airflow macros available currently: ds_add and ds_format.
I would like to know the syntax for using them both.
For example, I currently can use one of them like this to add 7 days to a date:
dt7 = '{{ macros.ds_add(ds, 7) }}'
However, I actually need to do something like this and get back a YYYYMMDD format without using datetime or any other python package, since I need to feed this into an Operator:
dt7_fixed = '{{ macros.ds_add(ds_nodash, 7) }}'
But ds_add does not support 'YYYYMMDD' format, only 'YYYY-MM-DD'.
A workaround is to use ds_format in that one-liner too, but I can't grok the right syntax.
I believe this will do what you want:
{{ macros.ds_format(macros.ds_add(ds, 7), '%Y-%m-%d', '%Y%m%d') }}
You should think of these macros the same way you would think of functions.
As per the link you included above ds_format takes in 3 params, date string and two strings representing desired input and output format.
The output of the originally used ds_add macro is a string which you can use as the first argument here.
Note in the source that this uses datetime under the hood.
Is it possible in Twig (used with Symfony2) to round number max to 8 decimal places OR less?
E.g.
number is 10.0001 so I would like to display it as 10.0001 (not 10.00010000)
number is 10.0000000000001 - display 10.0 (or 10)
number is 10.00100000001 -> display 10.001
Function number_format allows only to set static number of decimal places (e.g. always 8).
You might be able to get the result using number_format and trim like
{{ your_number | number_format(8) | trim (0) }}
That being said a custom twig extension like #bosam has recommended would probably be the easiest to use/remember in the future.
Best way for you to do this is a Custom Twig Extension implementing your logic.
That way you could use {{ myNumber | round_number }} and have the right rounded number to display.
My goal is to mask one digit from a 4-digit string. Instead of having 2451, I'd like to have 24*1.
I tried {{ my_var|replace(slice(2, 1): '*') }} but this raises the following error: The function "slice" does not exist in My:Bundle:file.html.twig.
The weirdest thing being that {{ my_var|slice(2, 1) }} works perfectly. So the function does exists.
How can I do what I want to achieve?
Many thanks.
Create Your own Twig extension - filter:
SymfonyCookbook
IMHO it would be cleanest way to do this.
slice is a filter not a function, you can try to pipe them but in your case i do not see something achievable without creating your custom twig function or filter to mask your needs: