Increment value in locomotivecms liquid markup - ruby-on-rails

I wouldn't have come here if I hadn't tried many different approaches...
Obviously collections of objects do have a field size/length, but the components don't have an index. I want something like this:
{% for product in contents.products %}
<h3>Produkt {{ product.index + 1 }}</h3>
<p>{{ product.price | concat: ' €' }}</p>
{% endfor %}
I have tried the following as documented here:
http://www.omniref.com/ruby/gems/locomotivecms-liquid/classes/Liquid::Increment
{% increment variable %}
Doesn't work. I have to work in the backend editor which complains about bad syntax. Unknown tag increment. Could I be working with an old version? Unfortunately I can't check it.
I also tried assigning a value to 0 before the for loop:
{% assign x = 0 %}
And then manually increment it by 1:
{% assign x = x + 1 %}
There must be way! I mean this is basic programming. Has anybody found a way around this?
Thanks!

You can do increment in Locomotive CMS this way:
{% assign counter = 0 %}
{% capture counter %}{{ counter | plus: 1 }}{% endcapture %}

Related

How to use Latex in dev.to editor?

I wonder is there some way I could render math formulas in dev.to editor to show as mathematical equations in my articles?
It seems that Dev.to supports Katex for mathematical formulas.
From dev.to documents:
KaTeX Embed
Place your mathematical expression within a KaTeX liquid block, as follows:
{% katex %} c = \pm\sqrt{a^2 + b^2} {% endkatex %}
To render KaTeX inline add the "inline" option:
{% katex inline %} c = \pm\sqrt{a^2 + b^2} {% endkatex %}
Info taken also for dev.to github issues.

Thymeleaf check if variable equals a number

I have the following Thymeleaf code, I want "--" to be printed if taxHistory.landValue equals to zero, and the actual value to be printed if taxHistory.landValue is greater than 0
<td th:if"${taxHistory.landValue==0}" th:text="--"></td>
<td th:unless="${taxHistory.landValue>0}" th:text="|${taxHistory.landValue}|"></td>
However I'm getting the following error
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "0}"
What is the correct way of doing this in Thymeleaf?
There are multiple ways to do this, but you can use a ternary operator and print your string if your desired condition does not match:
<td th:text="${taxHistory.landValue > 0 ? taxHistory.landValue : '--' }">[taxHistory.landValue]</td>
You can use the > notation to keep the HTML formed properly. This assumes that a negative value would also print your string (you didn't mention behavior in that unusual case).

Jinja2 if value less than zero

I am using Python and Jinja2 to generate a LaTeX table. The following code produces the bottom row of the table:
<snip>
\hline
\BLOCK{for col1, col2, col3, col4, col5, col6 in shareCompSum}
\VAR{col1} & \VAR{col2} & \VAR{col3}\euro & \VAR{col4}\euro & \VAR{col5}\euro & \VAR{col6}\euro \\
\BLOCK{endfor}
\hline
<snip>
The corresponding LaTeX-output is as follows:
\hline
Profit/Loss & & -174.040\euro & -9.040\euro & -723.20\euro & -733.28\euro \\
\hline
For columns 3 to 6, I would like the text color to be changed to red if the value of variable col3 is negative, and normal black if positive.
I am trying to write a statement along the following lines:
{% if col3 < 0 %} \textcolor{red}{\VAR{col3}\euro}
{% else %} \VAR{col3}\euro {% endif %}
It seems that Jinja does evaluate the if condition, but as a string-test rather than on numbers.
How can I perform the evaluation of float/numbers in Jinja?
The variable col3 appears as a string. You can set it to float via the following syntax:
{% if col3|float < 0.0 %} \textcolor{red}{\VAR{col3}\euro}
{% else %} \VAR{col3}\euro {% endif %}
The above Jinja code will typeset the variable col3 in red if negative, and black when larger than or equal to zero

Use Regexp to replace optional match between charaters

I'm trying to replace the html entity for a blank space with an actual space between occurrences of {{ and }}
Example example
"this is a gap {{ for user in users }}" =>
"this is a gap {{ for user in users }}"
I've found answers similar which had led me to write something like this (which doesn't work)
.gsub(/(?<=\{\{).*( ?).*(?=\}\})/,' ')
Any help with such a regex would be appreciated thanks!
You can do this with a complex regular expression I agree, but I find it simpler to use nested substitution. First use gsub to find the bracketed substrings and then use another to replace the entity.
string = 'this is a gap {{ for user in users }}'
result = string.gsub(/{{.*?}}/){ |s| s.gsub(/ /, ' ') }
# => "this is a gap {{ for user in users }}"
Single call to gsub using \K and \G
It's a bit tricky, but in Ruby 2.0+, we can do it with a compact regex thanks to the \K and \G tokens. Use this regex and replace with a space:
[^{}]*\K(?:{{|\G).*?\K (?=[^{]*})
See demo.
In Ruby:
result = subject.gsub(/[^{}]*\K(?:{{|\G).*?\K (?=[^{]*})/, ' ')
Explanation
[^{}]* matches any chars that are not braces
\K tells the engine to drop what was just matched
(?:{{|\G) either matches the opening curlies or asserts that we are positioned after the last match
.*?\K lazily matches chars (and drops them), up to...
Our match!
which, as the lookakead (?=[^{]*}) asserts, must be followed by any chars that are not an opening brace before meeting a closing brace

Jinja multiple rendering streams

Is there any way within a jinja template to render simultaneously to multiple streams?
Lets say I want to render a (printable) quiz, with first all the questions, then all the answers at the end. Each type of question (multiple choice, matching, missing word) is a different template.
Obviously I can take two passes and have question and answer in separate templates. But I would like to do this in one pass, so as to keep the templates well organised, and also to make the python calling code more regular, without a special case to handle this situation).
What I would like to have something like multiple 'streams', similar to blocks, but which which accumulate the output of multiple templates. Obviously the below is fantasy but is there another way within jinja to do this?
{% streams x, y %} {# define twp streams x and y #}
{% stream x %}
aaaa
{% endstream %}
{% stream y %}
bbbb
{% endstream %}
{% stream x %}
cccc
{% endstream %}
{% stream y %}
dddd
{% endstream %}
{{ x }} {# renders aaaacccc #}
{{ y }} {# renders bbbbdddd #}
Rendering to multiple streams is not possible. A simple solution would be to call the template twice, with a question boolean argument, and use if expressions to switch between question and answer code:
{% if questions %}
aaaa
{% else %}
bbbb
{% endif %}
{% if questions %}
cccc
{% else %}
dddd
{% endif %}
Then you call the template:
questions_html = template.render(questions=True)
answers_html = template.render(questions=False)

Resources