using System;
using System.Collections.Generic;
using Gallium;
using HandlebarsDotNet;
using Nitride.Contents;
namespace Nitride.Handlebars
{
///
/// An operation that uses the content to create a template that is then
/// applied against the content and metadata and then replaces the content
/// of that entity.
///
public class ApplyContentHandlebarsTemplate : NitrideOperationBase
{
private readonly HandlebarsTemplateCache cache;
public ApplyContentHandlebarsTemplate(HandlebarsTemplateCache cache)
{
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; }
///
public override IEnumerable Run(IEnumerable input)
{
this.CheckNotNull(x => x.CreateModelCallback);
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 ApplyContentHandlebarsTemplate WithCreateModelCallback(
Func? callback)
{
this.CreateModelCallback = callback;
return this;
}
private Entity Apply(
Entity entity,
HasHandlebarsTemplate _,
ITextContent content)
{
// Create the model using the callback.
TModel model = this.CreateModelCallback!(entity);
// Create a template from the contents.
string text = content.GetText();
HandlebarsTemplate