using System;
using System.Collections.Generic;
using FluentValidation;
using Gallium;
using HandlebarsDotNet;
using Nitride.Contents;
namespace Nitride.Handlebars;
///
/// An operation that applies a common or shared template on the content of
/// a document that includes theme or styling information.
///
public class ApplyStyleTemplate : OperationBase
{
// TODO: This does not use [WithProperties] because the source generator hasn't been taught how to do generics.
private readonly HandlebarsTemplateCache cache;
private readonly IValidator> validator;
public ApplyStyleTemplate(IValidator> validator, HandlebarsTemplateCache cache)
{
this.validator = validator;
this.cache = cache;
}
///
/// Gets or sets the callback used to create a model from a given
/// entity. This allows for the website to customize what information is
/// being passed to the template.
///
public Func? CreateModelCallback { get; set; }
///
/// Gets or sets the callback used to determine which template to use
/// for a given entity. This lets one have a per-page template change.
///
public Func? GetTemplateName { get; set; }
public IHandlebars? Handlebars { get; set; }
///
public override IEnumerable Run(IEnumerable input)
{
// Make sure we have sane data.
this.validator.ValidateAndThrow(this);
// Create and set up the Handlebars.
return input.ForEachEntity(this.Apply);
}
///
/// Sets the callback for the template and returns the operation to
/// chain operations.
///
/// The callback to set.
/// The ApplyContentHandlebarsTemplate to chain requests.
public ApplyStyleTemplate WithCreateModelCallback(Func? callback)
{
this.CreateModelCallback = callback;
return this;
}
public ApplyStyleTemplate WithGetTemplateName(Func? callback)
{
this.GetTemplateName = callback;
return this;
}
public ApplyStyleTemplate WithHandlebars(IHandlebars? handlebars)
{
this.Handlebars = handlebars;
return this;
}
private Entity Apply(Entity entity, ITextContent content)
{
TModel model = this.CreateModelCallback!(entity);
string name = this.GetTemplateName!(entity);
HandlebarsTemplate