protected override FormatCompiler CreateFormatCompiler()
        {
            var compiler = new FormatCompiler();

            compiler.RegisterTag(new MustacheJoinTagDefinition(), true);

            return(compiler);
        }
    public string RenderView(ControllerContext ctx, string toRender)
    {
        var fc = new FormatCompiler();

        fc.RegisterTag(new IncludeTag(this, ctx, _webServer.StaticFileController), true);
        var generator = fc.Compile(toRender);

        return(generator.Render(ctx));
    }
Beispiel #3
0
        public MustacheResponse(string templateFile, object args)
        {
            compiler = new FormatCompiler();
            compiler.RemoveNewLines = false;
            this.args = args;

            customTags.ForEach(x => compiler.RegisterTag(x, true));

            generator = compiler.Compile(File.ReadAllText(templateFile));
        }
        protected override void LoadTemplate()
        {
            base.LoadTemplate();

            if (!string.IsNullOrEmpty(this.TemplateHtml))
            {
                this.fileTag = new FileTagDefinition();

                FormatCompiler compiler = new FormatCompiler()
                {
                    RemoveNewLines = false
                };

                compiler.RegisterTag(this.fileTag, true);
                compiler.RegisterTag(new IfMatchTagDefinition(), true);
                compiler.RegisterTag(new ExtendedElseTagDefinition(), false);
                this.generator              = compiler.Compile(this.TemplateHtml);
                this.generator.KeyNotFound += this.Generator_KeyNotFound;
            }
        }
        /// <summary>
        /// Parse a Mustache template with arguments and return the results as a string
        /// </summary>
        /// <param name="mustache"Mustache template source</param>
        /// <param name="args">Class, struct, dynamic or anonymous object</param>
        /// <returns></returns>
        public static string Parse(string mustache, object args)
        {
            var compiler = new FormatCompiler();

            compiler.RemoveNewLines = false;
            customTags.ForEach(x => compiler.RegisterTag(x, true));

            var writer    = new StringWriter();
            var generator = compiler.Compile(mustache);

            generator.Render(args, writer);

            return(writer.ToString());
        }
        /// <summary>
        /// Parse a Mustache template with arguments and return the results as a string
        /// </summary>
        /// <param name="path">Path to the tempalte</param>
        /// <param name="args">Class, struct, dynamic or anonymous object</param>
        /// <returns></returns>
        public static string FromFile(string path, object args)
        {
            var compiler = new FormatCompiler();

            compiler.RemoveNewLines = false;
            customTags.ForEach(x => compiler.RegisterTag(x, true));

            var writer    = new StringWriter();
            var generator = compiler.Compile(cache[Path.GetFullPath(path)]);

            generator.Render(args, writer);

            return(writer.ToString());
        }
        /// <summary>
        /// Instance a new Mustache response with object, dynamic or anonymous parameter
        /// </summary>
        /// <param name="templateFile">Path to the template</param>
        /// <param name="args">Class, struct, dynamic or anonymous object</param>
        public MustacheTemplate(string templateFile, object args)
        {
            this.templateFile = templateFile;

            if (File.Exists(templateFile))
            {
                compiler = new FormatCompiler();
                compiler.RemoveNewLines = false;
                this.args = args;

                customTags.ForEach(x => compiler.RegisterTag(x, true));

                generator = compiler.Compile(cache[Path.GetFullPath(templateFile)]);
            }
        }
        static void Main(string[] args)
        {
            const string html = @"{{Form.Name}} {{#! Hello }} {{url|Homepage}}";

            var compiler = new FormatCompiler();

            compiler.RegisterTag(new CommandTagDefinition("url", Url), true);

            var generator = compiler.Compile(html);

            generator.KeyNotFound += (sender, eventArgs) => Console.WriteLine(eventArgs.Key);

            var model = new FormPost();

            model.Form.Name = "Hello";

            string rendered = generator.Render(model);

            Console.ReadKey();
        }
Beispiel #9
0
        public HttpResponseMessage GetMustacheSharpPage()
        {
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "article2.html");

            using (StreamReader reader = new StreamReader(templatePath))
            {
                string strTemp = reader.ReadToEnd();

                var data = TemplateData.Create();

                FormatCompiler compiler = new FormatCompiler();
                compiler.RegisterTag(new ListTagDefinition <Item>(), true);
                Generator generator = compiler.Compile(strTemp);
                string    result    = generator.Render(data);

                var response = new HttpResponseMessage();
                response.Content = new StringContent(result);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

                return(response);
            }
        }
 public void TestCompile_MissingDefaultParameter_ProvidesDefault()
 {
     FormatCompiler compiler = new FormatCompiler();
     compiler.RegisterTag(new DefaultTagDefinition(), true);
     const string format = @"{{#default}}";
     Generator generator = compiler.Compile(format);
     string result = generator.Render(null);
     Assert.AreEqual("123", result, "The wrong text was generated.");
 }
        public void TestCompile_NestedContext_ConsolidatesWriter()
        {
            FormatCompiler compiler = new FormatCompiler();
            compiler.RegisterTag(new UrlEncodeTagDefinition(), true);

            const string format = @"{{#urlencode}}{{url}}{{/urlencode}}";
            Generator generator = compiler.Compile(format);

            string actual = generator.Render(new { url = "https://google.com" });
            string expected = HttpUtility.UrlEncode("https://google.com");
            Assert.AreEqual(expected, actual, "Value field didn't work");
        }