Mix #bind and EL expression on a zul - zk

Using ZK 6 I want to do something like this:
<window title="${c:l(#load(vm.name))}">
My goal is to get a label (localization) based on a key that is loaded from my ViewModel and not a static String and this is the problem.
The example given doesn't work because the syntax is invalid but I think you can understand my idea. How to do this in a clean way?

You should use EL expression (including calls of tag library's methods) inside parentheses of annotation and should not enclose it with curly braces:
<window title="#load(c:l(vm.name))">
See EL Expression in Data Binding for more details.

Related

Use Freemarker macro call within string parameter of another macro

I have a macro A that formats some text
<#macro A text>...${text}...</#macro>
and another macro that has a parameter accepting text
<#macro B x>Another ${x} text</#macro>
I'd like to call B with the x paramter to be some text formatted by A, s.th. like
<#B x="<#A text='abc'/>" /> returns Another <#A text='abc'/>
Is this possible somehow?
I tried the ?interpret as suggested here by ddekany -
<#B x="<#A text='abc'/>"?interpret /> but this fails with the error:
Expecting a string, date or number here, Expression .... is
instead a freemarker.core.Interpret$TemplateProcessorModel
It seems that a macro call in FreeMarker is something different than a function call in other languages.
Macro calls aren't expressions, and hence can't be used inside expression (like a parameter value). Macros are called for their side effects, which is typically printing to the output, and have no return value. Functions (see #function) are called for their return values, and so function calls are expressions. So maybe you need functions, not a macros in this case.
But if you absolutely have to use the output of a macro call in an expression (or of any arbitrary template fragment), then you have to capture the output via <#assign someVar>...</#assign> or <#local someVar>...</#local>. (Beware with #escape. If you re-print the captured output with ${...}, it will be escaped again, so you will need #noescape.)
I found a workaround using assign:
<#assign a><#A text="abc"/></#assign>
<#B text=a/>
Anyway, it would be interesting to know if this is possible somehow.

How to display texts that contain curly braces

Is it possible to display a JSON string using e.g. sap.m.Text?
I'm pretty sure it's treating the text as a binding syntax due to the structure of the string "{...}".
I thought there may be a parameter to disable binding allowing any kind of raw text, but looking at the API, I see no such thing.
Here's an example of the issue: http://jsbin.com/zarijedaya/1/edit?html,js,output
You can make use of the setText method which will consider the JSON as a string.
new sap.m.Text().setText(json);
http://jsbin.com/bonoxavilo/1/edit
Texts with curly braces can be escaped via sap.ui.base.ManagedObject.escapeSettingsValue:
new Text({
// ManagedObject required from "sap/ui/base/ManagedObject"
text: ManagedObject.escapeSettingsValue(myJSONText)
});
From API referece:
escapeSettingsValue
Escapes the given value so it can be used in the constructor's settings object. Should be used when property values are initialized with static string values which could contain binding characters (curly braces)
Here is a demo: https://jsbin.com/bocuzaz/edit?js,output
Prerequisite
In order to make escapeSettingsValue work, bootstrap setting compatVersion needs to be set to "edge".

Prevent PlayFramework's template engine from escaping my Strings

I've created some utilities that help me at generating HTML and I reference them in my views as #div( "class" -> "well" ){ Hello Well. }. Until now those classes were subclassing NodeSeq because they aren't escaped then. But I need to get rid off the NodeSeq in the top of my class hierarchy because Scala's xml is flawed and makes my code hacky and because I could switch to Traits then.
So I tried to find out how to prevent Play from escaping my Tag-objects. But unfortunately the only valid solution that I found is to override the template compiler and have the user specify my compiler in his Build.scala settings.
But I hopefully have overlooked a way more simple approach?
If your html helpers returns 'Html' rather than String you don't need to wrap them using the #Html tag in the view.
eg
import play.api.templates.Html
def a(src: String, value: String) : Html = Html(s"<a href='$src'>$value</a>")
Would be called in the view as below without needing to wrap in #Html
#a("www.example.com", "Example")
Since version 2.2.0-M1 there appeared a new approach in the docs that explains how to add custom formats to the template engine. This allows me to easily integrate my utilities.
Custom Template Format: Java, Scala

Using gwtquery, how can we select elements with attributes having values containing more than one word?

Say I want to select an element such as
<div class="my class">InnerHTML</div>
While this may be done easily using CSS selectors with [class="my class"], gwtquery has a difference in the sense that it doesn't take the quotation marks in the input. It only accepts values like [attribute=attributevalue].
When I applied the same selector in GQuery with the space in between, it returned no matches. I have a feeling that this might be because of some incorrect parsing in the library for such cases. Is this so?
If so, is there any other way I might select these elements using GQuery?
It works for me (at least with gwtquery-1.1.0 and gwt 2.4.0)
GQuery myClassDivs = $("div[class=\"my class\"]");
returns a match for <div class="my class"/>.
However, with the class attribute, it's generally better to use the ~= selector instead, because <div class="my class"/> and <div class="class my"/> should usually be treated as equivalent.
So I would suggest using
GQuery myClassDivs = $("div[class~=my][class~=class]");
This will also select something like <div class="my class special"/>, which is usually desirable.
Generally, to select attributes with space-separated words, you'll use the [att~=val] selector as mentioned by Chris Lercher.
Or, you know, since you're selecting by the class attribute anyway, you could just use class selectors instead:
GQuery myClassDivs = $("div.my.class");
Regarding this:
When I applied the same selector in GQuery with the space in between, it returned no matches. I have a feeling that this might be because of some incorrect parsing in the library for such cases. Is this so?
If you're referring to adding the space in your unquoted attribute value, it's not a parsing error in the library. The selector itself is invalid because spaces in unquoted attribute values are explicitly invalid.
If quoted attribute values aren't recognized, then that is a parsing error in the library.

In Scala is there any way to get a parameter's method name and class?

At my work we use a typical heavy enterprise stack of Hibernate, Spring, and JSF to handle our application, but after learning Scala I've wanted to try to replicate much of our functionality within a more minimal Scala stack (Squeryl, Scalatra, Scalate) to see if I can decrease code and improve performance (an Achilles heal for us right now).
Often my way of doing things is influenced by our previous stack, so I'm open to advice on a way of doing things that are closer to Scala paradigms. However, I've chosen some of what I do based on previous paradigms we have in the Java code base so that other team members will hopefully be more receptive to the work I'm doing. But here is my question:
We have a domain class like so:
class Person(var firstName: String, var lastName: String)
Within a jade template I make a call like:
.section
- view(fields)
The backing class has a list of fields like so:
class PersonBean(val person: Person) {
val fields: Fields = Fields(person,
List(
Text(person.firstName),
Text(person.lastName)
))
}
Fields has a base object (person) and a list of Field objects. Its template prints all its fields templates. Text extends Field and its Jade template is supposed to print:
<label for="person:firstName">#{label}</label>: <input type="text" id="person:firstName" value="#{value}" />
Now the #{value} is simply a call to person.firstName. However, to find out the label I reference a ResourceBundle and need to produce a string key. I was thinking of using a naming convention like:
person.firstName.field=First Name
So the problem then becomes, how can I within the Text class (or parent Field class) discover what the parameter being passed in is? Is there a way I can pass in person.firstName and find that it is calling firstName on class Person? And finally, am I going about this completely wrong?
If you want to take a walk on the wild side, there's a (hidden) API in Scala that allows you to grab the syntax tree for a thunk of code - at runtime.
This incantation goes something like:
scala.reflect.Code.lift(f).tree
This should contain all the information you need, and then some, but you'll have your work cut out interpreting the output.
You can also read a bit more on the subject here: Can I get AST from live scala code?
Be warned though... It's rightly classified as experimental, do this at your own risk!
You can never do this anywhere from within Java, so I'm not wholly clear as to how you are just following the idiom you are used to. The obvious reason that this is not possible is that Java is pass-by-value. So in:
public void foo(String s) { ... }
There is no sense that the parameter s is anything other than what it is. It is not person.firstName just because you called foo like:
foo(person.firstName);
Because person.firstName and s are completely separate references!
What you could do is replacing the fields (e.g. firstname) with actual objects, which have a name attribute.
I did something similiar in a recent blog post:http://blog.schauderhaft.de/2011/05/01/binding-scala-objects-to-swing-components/
The property doesn't have a name property (yet), but it is a full object but is still just as easy to use as a field.
I would not be very surprised if the following is complete nonsense:
Make the parameter type of type A that gets passed in not A but Context[A]
create an implicit that turns any A into a Context[A] and while doing so captures the value of the parameter in a call-by-name parameter
then use reflection to inspect the call-by-name parameter that gets passed in
For this to work, you'd need very specific knowledge of how stuff gets turned into call-by-name functions; and how to extract the information you want (if it's present at all).