ERB library for C #

I'm looking to find a librray that emulates some of the capabilities of the Ruby ERB library. those. replace text for variables between <% and%>. I don't need the code execution part that ERB provides, but if you know something that has this, I would be very grateful.

+1


source to share


4 answers


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



+1


source


I changed the class I was using to test some things a while ago. It's not even as good as ERB, but it gets the job done by replacing text. It only works on properties, so you can fix that.

Application:

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 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;
    }
}

      

+2


source


Updated message

links that were helpful are not available. I left the titles so you can google them.

also look for "C # Razor" (this is the templating engine that MS uses with MVC)


there are a couple more.

Visual Studio comes with T4 which is a templating engine (i.e. vs. 2008, 2005 requires a free add)

Free T4 Editor - DEAD LINK

T4 Screen Cast - DEAD LINK

There is an open source project called Nvolicity that was taken over by the Castle Project

Nvolictiy castle project update - DEAD LINK

NTN Bones

+1


source


I just released a very simple replacement library a bit like ERB.

You can not evaluate in braces <%%>

, you can only use these braces <%= key_value %>

. key_value

will be the key for the Hashtable that you pass as replacement arguments, and the parentheses are replaced with the value in the Hashtable. It's all.

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

Your

Jorn

0


source







All Articles