[stringtemplate-interest] Conditional output of multivalued attributes / How to handle separator problem?

Ivan Vedic Ai vedic.ai at gmail.com
Sun Mar 18 12:44:43 PDT 2007


Hi all,

I'm having trouble designing a stringtemplate for code of a C#
constructor with arguments. I would like to construct a template that
would output items $it$ of a multivalued attribute only if item's
"$it.IsSet$" boolean property is set to "true". The trouble I'm having
is with the set 'separator=", "' that is written for all items, even
those excluded by my condition.

My constructor.st stringtemplate looks like this:
------------------------------------------------------
public $class.Name$($class.Parameters:{$if(it.IsSet)$$it.Type$
$it.Name$$endif$}; separator=", "$)
{
  $class.Parameters:{$if(it.IsSet)$this.$it.Name$ =
$it.Name$;$endif$}; separator="\r\n"$
}
------------------------------------------------------

Between the brackets I output a multivalued attribute
$class.Parameters$, but I only output $it.Type$ and $it.Name$ if the
value of $it.IsSet$ is set to "true".

The output I get is as follows:
------------------------------------------------------
public Miki(string name, , TestEnum enum)
{
   this._name = name;

   this._enum = enum;
}
------------------------------------------------------

What I would like to achieve is not output the separator for items
which do not get written out (notice the ", , " and an extra newline).

Is there a way arround this problem?
Can someone please tell me how should I design my stringtemplate to
achieve this functionality?



Here is the definition of my template setAttribute classes:
------------------------------------------------------
public class TestClass
{
    private string _name;
    private List<ParameterClass> _parameters;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public TestClass()
    {
        _parameters = new List<ParameterClass>();
    }

    public TestClass(string name)
        : this()
    {
        _name = name;
    }

    public List<ParameterClass> Parameters
    {
        get { return _parameters; }
        set { _parameters = value; }
    }
}

public class ParameterClass
{
    private string _type;
    private string _name;
    private bool _isSet;

    public string Type
    {
        get { return _type; }
        set { _type = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public bool IsSet
    {
        get { return _isSet; }
        set { _isSet = value; }
    }

    public ParameterClass(string type, string name, bool isSet)
    {
        _type = type;
        _name = name;
        _isSet = isSet;
    }
}
------------------------------------------------------

I initialize a "class" attribute with my TestClass() instance like this:
------------------------------------------------------
TestClass clazz = new TestClass("Miki");
clazz.Parameters.Add(new ParameterClass("string", "Name", true));
clazz.Parameters.Add(new ParameterClass("string", "Parameters", false));
clazz.Parameters.Add(new ParameterClass("TestEnum", "Enum", true));

template.SetAttribute("class", clazz);
------------------------------------------------------


More information about the stringtemplate-interest mailing list