Пример #1
0
            public void Registering_helper_doesnt_pollute_global_Handlebars()
            {
                var hbsve = new HandlebarsViewEngine();

                hbsve.RegisterHelper("name", (writer, context, args) =>
                {
                    writer.Write("Rendered by helper in HandlebarsViewEngine");
                });

                // compile with global Handlebars
                var func = Handlebars.Compile("Name: {{name}}");

                string output = func(new {});

                Assert.Equal("Name: ", output);
            }
Пример #2
0
        public void Handlebars_1_9_doesnt_throw_when_compiling_mustache_as_part_of_string()
        {
            var vpp = new VPP(
                new VPP.Dir("Views",
                            new VPP.Dir("Home",
                                        new VPP.File("index.hbs", "Pi is about {{formatvalue pi '{0:N}'}}")
                                        )
                            )
                );
            var hbsve = new HandlebarsViewEngine();

            hbsve.VirtualPathProvider = vpp;

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

            var httpContext = new Mock <HttpContextBase>();
            var controller  = new Mock <ControllerBase>();
            var routeData   = new RouteData();

            routeData.Values.Add("controller", "Home");
            var controllerContext = new ControllerContext(httpContext.Object, routeData, controller.Object);

            var viewengineResult = hbsve.FindView(controllerContext, viewName: "index", masterName: null, useCache: false);
            var hbsview          = viewengineResult.View;

            string actual = GetHtmlFromView(hbsview, new
            {
                pi = 3.14159265358979
            });

            Assert.Equal("Pi is about 3.14", actual);
        }