You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Matthias <th...@gmail.com> on 2013/11/17 15:30:40 UTC

conditional expression

Hi, if theres something in tapestry for inline conditional expressions? 
I think of something like

<a href="www.google.com" class="${isActive ? 'active' : 
'notActive'}">google</a>

I found nothing like that, but i loved to have it.

Thanks in advance
Matthias

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: conditional expression

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Sun, 17 Nov 2013 15:30:05 -0200, Matthias <th...@gmail.com>  
wrote:

> @Howard
> Thanks for your reply. I know that it was possible with Java code, but i  
> tried to avoid to write extra methods for such simple tasks.

Please don't ask us how to make your code and templates be worse and  
harder to test. This is 2013, not the beggining of the 90s. ;)

-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: conditional expression

Posted by Howard Lewis Ship <hl...@gmail.com>.
I get nervous about creating new binding factories that then try to parse
the (rather complex) property binding syntax, that's all. We've kind of
used up all the punctuation and delimiters.


On Sun, Nov 17, 2013 at 9:30 AM, Matthias <th...@gmail.com> wrote:

> @Howard
> Thanks for your reply. I know that it was possible with Java code, but i
> tried to avoid to write extra methods for such simple tasks.
>
> @Michael
> WOW! Thats really nice. Exactly what I was looking for. I improved it a
> bit to make the false value optional.
>
>     // constructor without falseBinding
>     public CondBinding(Binding conditionBinding, Binding trueBinding,
> TypeCoercer resolver) {
>         this(conditionBinding, trueBinding, null, resolver);
>     }
>
>     // just check if valueFalse is set
>
>     @Override
>     public Object get() {
>         String valueTrue = resolver.coerce(trueBinding.get(),
> String.class);
>         String valueFalse = falseBinding != null ?
> resolver.coerce(falseBinding.get(), String.class) : null;
>
>         Boolean condition = resolver.coerce(conditionBinding.get(),
> Boolean.class);
>         return condition != null && condition ? valueTrue : valueFalse;
>     }
>
>
>     // in BindingFactory
>
>     @Override
>     public Binding newBinding(String description, ComponentResources
> container, ComponentResources component, String expression,
>             Location location) {
>         String[] parts = expression.split(DELIMETER, 3);
>
>         // well i'm not sure what happens here if parts < 2
>
>         if (parts.length < 2) {
>             return bindingSource.newBinding(description, container,
> component, "prop", expression, location);
>         }
>
>         String condition = parts[0];
>         String valueTrue = parts[1];
>         Binding conditionBinding = makeBinding(condition,
> DEFAULT_CONDITION_PREFIX, container, description);
>         Binding trueBinding = makeBinding(valueTrue, DEFAULT_VALUE_PREFIX,
> container, description);
>
>         if (parts.length == 3) {
>
>             String valueFalse = parts[2];
>             Binding falseBinding = makeBinding(valueFalse,
> DEFAULT_VALUE_PREFIX, container, description);
>             return new CondBinding(conditionBinding, trueBinding,
> falseBinding, resolver);
>         }
>         return new CondBinding(conditionBinding, trueBinding, resolver);
>     }
>
> // Usage
> class="${cond:selected, selected}"
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

Re: conditional expression

Posted by Matthias <th...@gmail.com>.
Hi Boris, in my opinion it makes the tml less complicated and much more 
readable. Cause you don't have to look into java for just setting a 
css-class to an element.

In my case I tried to add a simple navigation with 3 elements and want 
to highlight the currently selected:

<ul>
       <li><t:eventlink t:event="jump" t:context="literal:0" 
class="${cond:first, selected}">First</t:eventlink></li>
       <li><t:eventlink t:event="jump" t:context="literal:1" 
class="${cond:second, selected}">Second</t:eventlink></li>
       <li><t:eventlink t:event="jump" t:context="literal:2" 
class="${cond:third, selected}">Third</t:eventlink></li>
</ul>

In my Java-Page-Class I already had those three methods (isFirst...), so 
there was no extra programming.

in grails: 
http://stackoverflow.com/questions/7437774/inline-if-statement-in-grails
in jsp: http://hscripts.com/tutorials/jsp/operators/conditionalopr.php




