Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lowercasing of tokens? #31

Closed
richard-edwards opened this issue Aug 14, 2014 · 2 comments
Closed

Lowercasing of tokens? #31

richard-edwards opened this issue Aug 14, 2014 · 2 comments

Comments

@richard-edwards
Copy link

Is there a way to lowercase a token? I'm looking for something like {{Entity:lowercase}}. If Entity value is 'Ticket', then I want 'ticket'

@richard-edwards
Copy link
Author

Sorry, missed the docs on this one. Created my own this way.

public class LowerTagDefinition : InlineTagDefinition
{

    public LowerTagDefinition()
        : base("lower")
    {
    }

    protected override IEnumerable<TagParameter> GetParameters()
    {
        return new[] { new TagParameter("param") { IsRequired = true } };
    }

    public override void GetText(TextWriter writer, Dictionary<string, object> arguments, Scope context)
    {
        writer.Write(arguments["param"].ToString().ToLower());
    }
}

Usage is : {{#lower MyVar}}

@jehugaleahsa
Copy link
Owner

There are two options, create a custom tag or override the KeyFound event.

Option 1:

public class LowerCaseTagDefinition : InlineTagDefinition
{
    private const string keyParameterName = "key";

    public LowerCaseTagDefinition()
        : base("lowercase")
    {
    }

    protected override IEnumerable<TagParameter> GetParameters()
    {
        return new TagParameter[] { new TagParameter(keyParameterName) };
    }

    public override void GetText(TextWriter writer, Dictionary<string, object> arguments, Scope context)
    {
        object value;
        if (arguments.TryGetValue(keyParameterName, out value) && value != null)
        {
            writer.Write(value.ToString().ToLowerInvariant());
        }
    }
}

// ...

FormatCompiler compiler = new FormatCompiler();
compiler.RegisterTag(new LowerCaseTagDefinition(), true);

Generator generator = compiler.Compile("{{#lowercase name}}");
string value = generator.Render(new { name = "Bob Smith" });

Option 2:

FormatCompiler compiler = new FormatCompiler();
Generator generator = compiler.Compile("{{name}}");
generator.KeyFound += (sender, e) => {
    e.Substitute = e.Substitute == null ? null : Convert.ToString(e.Substitute).ToLowerInvariant();
};
string value = generator.Render(new { name = "Bob Smith" });

The first approach has the benefit of giving more control over which tags are made lowercase. The second option is a lot less code. Although, if you want to control which keys are made lowercase, it becomes a bit of a pain. The second approach is also the only way to apply formatting via {{key:format}}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants