You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Brian Long <bl...@annadaletech.com> on 2011/10/25 16:14:47 UTC

property expressions

Hi all,

was hoping someone could point me in the direction on a good tutorial
on the new property expressions in tapestry 5.2.6? I was using OGNL
previously and would like to update these expression to work with
ANTLR instead.

The ognl expression i had was to decorate text field with different
classnames depending on whether the field was null

<t:textfield t:id="surname" t:value="surname" class="${ognl:surname ==
null ? 'touch-and-type' : ''}" />

Regards, Brian.

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


Re: property expressions

Posted by Josh Canfield <jo...@gmail.com>.
> <t:textfield t:id="surname" t:value="surname" class="${ognl:surname ==
> null ? 'touch-and-type' : ''}" />

Most tapestry folk will agree that putting logic in your templates
makes them brittle and hard to maintain, but sometimes need a little
something in the template. I created a simple utility binding to help
out in these kinds of situations.

Example usage:
<div class="products ${if:items?list|empty}">

Why | ? because : isn't valid in the text of a binding, or wasn't at
the time I wrote this.


import org.apache.tapestry5.Binding;
import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.internal.bindings.AbstractBinding;
import org.apache.tapestry5.ioc.Location;
import org.apache.tapestry5.services.BindingFactory;
import org.apache.tapestry5.services.BindingSource;

/**
 * Implementation of the if: binding prefix
 * default binding is literal, other bindings can be used by
specifying prefix.<br>
 * example: "if:prop?selected|prop:x"
 */
public class IfBindingFactory implements BindingFactory {
    private final BindingSource _bindingSource;

    public IfBindingFactory(BindingSource source) {
        this._bindingSource = source;
    }

    public Binding newBinding(String description, ComponentResources
container, ComponentResources component,
                              String expression, Location location) {
        final String[] p1 = expression.split("\\?", 2);
        final String[] p2 = p1[1].split("\\|", 2);

        Object testBinding = _bindingSource.newBinding(description,
container, component, BindingConstants.PROP, p1[0], location);
        Object trueBinding = p2[0];
        if (p2[0].length() > 0) {
            trueBinding = _bindingSource.newBinding(description,
container, component, BindingConstants.LITERAL, p2[0], location);
        }

        Object falseBinding = "";
        if (p2.length == 2 && p2[1].length() > 0) {
            falseBinding = _bindingSource.newBinding(description,
container, component, BindingConstants.LITERAL, p2[1], location);
        }

        return new IfBinding(testBinding, trueBinding, falseBinding);
    }

    public class IfBinding extends AbstractBinding {
        private Object _test;
        private Object _true;
        private Object _false;


        public IfBinding(Object testBinding, Object trueBinding,
Object falseBinding) {
            _test = testBinding;
            _true = trueBinding;
            _false = falseBinding;
        }

        public Object get() {
            final Object o = get(_test);
            if (o instanceof Boolean) {
                return o.equals(true) ? get(_true) : get(_false);
            }
            return o == null ? get(_false) : get(_true);
        }

        @Override
        public boolean isInvariant() {
            return false;
        }

        @Override
        public Class<Object> getBindingType() {
            return Object.class;
        }

        private Object get(Object o) {
            if (o instanceof Binding) {
                return ((Binding) o).get();
            }
            return o;
        }
    }
}



On Tue, Oct 25, 2011 at 2:14 PM, Brian Long <bl...@annadaletech.com> wrote:
> Hi all,
>
> was hoping someone could point me in the direction on a good tutorial
> on the new property expressions in tapestry 5.2.6? I was using OGNL
> previously and would like to update these expression to work with
> ANTLR instead.
>
> The ognl expression i had was to decorate text field with different
> classnames depending on whether the field was null
>
> <t:textfield t:id="surname" t:value="surname" class="${ognl:surname ==
> null ? 'touch-and-type' : ''}" />
>
> Regards, Brian.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

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