Write multiple statements in swig expression tag? - swig-template

i am tring to write multiple line in my swig expression tag but conditions are not evaluting,
but if i write all the statements in different tag then it is working.
Please find following example :
{% set promo_class = "whatone" %}
{% if (loop.index % 2) == 0 %}
{% set promo_class= promo_class +" mhxxl"; %}
{% endif %}
Following is not working for me, while above one is
{%
set promo_class = "whatone";
if ( loop.index % 2 ) == 0
set promo_class= promo_class +" mhxxl";
endif;
%}

Block statements are not part of the Swig Template language. Nowhere in the documentation does it show that as an example or an available syntax style.

Related

(Nearly.js) How can I import files which content I want to be the input of my grammars as in LaTex?

What I want:
First, assume I am able to successfully parse the following with my grammar.ne:
\begin{chapter}
...content
\end{chapter}
The desired behavior is to be able to make my grammar.ne file be able to parse a source file containing the above text. For example, in LaTex one can write something like this:
\chapter{filename}
The problem is simply that I don't really know how to do it. But I'll explain what I am trying.
What I am trying
% chapter grammar works well
chapter -> "\\begin{junior}" _ chapterContent _ "\\end{junior}" {% (data) => data[2] %}
% chapterTag below is able to read file and return a string.
% What I don't know how to do is making the grammar parse such a string as chapter
chapterTag -> "\\chapter{" anyCharacters "}"
{% (data) => {
// Assume function readfile exists.
const juniorText = readfile(data[1]);
// How do I tell Nearley to parse this string as a chapter?
return juniorText;
}
%}
If you've used LaTex before, you know what the content of the filename with the chapter would be (\begin{chapter} ... \end{chapter}).

Conditional logic on the same line in Adobe Document Generation is not recognizing template tags

I have a json field that is either absent or is set to true. But in my document I want it to show up as "Yes" or "No". I tried the following conditional expression ():
{% conditional-section expr(`my_field` = true) %}Yes{% end-section %}{% conditional-section expr(`my_field`!= true) %}No{% end-section %}
But when I call the Adobe Document Generation API, the PDF I get still has these template tags. For some reason it is not being detected. How can I achieve this?
I know I asked a question above in the comment, but I think I can take a stab at answering this now. So right now there is not a way to say "if a value is NOT present or present and equal to yes." There's a few ways you can handle this, but I think the easiest would be to change your data. To be clear, I don't mean change your database or anything, but keep in mind that before you call our API, you can massage the data a bit. So if the absence of my_field means yes, I'd do something like this (JavaScript):
if(!mydata.my_field) {
mydata.my_field = 'yes';
}
You could then do something like this:
{% conditional-section expr(my_field = "yes") %}
Yes
{% end-section %}
{% conditional-section expr(my_field = "no") %}
No
{% end-section %}
However, this will not be on the same line. As I said, this is a known issue. If you need it as such, again, I'd use the idea of massaging your data first. You could do something like this (again, JavaScript, but could be done in any language):
if(!mydata.my_field) {
mydata.my_field = 'yes';
}
if(mydata.my_field === 'yes') mydata.my_field_value = 'Yes';
else mydata.my_field_value = 'No';
All I did there was, based on the value of my_field, set another variable. In your Word template you can then simplify even more by just using {{my_field_value}}
With Document Generation being so flexible, you've got multiple different ways of solving a problem.
I have found this works for me:
{{my_field ? "Yes" : "No"}}

Probability Value for Integer Field oTree

for an experiment I want to program that a certain choice results in a probability distribution of an outcome. Currently I have:
Action = models.IntegerField(
choices=[
[1, 'altruistic (2:0.5,0:0.3,-2:0.2)'],
[2, 'selfish (2:0.3,0:0.4,-2:0.3)']
]
I tried:
Action = models.IntegerField(
choices=[
[random.choices([2,0,-2],[0.5,0.3,0.3]), 'altruistic (2:0.5,0:0.3,-2:0.2)'],
[random.choices([2,0,-2],[0.3,0.4,0.3]), 'selfish (2:0.3,0:0.4,-2:0.3)']
]
)
which gives me an Internal Server Error.
For the experiment I need the random number from the experiment, and the given choice. The given choice I should access with
player.get_field_display('Action')
However in the current state with :
{% extends "global/Page.html" %}{% load otree %}

{% block title %}
Treffen Sie ihre Entscheidung
{% endblock %}

{% block content %}
Bitte treffen Sie ihre Entscheidung. Spieler 1 hat entschieden {{player.get_field_display('Action')}} an Sie zu verteilen. Dies resultierte in {{ group.Action }}
{%formfields%}
{% next_button %}
{% endblock %}
this results in
TemplateSyntaxError: Unparsable argument '‘’Action’’'. Arguments must be valid Python literals. (line 29, in "player.get_field_display(<span")
Has anyone an idea for these flaws?

Using ampersand sign within client-side template and render it with thymeleaf

I use jsrender lib for client-side template generation but the whole website's output is processed by thymeleaf (spring mvc). The problem is that js template contains condtiion i.e.
<script id="main-menu-form-tmpl" type="text/x-jsrender">
{{if (index && (index == 0 || ...
but thymeleaf requires all the ampersands be escaped as &, but this causes jsrender to fail. Another solution found here, but jsrender fails as well because of unknown syntax.
Is there any way to output script's content without parsing it with thymeleaf engine?
JsRender lets you register templates from strings, as well as from script blocks.
See http://www.jsviews.com/#compiletmpl.
So instead of writing:
var mainTemplate = $.templates("#main-menu-form-tmpl");
and then calling mainTemplate.render(...) or mainTemplate.link(...) etc. you can instead remove your template script block and instead pass your template markup as a string to $.templates() as in:
var mainTemplate = $.templates("... {{if (index && (index == 0 || ...");
...
Or if you want you can keep the script block declaration, but with escaped ampersands and then get the contents of the script block as a string, unescape the ampersands, and pass that string to your template definition:
var mainTemplateString = $("#main-menu-form-tmpl").text().replace(/&/g, "&");
var mainTemplate = $.templates(mainTemplateString);
...
Alternatively you can wrap your template block in <![CDATA and again, strip the wrapper to get the real template markup string you want to pass to the template definition:
var mainTemplateString = $("#main-menu-form-tmpl").text().slice(16, -10);
var mainTemplate = $.templates(mainTemplateString);
...

Compile dust template with line breaks

Compiling a such template with dustc:
$ cat <<EOF | ./node_modules/.bin/dustc -
<p>Hi there!</p>
<p>I'm a {! dust !} template.</p>
EOF
outputs:
(function(){dust.register("-",body_0);function body_0(chk,ctx){return chk.write("<p>Hi there!</p><p>I'm a template.</p>");}return body_0;})();
but without \n between lines, eg: "<p>Hi there!</p>\n<p>I'm a template.</p>"
Is there any way to change this?
Thank you
You can use {~n} to create line breaks in your Dust templates. It's is especially useful within <pre> tags.
You can disable whitespace compression with
dust.optimizers.format = function(ctx, node) { return node };
Precompiling with gulp-dust, there's a preserveWhitespace option that does just that:
var compile=require('gulp-dust');
// ...
gulp.src('templates/**/*.dust')
.pipe(compile({ preserveWhitespace: true }))
// ...

Resources