using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml.XPath;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Templates;
using Umbraco.Cms.Core.Xml;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common
{
///
/// A helper class that provides many useful methods and functionality for using Umbraco in templates
///
///
/// This object is a request based lifetime
///
public class UmbracoHelper
{
private readonly IPublishedContentQuery _publishedContentQuery;
private readonly IUmbracoComponentRenderer _componentRenderer;
private readonly ICultureDictionaryFactory _cultureDictionaryFactory;
private IPublishedContent _currentPage;
private ICultureDictionary _cultureDictionary;
#region Constructors
///
/// Initializes a new instance of .
///
/// The item assigned to the helper.
///
///
///
/// Sets the current page to the context's published content request's content item.
public UmbracoHelper(ICultureDictionaryFactory cultureDictionary,
IUmbracoComponentRenderer componentRenderer,
IPublishedContentQuery publishedContentQuery)
{
_cultureDictionaryFactory = cultureDictionary ?? throw new ArgumentNullException(nameof(cultureDictionary));
_componentRenderer = componentRenderer ?? throw new ArgumentNullException(nameof(componentRenderer));
_publishedContentQuery = publishedContentQuery ?? throw new ArgumentNullException(nameof(publishedContentQuery));
}
///
/// Initializes a new empty instance of .
///
/// For tests - nothing is initialized.
internal UmbracoHelper()
{ }
#endregion
///
/// Gets (or sets) the current item assigned to the UmbracoHelper.
///
///
///
/// Note that this is the assigned IPublishedContent item to the
/// UmbracoHelper, this is not necessarily the Current IPublishedContent
/// item being rendered that is assigned to the UmbracoContext.
/// This IPublishedContent object is contextual to the current UmbracoHelper instance.
///
///
/// In some cases accessing this property will throw an exception if
/// there is not IPublishedContent assigned to the Helper this will
/// only ever happen if the Helper is constructed via DI during a non front-end request.
///
///
/// Thrown if the
/// UmbracoHelper is constructed with an UmbracoContext and it is not a
/// front-end request.
public IPublishedContent AssignedContentItem
{
get
{
if (_currentPage != null)
{
return _currentPage;
}
throw new InvalidOperationException(
$"Cannot return the {nameof(IPublishedContent)} because the {nameof(UmbracoHelper)} was not constructed with an {nameof(IPublishedContent)}."
);
}
set => _currentPage = value;
}
///
/// Renders the template for the specified pageId and an optional altTemplateId
///
/// The content id
/// If not specified, will use the template assigned to the node
public async Task RenderTemplateAsync(int contentId, int? altTemplateId = null)
=> await _componentRenderer.RenderTemplateAsync(contentId, altTemplateId);
#region RenderMacro
///
/// Renders the macro with the specified alias.
///
/// The alias.
///
public async Task RenderMacroAsync(string alias)
=> await _componentRenderer.RenderMacroAsync(AssignedContentItem?.Id ?? 0, alias, null);
///
/// Renders the macro with the specified alias, passing in the specified parameters.
///
/// The alias.
/// The parameters.
///
public async Task RenderMacroAsync(string alias, object parameters)
=> await _componentRenderer.RenderMacroAsync(AssignedContentItem?.Id ?? 0, alias, parameters?.ToDictionary