Пример #1
0
        string RenderPartial(Token token, MustacheContext ctx, string indent)
        {
            string name = token.Name;

            if (Partials == null)
            {
                return(string.Empty);
            }

            var partial = string.Empty;

            if (!Partials.TryGetValue(name, out partial))
            {
                return(string.Empty);
            }

            var key = "# partial #" + name + "#" + indent;

            if (!Cache.ContainsKey(key))
            {
                if (string.IsNullOrEmpty(indent))
                {
                    Cache[key] = new MustacheParser().Parse(partial, Delimiter.Default());
                }
                else
                {
                    var replaced = Regex.Replace(partial, @"^(.+)$", indent + "$1", RegexOptions.Multiline);
                    Cache[key] = new MustacheParser().Parse(replaced, Delimiter.Default());
                }
            }

            return(RenderTokens(ctx, Cache[key]));
        }
Пример #2
0
        string RenderName(Token token, MustacheContext ctx, bool escape)
        {
            var value = ctx.Lookup(token.Name);

            if (value == null && StrictMode)
            {
                throw new MustacheException(string.Format("lookup failed name:'{0}' around:\n...{1}...\n", token.Name, token.SurroundingTemplate));
            }

            if (value == null)
            {
                return(string.Empty);
            }

            if (value.IsLambda())
            {
                var template = value.InvokeNameLambda() as string;
                var tokens   = new MustacheParser().Parse(template, Delimiter.Default());
                value = RenderTokens(ctx, tokens);

                // Recheck for null
                if (value == null)
                {
                    return(string.Empty);
                }
            }

            var s = value.ToString();

            if (s == "{ }")
            {
                return(string.Empty);
            }

            if (escape)
            {
                return(System.Web.HttpUtility.HtmlEncode(s));
            }

            return(s);
        }
Пример #3
0
        string RenderSection(Token token, MustacheContext ctx)
        {
            var value = ctx.Lookup(token.Name);

            if (value == null && StrictMode)
            {
                throw new MustacheException(string.Format("lookup failed name:'{0}' around:\n...{1}...\n", token.Name, token.SurroundingTemplate));
            }

            if (value.ShouldNotRender())
            {
                return(string.Empty);
            }

            if (value.IsLambda())
            {
                var template = value.InvokeSectionLambda(token.SectionTemplate) as string;
                if (!string.IsNullOrEmpty(template))
                {
                    if (!Cache.ContainsKey(template))
                    {
                        // When Lambdas are used as the data value for Section tag,
                        // the returned value MUST be rendered against the current delimiters.
                        Cache[template] = new MustacheParser().Parse(template, token.CurrentDelimiter);
                    }
                    return(RenderTokens(ctx, Cache[template]));
                }
            }

            if (value is ICollection)
            {
                var sb = new StringBuilder();
                foreach (object item in value as ICollection)
                {
                    sb.Append(RenderTokens(new MustacheContext(item, ctx), token.Children));
                }
                return(sb.ToString());
            }

            return(RenderTokens(new MustacheContext(value, ctx), token.Children));
        }
Пример #4
0
        /// <summary>
        /// Parses and applies given template and returns rendered string.
        /// </summary>
        /// <param name="template">Mustache template string.</param>
        /// <param name="data">Data object.</param>
        /// <param name="partials">Partial templates.</param>
        /// <returns>Rendered string.</returns>
        public string Render(string template, object data, Dictionary <string, string> partials = null)
        {
            if (template == null)
            {
                throw new ArgumentNullException("template");
            }

            if (partials != null)
            {
                Partials = partials;
            }

            if (string.IsNullOrEmpty(template))
            {
                return(string.Empty);
            }

            if (!Cache.ContainsKey(template))
            {
                Cache[template] = new MustacheParser().Parse(template, Delimiter.Default());
            }

            return(RenderTokens(new MustacheContext(data, null), Cache[template]));
        }