TokenManager View Tokens

Likely fitting in the wheelhouse of most Sitecore developers is building a view model and passing it to a view to be rendered.  That’s what the ViewAutoToken class achieves.  The idea being that you collect data from the content authors at the time of token insertion, then use that data to build a view model and pass that model to a view cshtml.

Unique Aspects

When implementing a new view token you should extend the base class of ViewAutoToken.  This is very similar to an AutoToken except instead of implementing a method to render the raw html outputted by the token you define two methods, one to generate the view model and one to determine the view.

		public override object GetModel(TokenDataCollection extraData)
		{
			return extraData;
		}

		public override string GetViewPath(TokenDataCollection extraData)
		{
			return "/views/myToken.cshtml";
		}

AutoToken Features

All features from AutoTokens are available for the AutoViewTokens as well.  Such as gathering data from the content authors when applied to be used during rendering and filtering where the token may be used.

As usual with AutoTokens, you need only implement it in a loaded assembly and TokenManager will pick it up and wire it for use in RTEs.

Complete Example

	public class tokentest : ViewAutoToken
	{
		//Make sure you have a parameterless constructor.
		public tokentest() : base("test", "people/16x16/cubes_blue.png", "terkan")
		{
		}
		//This will add a button to the RTE.
		public override TokenButton TokenButton()
		{
			return new Data.TokenExtensions.TokenButton("test", "people/16x16/cubes_blue.png", 1000);
		}
		//These are the different fields that will be collected by the content authors at the time of insertion.
		public override IEnumerable<ITokenData> ExtraData()
		{
			yield return new GeneralLinkTokenData("LINK", "link", true);
			yield return new DroplistTokenData("Droplist", "droplist", true, new []
			{
				new KeyValuePair<string, string>("Text Label", "Value Passed"),
				new KeyValuePair<string, string>("Blue", "blue"),
			});
			yield return new BooleanTokenData("bool", "bool");
			yield return new IdTokenData("id", "id", true);
			yield return new IntegerTokenData("int", "int", true);
		}
		//These are the templates where the token may be used.
		public override IEnumerable<ID> ValidTemplates() {
			yield return new ID("{78816AC8-4FD7-43C4-A899-17829B4F3B72}");
		}
		//These are the root nodes that make a subtree where the token may be used.
		public override IEnumerable<ID> ValidParents()
		{
			yield return new ID("{A1E1342E-6836-4E20-A2C4-B1A38444B079}");
		}
		//Use the data gathered by the content author to assemble a view model.
		public override object GetModel(TokenDataCollection extraData)
		{
			return extraData;
		}
		//Use the data gathered by the content authors to define a path to the view cshtml.
		public override string GetViewPath(TokenDataCollection extraData)
		{
			return "/views/MyToken.cshtml";
		}
	}

And my view found at [webroot]/views/MyToken.cshtml

@using TokenManager.Data.TokenDataTypes.Support
@model TokenDataCollection
<div><strong>@Model.GetLink("link").Href</strong></div>
<div><strong>@Model.GetString("droplist")</strong></div>
<div><strong>@Model.GetBoolean("bool")</strong></div>
<div><strong>@Model.GetId("id")</strong></div>
<div><strong>@Model.GetInt("int")</strong></div>