ERB like library for C#

2019-06-03 16:14发布

问题:

I am looking to find a librray that emulates part of the capabilities of Ruby's ERB library. ie substitute text for variables between <% and %>. I dont need the code execution part that ERB provides but if you know of something that has this I would be super appreciative.

回答1:

Have a look at TemplateMachine, I haven't tested it, but it seems to be a bit ERB-like.



回答2:

I modified a class I used to test some things a while ago. It's not even nearly as good as ERB but it gets the job done substituting text. It only works with properties though, so you might want to fix that.

Usage:

Substitutioner sub = new Substitutioner(
    "Hello world <%=Wow%>! My name is <%=Name%>");

MyClass myClass = new MyClass();
myClass.Wow = 42;
myClass.Name = "Patrik";

string result = sub.GetResults(myClass);

Code:

public class Substitutioner
{
    private string Template { get; set; }

    public Substitutioner(string template)
    {
        this.Template = template;
    }

    public string GetResults(object obj)
    {
        // Create the value map and the match list.
        Dictionary<string, object> valueMap = new Dictionary<string, object>();
        List<string> matches = new List<string>();

        // Get all matches.
        matches = this.GetMatches(this.Template);

        // Iterate through all the matches.
        foreach (string match in matches)
        {
            if (valueMap.ContainsKey(match))
                continue;

            // Get the tag's value (i.e. Test for <%=Test%>.
            string value = this.GetTagValue(match);

            // Get the corresponding property in the provided object.
            PropertyInfo property = obj.GetType().GetProperty(value);
            if (property == null)
                continue;

            // Get the property value.
            object propertyValue = property.GetValue(obj, null);

            // Add the match and the property value to the value map.
            valueMap.Add(match, propertyValue);
        }

        // Iterate through all values in the value map.
        string result = this.Template;
        foreach (KeyValuePair<string, object> pair in valueMap)
        {
            // Replace the tag with the corresponding value.
            result = result.Replace(pair.Key, pair.Value.ToString());
        }

        return result;
    }

    private List<string> GetMatches(string subjectString)
    {
        try
        {
            List<string> matches = new List<string>();
            Regex regexObj = new Regex("<%=(.*?)%>");
            Match match = regexObj.Match(subjectString);
            while (match.Success)
            {
                if (!matches.Contains(match.Value))
                    matches.Add(match.Value);
                match = match.NextMatch();
            }
            return matches;
        }
        catch (ArgumentException)
        {
            return new List<string>();
        }
    }

    public string GetTagValue(string tag)
    {
        string result = tag.Replace("<%=", string.Empty);
        result = result.Replace("%>", string.Empty);
        return result;
    }
}


回答3:

Updated the post

the links which were of help are not longer available. I have left the titles so you can google them.

also look for "C# Razor" (this is the template engine which MS use with MVC)


there are a couple more out there.

Visual Studio ships with T4, which is a templating engine (that is vs 2008, 2005 requires a free add on)

Free T4 Editor - DEAD LINK

T4 Screen Cast- DEAD LINK

There is an open sourse project called Nvolicity, which was taken over by Castle Project

Nvolictiy Castle project upgrades - DEAD LINK

HTH Bones



回答4:

I just released a very simple library for substituting slightly like ERB.

You cannot evaluate in <%%> braces, you can only use these braces: <%= key_value %>. key_value will be the key for the Hashtable you pass as substituting arguments and with the braces being replaced by the value in the Hashtable. That's all.

https://github.com/Joern/C-Sharp-Substituting

Yours,

Joern