foreach loop in transformation

Novice User asked on July 3, 2018 16:10

I want to link tags inside my smart search transformation. This line is returning only text of the tags

{% count=0; foreach(x in GetSearchValue("DocumentTags").Split(",", true)){count += 1;GetSearchValue("DocumentTags").Split(",")[count-1];}%}

how do I link it with "a href" tag as its just one line and I lost the variable if i split the line.

Correct Answer

Brenden Kehren answered on July 3, 2018 18:13

Your macro should look something like this:

{%
    foreach (x in GetSearchItem("DocumentTags).Split(","))
    { "<a href='your link here'>" + x + "</a>";  }
|(identity)GlobalAdministrator%}
1 votesVote for this answer Unmark Correct answer

Recent Answers


Novice User answered on July 3, 2018 18:41 (last edited on December 10, 2019 02:31)

X doest not return any thing. But I modified my code as

{% z=0; foreach(x in GetSearchValue("DocumentTags").Split(",", true)){z += 1;"<a href='" + GetSearchValue("DocumentTags").Split(",")[z-1] + "'>" + GetSearchValue("DocumentTags").Split(",")[z-1]+ "</a>";}|(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 3, 2018 19:05 (last edited on December 10, 2019 02:31)

I think you are mixing transformation types here:

  1. if you are using ACSX transformation <%# GetSearchValue("DocumentTags")%> will work
  2. in text/xml {%GetSearchValue("DocumentTags")%} - will not work, but {%DocumentTags%} will

If you are using text/xml, you do this (assuming DocumentTags = 'abc, cde, xyz'):

{%
arr = DocumentTags.split(",").Flatten();  
arr.Transform("<a href='/someulr?tag={#arr[DataItemIndex]#}'>{#arr[DataItemIndex]#}</a>");
|(identity)GlobalAdministrator%}
1 votesVote for this answer Mark as a Correct answer

Novice User answered on July 3, 2018 19:44 (last edited on December 10, 2019 02:31)

if i dont use GetSearchValue("DocumentTags"), it displays nothing and

{%
arr = DocumentTags.split(",").Flatten();  arr.Transform("<a href='/someulr?tag={#arr[DataItemIndex]#}'>#arr[DataItemIndex]#}</a>");
|(identity)GlobalAdministrator%}

it works when its on one line and displays on first tags three times. what I am missing ?

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on July 3, 2018 19:52 (last edited on December 10, 2019 02:31)

You have mistake in the macro >#arr[DataItemIndex]#}< should be >{#arr[DataItemIndex]#}<

try this:

{%
test = "abc,cde,xyz";
arr = test.split(",").Flatten();  
arr.Transform("<a href='/someulr?tag={#arr[DataItemIndex]#}'>{#arr[DataItemIndex]#}</a><br>");
|(identity)GlobalAdministrator%}

If it work replace test with GetSearchValue or GetSearchItem or whatever works for you.

1 votesVote for this answer Mark as a Correct answer

Zbyněk Nedoma answered on August 2, 2018 12:02 (last edited on August 2, 2018 12:28)

Update: Moved to separate question, because this is already answered

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.