StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
405 stars 58 forks source link

Is it possible to pass a Dictionary<string, Func<IParametro, object>> to Render? #90

Closed paulodiogo closed 4 years ago

paulodiogo commented 4 years ago

Hello, I have an complex and dynamic object that has a list of IParametro and these guys has an method ObterValor. I am trying to render a template that is like ´XPTO {{X}}´ where X is a IParametro.

I am trying to do something like:

Dictionary<string, Func<IParametro, object>> dados = new Dictionary<string, Func<IParametro, object>>();

But the render returns: XPTO System.Func`2[LogicaPesquisa.Negocio.IParametro,System.Object]

Is it possible to pass a Dictionary<string, Func<IParametro, object>>?

Thank you! Stay safe!

I created a class like this:

private class ItemParametro
{
    private readonly IParametro parametro;

    public ItemParametro(IParametro parametro)
    {
        this.parametro = parametro;
    }

    public override string ToString()
    {
        return parametro?.ObterValor()?.ToString();
    }
}

this class smells =>.

Romanx commented 4 years ago

Hi there,

I'm not entirely sure of the use case but the only way to have a Func called when rendering is to us our Lambda functionality, the issue here is your signature doesn't match what Stubble expects. If stubble doesn't know how to render a value it will call ToString on the value so one option if you're trying to render a template XPTO {{X}} where X is a IParametro is to add a ToString() on the class which will be used in the render.

Hopefully this helps some, if not could you provide a small reproduction sample and we can try help more?

Thanks, stay safe too

paulodiogo commented 4 years ago

That helped. I used other approach and in the end used a Dictionary<string, string>.

Thank you! You guys made a great job here!