Skip to content

IContentRenderHook

IContentRenderHook runs your code around one component each time it renders. The designer calls it a component modifier: an editor adds it to a component, and it can add a CSS class, a style or an attribute, change the component's property values, write markup around it, or leave it out.

Interface declaration

	namespace Veva.Core.Domain.Components;

	public interface IContentRenderHook
	{
		void Invoke(RenderContext context, RenderTreeBuilder builder, Action<RenderTreeBuilder> next);
	}

Example

This modifier marks a component as written in another language than the page, which screen readers and translation tools read from the lang attribute. The editor types the language code on the modifier.

	using System.ComponentModel;
	using Microsoft.AspNetCore.Components.Rendering;
	using Veva.Core.Domain.Components;

	[DisplayName("Content language")]
	[Description("Sets the lang attribute on content in another language than the page")]
	public class ContentLanguageHook : IContentRenderHook
	{
		[EditorBrowsable]
		[DisplayName("Language code")]
		[Description("For example en or is")]
		public string Language { get; set; }

		public void Invoke(RenderContext context, RenderTreeBuilder builder, Action<RenderTreeBuilder> next)
		{
			if (!string.IsNullOrWhiteSpace(Language))
			{
				// Veva wraps the component in a div that carries this attribute.
				context.Attributes["lang"] = Language.Trim();
			}

			// Always call next, or the component is not rendered.
			next(builder);
		}
	}

Registration

This is how the implementation is registered into the DI container in Veva, this should be done in your module config file (The class which implements IModule).

	using Veva.Core.Application.DependencyInjection;
	using Veva.Core.Domain.Components;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		// The key is the class name; the content stores it when an editor adds the modifier.
		services.AddNamed<IContentRenderHook, ContentLanguageHook>(ServiceLifetime.Transient);
	}

The hook does not run on its own. Registering it adds it to the list of component modifiers in the page designer. An editor adds it to a component, fills in its settings, and can switch it off again without removing it. A component can carry several modifiers, and the editor drags them into the order they run in. The modifier is stored with that one piece of content, so it applies there and nowhere else.

The list shows the class's [DisplayName] and [Description]. Public read/write properties marked [EditorBrowsable] (from System.ComponentModel) become the modifier's settings, like Language above. The content stores the modifier under its registration name, which is the class name. If you rename the class, the stored modifiers no longer find it: the page skips them and the designer lists them as unknown.

How the chain runs. Each time the component renders, Veva calls its modifiers in the editor's order. Each one does its work and calls next(builder) to run the one after it. After the last modifier Veva runs two hooks of its own. The first wraps the component in one div when a modifier has put something in CssClass, InlineStyles, Attributes or CssRules on the context, and adds no element when all four are empty. The second renders the component itself, with the values in PropertyValues.

What a hook can do.

  • Add to the wrapper: context.CssClass.Add(...), context.InlineStyles[...], context.Attributes[...], or a rule in context.CssRules, which Veva scopes to that wrapper.
  • Change what the component gets: context.PropertyValues holds the component's property values by name, and the component is rendered after your hook has run.
  • Write markup around the component: add to builder before and after the call to next. Take sequence numbers from context.NextSequence().
  • Leave the component out: do not call next. Nothing after your hook runs, so the component is not rendered.

What a hook can read. Content is the piece of content and ComponentType its component. PageRequest gives the page, the website and the visitor, DataBindingContext the data binding values at that place on the page, and ParentRenderContext the context of the component around it. DesignMode is true in the page designer.

Hiding content. A hook that keeps content from visitors should still show it to editors, or they cannot tell what is hidden. In design mode, render it through HiddenContent.Render (Veva.Core.Application.BlazorComponents.Components): the designer then shows the content faded, under a notice with your title. Veva's own modifiers, such as Authorized Users and Date/Time activation, do the same.

Errors and speed. Invoke is not async, so it cannot await anything, and it runs on every render of the component: keep it quick. If it throws, the page shows the text "Render hook failed" and the exception's message where the component would be, to visitors too, so catch what you can handle. The hook is created from the service container, so constructor injection works.

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva