Tag Archives: regex

Render tags in templates as a partial view with MVC3

Suppose you are rendering templated content. Sometimes you want to reference partial views (or actions) in your templates and have them render with attributes provided by your template. One option is to use a Razor templating engine. But I just needed to render partial views based on a custom tag format, so I came up with my own solution:

        /// <summary>
        ///   Render parameterized tags as a partial view in MVC3 templates
        ///   Supports tags such as 
        /// </summary>
        /// The helper.
        /// The content.
        private static string RenderPartialViewTagsInTemplate(HtmlHelper helper, string content)
            var controls = new Dictionary();
            MatchCollection matches = Regex.Matches(content, @"&lt;view: (?S+)(s+(?[^=s]+)=""?(?[^""s]+)""?)*?s*/&gt;", RegexOptions.ExplicitCapture);
            foreach (Match tag in matches)
                string viewName = tag.Groups["name"].Value;
                var routeValues = new RouteValueDictionary();
                for (int i = 0; i  { content = content.Replace(c.Key, c.Value); });
            return content;