Exemplo n.º 1
0
        public override async Task UpdatedAsync(UpdateContentContext context, TitlePart part)
        {
            var settings = GetSettings(part);

            // Do not compute the title if the user can modify it and the text is already set.
            if (settings.Options == TitlePartOptions.Editable && !String.IsNullOrWhiteSpace(part.ContentItem.DisplayText))
            {
                return;
            }

            if (!String.IsNullOrEmpty(settings.Pattern))
            {
                var model = new TitlePartViewModel()
                {
                    Title       = part.Title,
                    TitlePart   = part,
                    ContentItem = part.ContentItem
                };

                var title = await _liquidTemplateManager.RenderStringAsync(settings.Pattern, NullEncoder.Default, model,
                                                                           new Dictionary <string, FluidValue>() { ["ContentItem"] = new ObjectValue(model.ContentItem) });

                title = title.Replace("\r", String.Empty).Replace("\n", String.Empty);

                part.Title = title;
                part.ContentItem.DisplayText = title;
                part.Apply();
            }
        }
        public override async Task UpdatedAsync(UpdateContentContext context, TitlePart part)
        {
            var settings = GetSettings(part);

            // Do not compute the title if the user can modify it and the text is already set.
            if (settings.Options == TitlePartOptions.Editable && !String.IsNullOrWhiteSpace(part.ContentItem.DisplayText))
            {
                return;
            }

            if (!String.IsNullOrEmpty(settings.Pattern))
            {
                var model = new TitlePartViewModel()
                {
                    Title       = part.Title,
                    TitlePart   = part,
                    ContentItem = part.ContentItem
                };

                var templateContext = new TemplateContext();
                templateContext.SetValue("ContentItem", part.ContentItem);
                templateContext.MemberAccessStrategy.Register <TitlePartViewModel>();
                templateContext.SetValue("Model", model);

                var title = await _liquidTemplateManager.RenderAsync(settings.Pattern, NullEncoder.Default, templateContext);

                title = title.Replace("\r", String.Empty).Replace("\n", String.Empty);

                part.Title = title;
                part.ContentItem.DisplayText = title;
                part.Apply();
            }
        }