1

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?

2 Answers 2

2

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>
2
  • 1
    I have updated my answer with a solution for Kentico 9 Jan 26, 2017 at 10:26
  • Fixed typo in ResolveMacroExpression but otherwise that was exactly what I needed. Many thanks. Jan 26, 2017 at 14:01
0

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

2
  • It's outputting an array of column/fields names that I want to write out from the data source item. Jan 26, 2017 at 3:05
  • ApplyTransfornation doesn't help me, unless I'm missing something. I just want to output the value of the field. Jan 26, 2017 at 3:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.