Question

I have a situation where our core project requires heavy templating.

We have come up with a solution which should make the project very flexible, but I am unsure how to implement a solution.

In our views I want to be able to place custom tags like this:

<div class="some view">
     {{SomeTag}}
</div>

Now these tags don't have anything to do with the model, what we want to do is to replace these tags at runtime on the server (not the browser!) with the contents of a file on the server (under some designated directory) called "SomeTag.html".

Is there some way to add a method or override a method in a Base Controller (which will inherit "Controller") which searches through the output of the view for any {{Tag}} and does a replace with it's corresponding Tag.html file?

For example perhaps this is appropriate?

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
    }
Was it helpful?

Solution

You could use a modified form of

HtmlHelper.Partial(partialViewName)

You can write an extension method specific to your scenario

public static MvcHtmlString Template(this HtmlHelper htmlHelper, string templateKey)
{
    string partialViewName = null;

    // get partialViewName according to the template key

    return htmlHelper.Partial(partialViewName);
}

You will use it like

@Html.Template("SomeTag")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top