0

I'm currently working on a Web project for which I need to be able to send an email. I started using DotLiquid for the templating engine, but I found certain issues with it, the biggest being that it does not seem to be able to handle enums.

I tried to register the type as "Safe" like that Template.RegisterSafeType(typeof(Gender), new string[] { "Male", "Female" });, but it does not seem to work. At best, there is no longer any exception thrown, but the expected result is empty.

<ul>
    {% for f in Model.Friends %}
        {% if f.Gender == Male %}
            <li>
                {{ f.FirstName }} {{ f.LastName }} {{ f.Gender }}
            </li>
        {% endif %}
    {% endfor %}
</ul>

<ul>
    {% for f in Model.Friends %}
        {% if f.Gender == Gender.Male %}
            <li>
                {{ f.FirstName }} {{ f.LastName }} {{ f.Gender }}
            </li>
        {% endif %}
    {% endfor %}
</ul>

<ul>
    {% for f in Model.Friends %}
        {% if f.Gender == 0 %}
            <li>
                {{ f.FirstName }} {{ f.LastName }} {{ f.Gender }}
            </li>
        {% endif %}
    {% endfor %}
</ul>

None of the above were able to return anything. I would be happy with a string or int representation of the enum, but there is nothing for now. Has anyone found a solution to this issue ?

I would like to avoid to "transform" the enum myself in the Drop object as this could be confusing later on.

Thank you.

4

1 回答 1

3

好的,所以如果有人对此感兴趣,我找到了答案。

当您注册类型时,您还可以Func<object,object>根据您的重载将 a 指定为第二个或第三个参数。此函数允许您指定转换值。

因此,在我的示例中,您可以这样做:

Template.RegisterSafeType(typeof(Gender), o => o.ToString());

它开始工作。

于 2014-09-30T17:24:22.020 回答