Thymeleaf 3 - multiple ternary evaluations possible? - thymeleaf

Does Thymeleaf 3 support multiple ternary conditions, such as:
cond1 ? do1 : cond2 ? do2 : do3
The following code works in Thymeleaf 3:
<th:block th:replace="${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ~{app/fragments :: ${toolbar}}">
Where I'm passing trueto get the default toolbar or a string to get a custom toolbar.
But this code, which should essentially remove the block if nothing is passed in the toolbar parameter generates a parsing exception:
<th:block th:replace="${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ${toolbar != null} ? ~{app/fragments :: ${toolbar}} : ~{common/fragments :: _$empty}">
Maybe I'm taking the wrong approach here?
The Thymeleaf parsing exception:
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "${toolbar == true} ? ~{common/fragments :: _$pageToolbar} : ${toolbar != null} ? ~{app/fragments :: ${toolbar}} : ~{common/fragments :: _$empty}" (template: "common/fragments" - line 70, col 15)
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:131) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:62) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:44) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.computeFragment(AbstractStandardFragmentInsertionTagProcessor.java:387) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardFragmentInsertionTagProcessor.doProcess(AbstractStandardFragmentInsertionTagProcessor.java:111) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.Model.process(Model.java:282) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1587) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleOpenElementEnd(TemplateHandlerAdapterMarkupHandler.java:304) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:278) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleOpenElementEnd(OutputExpressionInlinePreProcessorHandler.java:186) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleOpenElementEnd(InlinedOutputExpressionMarkupHandler.java:124) ~[thymeleaf-3.0.6.RELEASE.jar:3.0.6.RELEASE]
at org.attoparser.HtmlElement.handleOpenElementEnd(HtmlElement.java:109) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.HtmlMarkupHandler.handleOpenElementEnd(HtmlMarkupHandler.java:297) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupEventProcessorHandler.handleOpenElementEnd(MarkupEventProcessorHandler.java:402) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.ParsingElementMarkupUtil.parseOpenElement(ParsingElementMarkupUtil.java:159) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:710) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
... 79 common frames omitted

Does Thymeleaf 3 support multiple ternary conditions, such as:
cond1 ? do1 : cond2 ? do2 : do3
Yes, it does. Just enclose the second ternary operator with brackets to get rid of parse exception.
cond1 ? do1 : (cond2 ? do2 : do3)
Your expression should work after applying that.

Related

Dart - Functional Difference Between Conditional Operator vs. If Statement

Why does this work:
var myAnimal = (1 == 1)? 'Cat' : 'Dog'; //OK
myList.forEach((element) => (1 == element)? print('1') : print('Not 1')); //OK
But this doesn't?
var myAnimal = if(1 == 1) 'Cat'; else 'Dog';
//Expected an identifier, but got 'if'
myList.forEach((element) => if(1 == element) print('1'); else print('Not 1'));
//Expected an identifier, but got 'if'
I've been treating it like a given but I don't actually know why.
Because one is a statement (if) and other is an expression (ternary).
A statement "does" something, while an expression evaluates to a value.
The => in your function acts similarly to a return keyword.
While you can do something like: return print('statement'); . You can't return two of those statements: return: print('statement'); print('statement');

Dafny precondition to require a string is not only whitespace

I am trying to write a precondition to require a string to contain at least one non-whitespace character. I wrote the following:
predicate AllWhiteSpaceChars(s: string) {
forall i :: 0 <= i < |s| ==> s[i] in {' ', '\n', /*'\f',*/ '\r', '\t'/*, '\v'*/}
}
But I can't get my program to verify with it. The following fails:
method test1(s: string)
requires !AllWhiteSpaceChars(s)
{
print s;
}
method test2()
{
test1("./foo");
}
What is wrong with my predicate, and how can I create a working precondition?
Seems to be a trigger issue. The following works. But maybe someone more familiar with triggers can suggest a better fix.
predicate HasNonWhiteSpace(s: string) {
if s == []
then false
else s[0] !in {' ', '\n', /*'\f',*/ '\r', '\t'/*, '\v'*/} || HasNonWhiteSpace(s[1..])
}
method test1(s: string)
requires HasNonWhiteSpace(s)
{
print s;
}
method test2()
{
test1("./foo");
test1("\t\n ");
test1("d d");
}
BTW: not sure if you meant to require the string being printed to be non-empty. My current solution also requires that.

How to create inline if without else case