On 17.11.2013 18:32, Boris Horvat wrote:
> But why would you want that? I can see the benefit but I am afraid that
> that will just make your tml more complicated where in my mind logic like
> that belongs in .java
>
> just my 2 cents
>
> cheers


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: conditional expression

Posted by Boris Horvat <ho...@gmail.com>.
But why would you want that? I can see the benefit but I am afraid that
that will just make your tml more complicated where in my mind logic like
that belongs in .java

just my 2 cents

cheers


On Sun, Nov 17, 2013 at 6:30 PM, Matthias <th...@gmail.com> wrote:

> @Howard
> Thanks for your reply. I know that it was possible with Java code, but i
> tried to avoid to write extra methods for such simple tasks.
>
> @Michael
> WOW! Thats really nice. Exactly what I was looking for. I improved it a
> bit to make the false value optional.
>
>     // constructor without falseBinding
>     public CondBinding(Binding conditionBinding, Binding trueBinding,
> TypeCoercer resolver) {
>         this(conditionBinding, trueBinding, null, resolver);
>     }
>
>     // just check if valueFalse is set
>     @Override
>     public Object get() {
>         String valueTrue = resolver.coerce(trueBinding.get(),
> String.class);
>         String valueFalse = falseBinding != null ?
> resolver.coerce(falseBinding.get(), String.class) : null;
>         Boolean condition = resolver.coerce(conditionBinding.get(),
> Boolean.class);
>         return condition != null && condition ? valueTrue : valueFalse;
>     }
>
>
>     // in BindingFactory
>     @Override
>     public Binding newBinding(String description, ComponentResources
> container, ComponentResources component, String expression,
>             Location location) {
>         String[] parts = expression.split(DELIMETER, 3);
>
>         // well i'm not sure what happens here if parts < 2
>         if (parts.length < 2) {
>             return bindingSource.newBinding(description, container,
> component, "prop", expression, location);
>         }
>
>         String condition = parts[0];
>         String valueTrue = parts[1];
>         Binding conditionBinding = makeBinding(condition,
> DEFAULT_CONDITION_PREFIX, container, description);
>         Binding trueBinding = makeBinding(valueTrue, DEFAULT_VALUE_PREFIX,
> container, description);
>
>         if (parts.length == 3) {
>             String valueFalse = parts[2];
>             Binding falseBinding = makeBinding(valueFalse,
> DEFAULT_VALUE_PREFIX, container, description);
>             return new CondBinding(conditionBinding, trueBinding,
> falseBinding, resolver);
>         }
>         return new CondBinding(conditionBinding, trueBinding, resolver);
>     }
>
> // Usage
> class="${cond:selected, selected}"
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Sincerely
*Boris Horvat*

Re: conditional expression

Posted by Matthias <th...@gmail.com>.
@Howard
Thanks for your reply. I know that it was possible with Java code, but i 
tried to avoid to write extra methods for such simple tasks.

@Michael
WOW! Thats really nice. Exactly what I was looking for. I improved it a 
bit to make the false value optional.

     // constructor without falseBinding
     public CondBinding(Binding conditionBinding, Binding trueBinding, 
TypeCoercer resolver) {
         this(conditionBinding, trueBinding, null, resolver);
     }

     // just check if valueFalse is set
     @Override
     public Object get() {
         String valueTrue = resolver.coerce(trueBinding.get(), 
String.class);
         String valueFalse = falseBinding != null ? 
resolver.coerce(falseBinding.get(), String.class) : null;
         Boolean condition = resolver.coerce(conditionBinding.get(), 
Boolean.class);
         return condition != null && condition ? valueTrue : valueFalse;
     }


     // in BindingFactory
     @Override
     public Binding newBinding(String description, ComponentResources 
container, ComponentResources component, String expression,
             Location location) {
         String[] parts = expression.split(DELIMETER, 3);

         // well i'm not sure what happens here if parts < 2
         if (parts.length < 2) {
             return bindingSource.newBinding(description, container, 
component, "prop", expression, location);
         }

         String condition = parts[0];
         String valueTrue = parts[1];
         Binding conditionBinding = makeBinding(condition, 
DEFAULT_CONDITION_PREFIX, container, description);
         Binding trueBinding = makeBinding(valueTrue, 
DEFAULT_VALUE_PREFIX, container, description);

         if (parts.length == 3) {
             String valueFalse = parts[2];
             Binding falseBinding = makeBinding(valueFalse, 
DEFAULT_VALUE_PREFIX, container, description);
             return new CondBinding(conditionBinding, trueBinding, 
falseBinding, resolver);
         }
         return new CondBinding(conditionBinding, trueBinding, resolver);
     }

