This is similar to this XSLT question, but ultimately for DWT.
We can get a keyword's key via a C# TBB.
ItemFields fields = new ItemFields(component.Metadata, component.Schema);
KeywordField keywordField = fields["state"] as KeywordField;
String stateValue = package.EvaluateExpression("Component.Metadata.state");
package.PushItem("statekey", package.CreateStringItem(ContentType.Text, keywordField.Value.Key));
In DWT, I sometimes want the key
of a selected keyword in a component.
Is adding and getting these from the package the correct approach?
@@Component.Metadata.State@@
gets me the value. Referencing the key directly from DWT would be great, but I haven't seen anything to suggest DWT exposes it.
As another alternative to John's answer. How about taking a similar approach to Will's "Get Component Template Uris" and write a TBB that iterates over a category's keywords and writes them out into the package with the desired output value? You'd then be able to use these values directly from DWT with a "double-evaluation" like:
@@Keyword${Component.Metadata.state}Value@@