Does Dart support a syntax to have an inline if without an else case? I sometimes find myself in a situation to create a flutter layout where this might be really helpful
new Row(children: <Widget>[
new Text(item.name),
item.name2 != null ? new Text(item.name2) : new Container(),
]
In this example the empty container is unnecessary so I was hoping for something like this:
new Row(children: <Widget>[
new Text(item.name),
item.name2 != null ? new Text(item.name2),
]
there is no inline if without else
but in your case you're using if only to check only if it's null or not
dart have :
x = someVar ?? 0
here dart checks if someVar == null ? if true sets x value to 0, if false sets it to someVar value
but flutter will never let you add null to it's widget tree, so your can't use it in row/col
You can do this:
myVar == null ? 0 : myVar
If myVar is null then is 0, else then myVar
If you need to call a function you can use like this:
String s; //null
s == null ? print("if shorthand working") : {};
For variable assignment:
String c = "not null value";
String b; //null
var a = b ?? c;
Now a = c.
Visual Studio Code tip: add this to ignore linting for a line
// ignore: unnecessary_statements
In general, the "inline if" can be written in darts as follows:
<condition> ? <expression 1> : <expression 2>
So that if <condition> evaluates to True, then <expression 1> is returned and otherwise <expression 2>.
For example:
result = myVar == null ? 0 : myVar ;
// if myVar is null -> result = 0
// if myVar is not null -> result = myVar
There is actually plans to include a similar feature into the Dart language itself.
For a preview of how it would look like, take a look at this article by Remi
https://medium.com/flutter-community/quick-tip-sync-a-taste-of-the-future-9be4cd6993f4
And some accompanying Github issues
https://github.com/flutter/flutter/issues/17862
https://github.com/dart-lang/language/issues/47
https://github.com/dart-lang/language/issues/78

2620:Bad character error in stored procedure teradata

Concatenation is working in select statement:
SELECT 'HELLO' || 'WORLD';
is returning HELLOWORLD, but when I try to use it in stored procedure like below:
SET Time_of_Day=TRIM(Hour_of_Day) || ' : ' || TRIM(Minute_of_Hour) || ' : ' || TRIM(Second_of_Minute);
where Hour_of_Day,Minute_of_Hour,Second_of_Minute are variables, I also tried without TRIM:
ERROR:
**CALL FAILED 2620:PROCEDURE_NAME:THE FORMAT OR DATA CONTAINS A BAD CHARACTER**
Casting a string to a time in Teradata requires two-digit hour/minute/second:
SET Time_of_Day=TRIM(Hour_of_Day (FORMAT '99')) || ' : ' ||
TRIM(Minute_of_Hour (FORMAT '99')) || ' : ' ||
TRIM(Second_of_Minute (FORMAT '99'))
But this snippet is probably from the SP question you deleted an hour ago (a few seconds before I could post my answer).
There's no need for running 86400 single-row Inserts, simply create all data in a single Select, e.g.:
SELECT
Row_Number() Over (ORDER BY h,m,s),
Extract(SECOND From t) AS s,
Extract(MINUTE From t) AS m,
Extract(HOUR From t) AS h,
t
FROM
(
SELECT Cast(Begin(pd) AS TIME(0)) AS t
FROM sys_calendar.CALENDAR
WHERE calendar_date = Current_Date
EXPAND ON PERIOD(Cast(Current_Date AS TIMESTAMP(0)), Cast(Current_Date + 1 AS TIMESTAMP(0))) AS pd
) AS dt

Lua, Modify print function

I am writing a generic Log() function in lua which utilizes lua print function:
Log (variable, 'String: %s ', str, 'Word: %d', w)
Currently I'm using below approach:
print(string.format (variable, 'String: %s ', str, 'Word: %d', w))
I tried something like:
Log = function(...) begin
return print(string.format(...))
end
But it doesn't work, Is this correct approach? Or Is there any better more generic way to get this done?
If you just want to print a sequence of values, you can do that with print:
print(variable, 'String: %s ', str, 'Word: %d', w)
What you seem to want is something more complicated. Your algorithm seems to be:
For each argument:
If the argument is not a string, then convert it to a string and print it.
If the argument is a string, figure out how many % patterns it has (let us call this number k). Pass string.format the current argument string and the following k parameters, printing the resulting string. Advance k parameters.
That's a much more complicated algorithm than can be done in a one-line system.
Using Lua 5.3, here's what such a function would look like (note: barely tested code):
function Log(...)
local values = {}
local params = table.pack(...)
local curr_ix = 1
while (curr_ix <= params.n) do
local value = params[curr_ix]
if(type(value) == "string") then
--Count the number of `%` characters, *except* for
--sequential `%%`.
local num_formats = 0
for _ in value:gmatch("%%[^%%]") do
num_formats = num_formats + 1
end
value = string.format(table.unpack(params, curr_ix, num_formats + curr_ix))
curr_ix = curr_ix + num_formats
end
values[#values + 1] = value
curr_ix = curr_ix + 1
end
print(table.unpack(values))
end
I don't think your current approach works, because the first argument of string.format expects the format specifier, not the rest of the arguments.
Anyway, this is the way to combine formatting and printing together:
Log = function(...)
return print(string.format(...))
end
And call it like this:
Log("String: %s Number: %d", 'hello' , 42)
Also, it might be better to make the format specifier argument more explicit, and use io.write instead of print to get more control over printing:
function Log(fmt, ...)
return io.write(string.format(fmt, ...))
end

Resources