// Usage
class="${cond:selected, selected}"

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: conditional expression

Posted by Michael <ga...@gmail.com>.
//Binding
public class CondBinding extends AbstractBinding {
private final Binding conditionBinding;
private final Binding trueBinding;
private final Binding falseBinding;
private final TypeCoercer resolver;

public CondBinding(Binding conditionBinding, Binding trueBinding, 
Binding falseBinding, TypeCoercer resolver) {
this.trueBinding = trueBinding;
this.falseBinding = falseBinding;
this.conditionBinding = conditionBinding;
this.resolver = resolver;
}


@Override
public Object get() {
String valueTrue = resolver.coerce(trueBinding.get(), String.class);
String valueFalse = resolver.coerce(falseBinding.get(), String.class);
Boolean condition = resolver.coerce(conditionBinding.get(), Boolean.class);
return condition != null && condition ? valueTrue : valueFalse;
}
}

//Factory
public class CondBindingFactory implements BindingFactory {
private static final String DELIMETER = "\\s*,\\s*";
private static final String DEFAULT_VALUE_PREFIX = "literal";
private static final String DEFAULT_CONDITION_PREFIX = "prop";

private final BindingSource bindingSource;
private final TypeCoercer resolver;

public CondBindingFactory(BindingSource bindingSource, TypeCoercer 
resolver) {
this.bindingSource = bindingSource;
this.resolver = resolver;
}

@Override
public Binding newBinding(String description, ComponentResources 
container, ComponentResources component, String expression, Location 
location) {
String[] parts = expression.split(DELIMETER, 3);

if (parts.length < 3) {
return bindingSource.newBinding(description, container, component, 
"prop", expression, location);
}

String condition = parts[0];
String valueTrue = parts[1];
String valueFalse = parts[2];

return new CondBinding(makeBinding(condition, DEFAULT_CONDITION_PREFIX, 
container, description),
makeBinding(valueTrue, DEFAULT_VALUE_PREFIX, container, description),
makeBinding(valueFalse, DEFAULT_VALUE_PREFIX, container, description), 
resolver);
}

private Binding makeBinding(String expression, String defaultPrefix, 
ComponentResources container, String description) {
String prefix = defaultPrefix;
String reference = "";

if (!StringUtils.isEmpty(expression)) {
String[] parts = expression.split("\\s*:\\s*", 1);

prefix = parts.length == 2 ? parts[0] : defaultPrefix;
reference = parts.length == 2 ? parts[1] : parts[0];
}

return bindingSource.newBinding(description, container, prefix, reference);
}
}

//Contribution in AppModule
public static void contributeBindingSource(MappedConfiguration<String, 
BindingFactory> configuration,
BindingSource bindingSource, TypeCoercer coercer) {
configuration.add("cond", new CondBindingFactory(bindingSource, coercer));
}

//Usage

${cond:isActive, literal:active, literal:notActive}


But for CSS classes and boolean values its better to use something like 
that:
<div class="active_${isActive}"/>

/*Active css class*/
.active_true {}

/*Unactive css class*/
.active_false {}

17.11.2013 18:30, Matthias пишет:
> Hi, if theres something in tapestry for inline conditional 
> expressions? I think of something like
>
> <a href="www.google.com" class="${isActive ? 'active' : 
> 'notActive'}">google</a>
>
> I found nothing like that, but i loved to have it.
>
> Thanks in advance
> Matthias
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>

-- 
Best regards,
Michael Gagauz


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: conditional expression

Posted by Howard Lewis Ship <hl...@gmail.com>.
You do it in Java code.  public String getLinkClass() { return isActive ?
"active" : "notActive"; }  and just reference ${linkClass} in the template.


On Sun, Nov 17, 2013 at 6:30 AM, Matthias <th...@gmail.com> wrote:

> Hi, if theres something in tapestry for inline conditional expressions? I
> think of something like
>
> <a href="www.google.com" class="${isActive ? 'active' :
> 'notActive'}">google</a>
>
> I found nothing like that, but i loved to have it.
>
> Thanks in advance
> Matthias
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com