3

Is there a good way to replace placeholders with dynamic data ? I have tried loading a template and then replaced all {{PLACEHOLDER}}-tags, with data from the meta object, which is working. But if I need to add more placeholders I have to do it in code, and make a new deployment, so if it is possible I want to do it through the database, like this:

Table Placeholders
ID, Key (nvarchar(50),  Value (nvarchar(59))
1   {{RECEIVER_NAME}}   meta.receiver
2   {{RESOURCE_NAME}}   meta.resource
3 ..
4 .. and so on

the meta is the name of the parameter sent in to the BuildTemplate method.

So when I looping through all the placeholders (from the db) I want to cast the value from the db to the meta object. Instead of getting "meta.receiver", I need the value inside the parameter.

GetAllAsync ex.1

public async Task<Dictionary<string, object>> GetAllAsync()
{
     return await _context.EmailTemplatePlaceholders.ToDictionaryAsync(x => x.PlaceholderKey, x => x.PlaceholderValue as object);
}

GetAllAsync ex.2

public async Task<IEnumerable<EmailTemplatePlaceholder>> GetAllAsync()
{
     var result = await _context.EmailTemplatePlaceholders.ToListAsync();
     return result;
}

sample not using db (working))

private async Task<string> BuildTemplate(string template, dynamic meta)
{
    var sb = new StringBuilder(template);

    sb.Replace("{{RECEIVER_NAME}}", meta.receiver?.ToString());
    sb.Replace("{{RESOURCE_NAME}}", meta.resource?.ToString());    

    return sb.ToString();
}

how I want it to work

private async Task<string> BuildTemplate(string template, dynamic meta)
{
    var sb = new StringBuilder(template);

    var placeholders = await _placeholders.GetAllAsync();

    foreach (var placeholder in placeholders)
    {           
        // when using reflection I still get a string like "meta.receiver" instead of meta.receiver, like the object.
        // in other words, the sb.Replace methods gives the same result.
        //sb.Replace(placeholder.Key, placeholder.Value.GetType().GetField(placeholder.Value).GetValue(placeholder.Value));
        sb.Replace(placeholder.Key, placeholder.Value);
    }  

    return sb.ToString();
}

I think it might be a better solution for this problem. Please let me know!

3
  • I think you want string interpolation like in angularjs?
    – habib
    May 31, 2018 at 15:01
  • "CODE NOT WORKING" is not a good description. The code should work provided you have the right value in the placeholder.Value property. If you don't, then it's not the code you've posted that is incorrect, it's the .GetAllAsync() method, which you haven't posted. Please post a minimal reproducible example. Jun 1, 2018 at 8:57
  • Hi Lasse, thank you for your correction! The thing is that I have tried different types of GetAllAsync return IEnumerable<T>, IDictionary<T> and so on, and I get same problem. The Code is WORKING, but NOT as I WANT.
    – n3tx
    Jun 1, 2018 at 9:09

4 Answers 4

4

We have solved similar issue in our development.

We have created extension to format any object.

Please review our source code:

public static string FormatWith(this string format, object source, bool escape = false)
{
    return FormatWith(format, null, source, escape);
}

