[antlr-interest] StringTemplate conditional

Barnes, Jeff JB0284 at att.com
Fri Aug 3 05:54:48 PDT 2007


I'm working on a project that specifies a platform independent UI. We
are using StringTemplate for the platform specific html rendering. We
abstract the data fields in the form to a Form object. There are several
cases where the user selects one of a group of things, lets call it
selectedThing. There is a get/set selectedThing pair in the form. This
represents state in the form. StringTemplate conditional processing is
limited to either $if(somethingExists) text$ or $if(myBoolean) text$.
Neither fits the paradigm of having a selectedThing comparison:
$if(thing=selectedThing) text$.

Here is what I want to do more explicitly:

import org.antlr.stringtemplate.StringTemplateGroup;
import org.antlr.stringtemplate.StringTemplate;

public class Test
{
    static class User {
        String name;
        public String getName() {
            return name;
        }
    }

    static class Form {
        User[] users;
        User selectedUser;
        public User[] getUsers() { return users; }
        public User getSelectedUser() { return selectedUser; }
    }

    public static void main(String[] args)
    {
        User[] users = new User[2];
        users[0] = new User();
        users[0].name = "Terence";
        users[1] = new User();
        users[1].name = "Parr";

        Form f = new Form();
        f.users = users;
        f.selectedUser = users[1];

        StringTemplate s = new StringTemplate("$form.users:{user |
<option"+ 
               "$if(user=form.selectedUser)$ selected$endif$>" +
               "$user.name$</option>};separator=\"\n\"$");
        st.setAttribute("form", f);
        System.out.println(f.toString());        
    }
}

The StringTemplate declaration is not valid. Does this break model/view
separation? Why doesn't StringTemplate support this? Is there another
way to conceptualize this problem?

Regards,
Jeff


More information about the antlr-interest mailing list