Example #1
0
        private static bool InvokePartial(
            string partialName,
            BindingContext context,
            IHandlebarsEngine engine)
        {
            var templateRegistry = engine.Configuration.HandlebarsTemplateRegistry;

            if (!templateRegistry.TryGetTemplate(partialName, out HandlebarsTemplate template))
            {
                var partialLookupKey = $"{context.TemplateName ?? string.Empty}+{partialName}";
                if (!templateRegistry.TryGetTemplate(partialLookupKey, out template))
                {
                    template = engine.CompileView(partialName, context.TemplateName, false);
                    if (template == null)
                    {
                        return(false);
                    }
                    templateRegistry.RegisterTemplate(partialLookupKey, template);
                }
                else
                {
                    return(false);
                }
            }

            try
            {
                template.RenderTo(context.TextWriter, context);
                return(true);
            }
            catch (Exception exception)
            {
                throw new HandlebarsRuntimeException(
                          $"Runtime error while rendering partial '{partialName}', see inner exception for more information",
                          exception);
            }
        }