 | HandlebarsViewGetViewContext Method |
Gets the
ViewContext that was stored in the context when rendering began for the view.
Namespace: HandlebarsDotNet.MvcAssembly: HandlebarsDotNet.Mvc (in HandlebarsDotNet.Mvc.dll) Version: 1.0.0-beta
Syntaxpublic static ViewContext GetViewContext(
Object context
)
Public Shared Function GetViewContext (
context As Object
) As ViewContext
public:
static ViewContext^ GetViewContext(
Object^ context
)
static member GetViewContext :
context : Object -> ViewContext
Parameters
- context
- Type: SystemObject
The context as returned by SetupContext(ViewContext).
Return Value
Type:
ViewContextThe
ViewContext for this view.
Examples
This example registers a helper globally for the view engine. In this case it needs the ViewContext in order to instantiate a HtmlHelper.
Note that this is only an example and doesn't include error checking.
var hbsve = new HandlebarsViewEngine();
hbsve.RegisterHelper("formatvalue", (writer, context, args) =>
{
object val = args[0];
string format = args[1] as string;
ViewContext viewContext = HandlebarsView.GetViewContext(context);
HtmlHelper htmlHelper = new HtmlHelper(viewContext, viewContext.View as HandlebarsView);
string formatted = htmlHelper.FormatValue(val, format);
writer.Write(formatted);
});
ViewEngines.Engines.Add(hbsve);
public ActionResult Index()
{
var model = new { pi = 3.14159265358979 };
return View(model);
}
{{formatvalue pi "Pi is about {0:N}"}}
See Also