nickwesselman
nickwesselman

Reputation: 6890

Get data item value dynamically in Kentico Text/XML Transformation

I have a custom macro method that returns a string array of field names whose values I want to output in a Text/XML transformation. The transformation is being used as the Item Template in a Datalist. I cannot figure out though how to dynamically get the value from the current data item. It seems that DataItem does not resolve -- nothing outputs from the GetValue call below. Using CurrentDocument would obviously resolve the page instead of the data item.

<tr>
  <td>{% SKUNumber %}</td>
  {% foreach (spec in Util.GetSpecFieldsForPageType(CurrentDocument.NodeClass, "RelatedModelsTable")) {  %}
    <td class="hidden-xs">{% GetValue(DataItem, spec) %} </td>
  {% } #%}
</tr>

Is there any way to access the current data item in a Text/XML transformation? Or do I need to switch to an ASCX transform?

Upvotes: 1

Views: 1039

Answers (2)

Kristian Bortnik
Kristian Bortnik

Reputation: 838

Assuming you're using Kentico v10, you're looking for the Object macro.

Using an indexer, you can specify a column to retrieve data from:

{% Object[columnName] %}

In your case, it would be:

<tr>
  <td>{% SKUNumber %}</td>
  {% foreach (spec in Util.GetSpecFieldsForPageType(CurrentDocument.NodeClass, "RelatedModelsTable")) { %}
    <td class="hidden-xs">{% Object[spec] %}</td>
  {% } #%}
</tr>

Here's a generic example, for those who need an example without any custom macros. This will dynamically render DocumentID, DocumentName and NodeAliasPath, specifying the columns in an array, as opposed to hardcoding them.

<tr>
  {% columns = "DocumentID,DocumentName,NodeAliasPath"; foreach (column in columns.Split(",")) {  %}
  <td>{% Object[column] %} </td>
  {% } #%}
</tr>


EDIT:

As Kentico v9 does not have the Object macro for transformations, one way to dynamically resolve a value of the repeated item is to use Util.ResolveMacroExpession, and supply the column name as the parameter.

The repeated item fields are registered as named sources, so there is no way to access them with an indexer or macro method.

Here's the same example from the question author, modified to work in Kentico v9:

<tr>
  <td>{% SKUNumber %}</td>
  {% foreach (spec in Util.GetSpecFieldsForPageType(CurrentDocument.NodeClass, "RelatedModelsTable")) { %}
    <td class="hidden-xs">{% Util.ResolveMacroExpression(spec) %}</td>
  {% } #%}
</tr>

Upvotes: 2

Peter Mogilnitski
Peter Mogilnitski

Reputation: 998

You have to give us the scenario to get the right answer

If your {% Util.GetSpecFieldsForPageType(CurrentDocument.NodeClass, "RelatedModelsTable")%} returns IEnumbrable you can use ApplyTransformation

Upvotes: 0

Related Questions