views:

335

answers:

3

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  A: 

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

PEZ
this looks like what I am looking for.
MikeJ
+1  A: 

Hi,

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

T4 Screen Cast

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

Nvolictiy Castle project upgrades

HTH Bones

dbones
+2  A: 

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;
    }
}
Patrik
i always like it when people post code. wonder how I give right answer to all the responses?
MikeJ