TokenManager: Dynamic Tokens

This is a post regarding a specific type of token used in the TokenManager, the dynamic token.

What does it do?

Dynamic tokens are for when you need to inject dynamically acquired content into rich text, such as user session data.

How is set up?

A dynamic token collection is created.  When you examine the fields you’ll see a field for a Type.

contenttree.png

field.png

Then you must implement a token collection. The most basic implementation will follow this model.  Essentially data is to be exposed through anonymous methods.

	public class SessionTokenCollection : TokenCollection<IToken>
	{
		public SessionTokenCollection(Item backingItem)
			: base(backingItem)
		{
			SetupTokenValues();
		}

		public void SetupTokenValues()
		{
			AddOrUpdateToken(new DynamicToken("Email", () =>
			{
				return "MyEmail@email.email";
			}));
			AddOrUpdateToken(new DynamicToken("Due Date", () =>
			{
				return "12/13/2016";
			}));
			AddOrUpdateToken(new DynamicToken("Days Until Due Date", () =>
			{
				return "12";
			}));
			AddOrUpdateToken(new DynamicToken("Day Of Month Of Autopay", () =>
			{
				return "13th";
			}));
			AddOrUpdateToken(new DynamicToken("Autopay Status", () =>
			{
				return "On";
			}));
		}

		public override string GetCollectionLabel()
		{
			return "User Session Tokens";
		}

		public override IToken InitiateToken(string token)
		{
			throw new NotImplementedException();
		}

		public override void ResetTokenCache()
		{
			throw new NotImplementedException();
		}
	}

Customization options

Through the use of token selector filtration you can expose specific dynamic tokens to only a branch of your website, or to only a specific data template.

Sample Usage

You could set up a name and email token to read from the users session variable to set up a paragraph that reads like this:
“Hello NAME welcome back, we have you set up to send automatic payments, confirmation of these payments will be sent to EMAIL”