I have a List<String> bound by a Spring ModelAttribute. How do I iterate over it in Freemarker?
I've tried:
<#list ${modelattribute.listField} as thing >
<tr><td>${thing}</td></tr></#list>
</#list>
But Freemarker is not expecting the escape sequence
Encountered "{" at line 108, column 43 in things/search.html.
Was expecting one of: ...
How should I reference this value?
You should do something like this:
<#list modelattribute.listField as thing >
<tr><td>${thing}</td></tr></#list>
</#list>
Related
I’ve a handlebar expression which is giving me a dynamic string. I want to remove the white space between letters and use it as a div id. I know I can do this using JS. But is there a way to do this within handlebar template?
{{name}} is giving me “abc xyz” and I want “abcxyz” string.
What you can do is register a helper yourself and use it in your template to replace the white-space in the string with nothing.
Handlebars.js has a function registerHelper(String, Function) which (as you see) takes a string (the name of your helper) and a function that will return the result of your helper.
For example, if we want a helper function that replaces "Facebook" with "Google" in a given string you could do something like this.
Handlebars.registerHelper('replace', function(string) {
return string.replace('Facebook', 'Google');
});
In the template we would invoke it like {{replace "Hello, Facebook!"}} and it would return Hello, Google!.
In case of a multipurpose function (what it obviously should be in this case instead of the example I gave) you would pass the string to invoke the replacement on, the string to replace and what it should be replaced with.
Handlebars.registerHelper('replace', function(string, search, replace) {
return string.replace(search, replace);
});
In the same way as we did before we would invoke it in the template using {{replace "Hello, Facebook!" "Facebook" "Google"}}.
If you want to avoid writing your own helpers. You can use the following module : https://github.com/helpers/handlebars-helpers
simply install it
npm install --save handlebars-helpers
And you're good to go, you can use the following helper for your issue :
{{replace name " " ""}}
Here's another usage example :
{{replace "a b a b a b" "a" "z"}}
<!-- results in: 'z b z b z b' -->
I'm not finding a function to test for the existence of a value in the ISML template code. There's 'isDefined' but not 'isNull'.
isDefined returns true on null values:
<isset name="woot" value="" scope="request">
<isif condition="#isDefined(woot)#">
<h1>woot</h1>
</isif>
For now I'm using:
<isif condition="#woot EQ null#">
or
<isif condition="#woot EQ ''#">
I don't know if this will work for Boolean values.
isDefined is how you would check for a null value. In AbstractTemplate you have the method isDefined(Object anObject) that is called. Checkout the compiled jsp and java versions of your isml template.
In AbstractTemplate
public Boolean isDefined(Object anObject){
...
return anObject != null ? Boolean.TRUE : Boolean.FALSE;
}
The code in your example is a bit misleading, it doesn't actually test for a null reference. Bear with me.
First statement :
<isset name="woot" value="" scope="request">
Compiles to :
Object temp_obj = ("");
getPipelineDictionary().put("woot", temp_obj);
This just sets the woot variable to an empty string. If you add the following scriptlet to your isml you will see that it really isn't a null.
Disclaimer: don't use scriptlets in production code, this is only for demonstrating a point
<%
Object woot = getPipelineDictionary().get("woot");
out.print(woot == null); //print false
%>
Second line:
<isif condition="#isDefined(woot)#">
Evaluates if the variable exist and it does. It has an empty string as a value, not null like you might think.
So what happens here then?
<isif condition="#woot EQ null#">
Looking at the compiled version:
context.getFormattedValue(getObject("woot"),null).equals(context.getFormattedValue(getObject("null"),null))
The context.getFormattedValue(getObject("null"),null) is the important bit here. It tries to retreive the variable called null, it doesnt exist so returns null. The getFormattedValue method then returns an empty string for the null argument (see TemplateExecutionConfig::getFormattedValue). The whole statement then evals to true. Not because woot is null, but because you are comparing it to a variable that doesnt exist, so you are inadvertently evaluating two empty strings. This behaviour is consistent with the EQ operator because it is use to compare strings.
You would get the same result if u would use this statement too.
<isif condition="#woot EQ iDontExistButImAlsoNotNull#"> //true
The third statement compares the woot variable to an empty string value, which returns true.
<isif condition="#woot EQ ''#">
Compiled version:
context.getFormattedValue(getObject("woot"),null).equals(context.getFormattedValue("",null))
So the real problem is that woot doesn't have the literal value null. See the following code:
<isset name="foo" value="#IDontExitPrettySureAboutThat#" scope="request">
<%
Object foo = getPipelineDictionary().get("foo");
out.print("foo is null? ");
out.print(foo == null);
//prints : foo is null? true
%>
<isif condition="#isDefined(foo)#">
<h1>foo1</h1> //is never printed
</isif>
I'm abusing the fact that IDontExitPrettySureAboutThat doesn't exist to set a null value to foo. isDefined then starts to work like you would expect. That is until someone initializes my variable to something other than null.
I wouldn't advocate that you use this method, however. I think the best advice is not to use null to represent a missing value or invalid state.
This thread goes into some details on this topic.
test.xqy:
element test
{
attribute haha {"&"}
}
command line:
$ basex test.xqy
<test haha="&"/>
And I need the output to be: <test haha="&"/>. The XML entity is not processed by BaseX?
The document <test haha="&"/> is not well-formed, so there’s no chance to get this output with BaseX or any other XML processor. However, the value of haha is indeed '&'; you will see this if you return the attribute value as string:
string(<test haha="&"/>/#haha)
I have a very simple situation in PL/SQL application.
I create an array like so:
TYPE myArrayType IS TABLE OF myList%ROWTYPE;
myArray myArrayType;
myList is a cursor with 63 rows. Later in my code I try to loop trough my newly created array like so:
htp.p('<h2>My data table</h2>');
htp.p('<table>');
for elem in 1 .. myArray.count loop
htp.p('<tr>');
htp.p('<td>');
htp.p(' Data= '||myArray(elem)||' '); // <-- ERROR
htp.p('</td>');
htp.p('</tr>');
end loop;
htp.p('</table>');
But I get an error 'wrong number or types of arguments', can someone please help a newbie?
In the line:
htp.p(' Data= '||myArray(elem)||' '); // <-- ERROR
You are trying to concatenate string with the record, therefore you are getting an error. To fix your problem you have to provide the column name existing in this record:
htp.p(' Data= '||myArray(elem).column_name||' ');
I have a soap testStep in SOAPUI with an XQuery match.
The XML (simplified) look as follows:
<root>
<element>
<a>a</a>
<b>b</b>
<c>c</c>
<d>d</d>
</element>
</root>
I want to make an XQuery to get all child nodes from <element> removing a child element depending on his node name. My XQuery looks like:
for $x in //root/element/element()
return
if (name($x) != 'a') then $x
else ""
I expect the next result:
<b>b</b>
<c>c</c>
<d>d</d>
I think that my XQuery is correct, I tested with an XQuery online evaluator and looks ok, you can try with the follow link
However when I use this expression in a XQuery Match assertion in SOAPUI I get the following message: More than one match in current response. How can achieve this with SOAPUI?
Thanks,
Doing some tries finally I found the solution, the way to do this XQuery in SOAPUI is specifying a root node in the XQuery expression i.e:
<MyResult>
{
for $x in //root/element/element()
return
if (name($x) != 'a') then $x
else ""
}
</MyResult>