public static string FormatWith(this string format, IFormatProvider provider, object source, bool escape = false)
{
    if (format == null)
        throw new ArgumentNullException("format");

    List<object> values = new List<object>();
    var rewrittenFormat = Regex.Replace(format,
        @"(?<start>\{)+(?<property>[\w\.\[\]]+)(?<format>:[^}]+)?(?<end>\})+",
        delegate(Match m)
        {
            var startGroup = m.Groups["start"];
            var propertyGroup = m.Groups["property"];
            var formatGroup = m.Groups["format"];
            var endGroup = m.Groups["end"];

            var value = propertyGroup.Value == "0"
                ? source
                : Eval(source, propertyGroup.Value);

            if (escape && value != null)
            {
                value = XmlEscape(JsonEscape(value.ToString()));
            }

            values.Add(value);

            var openings = startGroup.Captures.Count;
            var closings = endGroup.Captures.Count;

            return openings > closings || openings%2 == 0
                ? m.Value
                : new string('{', openings) + (values.Count - 1) + formatGroup.Value
                  + new string('}', closings);
        },
        RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

    return string.Format(provider, rewrittenFormat, values.ToArray());
}

private static object Eval(object source, string expression)
{
    try
    {
        return DataBinder.Eval(source, expression);
    }
    catch (HttpException e)
    {
        throw new FormatException(null, e);
    }
}

The usage is very simple:

var body = "[{Name}] {Description} (<a href='{Link}'>See More</a>)";
var model = new { Name="name", Link="localhost", Description="" };
var result = body.FormatWith(model);
0

You want to do it like this:

sb.Replace(placeholder.Key, meta.GetType().GetField(placeholder.Value).GetValue(meta).ToString())

and instead of meta.reciever, your database would just store receiver

This way, the placeholder as specified in your database is replaced with the corresponding value from the meta object. The downside is you can only pull values from the meta object with this method. However, from what I can see, it doesn't seem like that would be an issue for you, so it might not matter.

More clarification: The issue with what you tried

//sb.Replace(placeholder.Key, placeholder.Value.GetType().GetField(placeholder.Value).GetValue(placeholder.Value));

is that, first of all, you try to get the type of the whole string meta.reciever instead of just the meta portion, but then additionally that there doesn't seem to be a conversion from a string to a class type (e.g. Type.GetType("meta")). Additionally, when you GetValue, there's no conversion from a string to the object you need (not positive what that would look like).

2
  • Hi Nick, thank you for your solution, but it is not working for me. I get error CS1503: Argument 1: cannot convert from 'object' to 'string' and when i add .ToString() "placeholder.Value.ToString()" I get "Object reference not set to an instance of an object".
    – n3tx
    May 31, 2018 at 19:47
  • My bad, it should definitely be placeholder.Value.ToString(). This error, however, occurs when your metaobject does not have the value you're seeking. For instance, if placeholder.Value is receiver, and meta.receiver isn't a valid field, this error will be thrown. You can wrap this statement in a try-catch block to avoid these crashes, but the end-point is that you have to be sure the value you're passing in is valid. Did you change your database from meta.receiver to receiver as I suggested?
    – N.D.C.
    Jun 1, 2018 at 16:10
0

As you want to replace all the placeholders in your template dynamically without replacing them one by one manually. So I think Regex is better for these things.

This function will get a template which you want to interpolate and one object which you want to bind with your template. This function will automatically replace your placeholders like {{RECEIVER_NAME}} with values in your object. You will need a class which contain all the properties that you want to bind. In this example by class is MainInvoiceBind.

    public static string Format(string obj,MainInvoiceBind invoice)
    {
        try
        {
            return Regex.Replace(obj, @"{{(?<exp>[^}]+)}}", match =>
            {
                try
                {
                    var p = Expression.Parameter(typeof(MainInvoiceBind), "");
                    var e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new[] { p }, null, match.Groups["exp"].Value);
                    return (e.Compile().DynamicInvoke(invoice) ?? "").ToString();
                }
                catch
                {
                    return "Nill";
                }

            });
        }
        catch
        {
            return string.Empty;
        }
    }

I implement this technique in a project where I hade to generates email dynamically from there specified templates. Its working good for me. Hopefully, Its solve your problem.

0

I updated habibs solution to the more current System.Linq.Dynamic.Core NuGet package, with small improvements.

This function will automatically replace your placeholders like {{RECEIVER_NAME}} with data from your object. You can even use some operators, since it's using Linq.

public static string Placeholder(string input, object obj)
{
    try {
        var p = new[] { Expression.Parameter(obj.GetType(), "") };

        return Regex.Replace(input, @"{{(?<exp>[^}]+)}}", match => {
            try {
                return DynamicExpressionParser.ParseLambda(p, null, match.Groups["exp"].Value)
                  .Compile().DynamicInvoke(obj)?.ToString();
            }
            catch {
                return "(undefined)";
            }
        });
    }
    catch {
        return "(error)";
    }
}

You could also make multiple objects accessible and name them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.