Variables In Transformations

Delford Chaffin asked on August 7, 2014 17:36

Recently, I wrote a function to use in a transformation that returned a string. I then wanted to use that string in 3 different ways throughout the transformation, but I didn't want to call the function that many times as that would involve 3 times as many trips to the db as necessary.

I ended up with:

<script runat="server">
public string progressClass { get; set; }
protected override void OnDataBinding(EventArgs e)
{
  base.OnDataBinding(e);
  progressClass = MyFunctions.ProgressClass(Eval("NodeParentID"), Eval("NodeID"), CurrentUser.UserID);
}
</script>

<li class="lesson 
  <%# Eval("LessonType") %>
  <%# IfCompare(DataItemIndex, 0, "", "first") %>
  <%# IfCompare(DataItemIndex, DataRowView.DataView.Count-1, "", "last") %>
  <%# progressClass %>
">
  <%# IfCompare(progressClass, "disabled", "<a href=\""+GetDocumentUrl()+"\" class=\"wrap\">", "<div class=\"wrap\">") %>
    <span class="title"><%# Eval("Title") %></span>
    <span class="duration"><%# MyFunctions.FormatTime(Eval("Duration")) %></span>
  <%# IfCompare(progressClass, "disabled", "</a>", "</div>") %>
</li>

... and that worked, but it seems like there should be a simpler way to assign the result of a function to a variable that I can re-use. Is there a way I'm just missing or is this really the best way to do this?

Recent Answers


Brenden Kehren answered on August 7, 2014 19:30

I would do exactly what you have without a problem. You've created a special function that takes input parameters for that particular document being rendered (NodeParentID, NodeID and UserID) and assigning a variable for that instance of the document, then using that variable in the transformation. It will change for every document being rendered so there should be no worry about it not being efficient. It would be no different than modifying a value on row databound.

0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on August 7, 2014 22:38

Yeah, I know it works and everything and I'm not worried about the efficiency of the code as much as the coding experience. I would just like to be able to do something like:

<% progressClass = MyFunctions.ProgressClass(Eval("NodeParentID"), Eval("NodeID"), CurrentUser.UserID); %>
<li class="lesson 
  <%# Eval("LessonType") %>
  <%# IfCompare(DataItemIndex, 0, "", "first") %>
  <%# IfCompare(DataItemIndex, DataRowView.DataView.Count-1, "", "last") %>
  <%# progressClass %>
">
  <%# IfCompare(progressClass, "disabled", "<a href=\""+GetDocumentUrl()+"\" class=\"wrap\">", "<div class=\"wrap\">") %>
    <span class="title"><%# Eval("Title") %></span>
    <span class="duration"><%# MyFunctions.FormatTime(Eval("Duration")) %></span>
  <%# IfCompare(progressClass, "disabled", "</a>", "</div>") %>
</li>
0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 8, 2014 00:47

So if you modified your code slightly and did this, doesn't this work?

<%# var progressClass = MyFunctions.ProgressClass(Eval("NodeParentID"), Eval("NodeID"), CurrentUser.UserID); %>
0 votesVote for this answer Mark as a Correct answer

Delford Chaffin answered on August 8, 2014 17:10

I was fairly certain I had tried something similar already, but I tried your code and now I get:

[CMSDataProperties.LoadTransformation]: http://server/CMSVirtualFiles/Transformations/=vg=29bdbbce-8848-4354-a047-0fcaa0f72c29/einu.lesson/WorkPlanListItem.ascx(2): error CS1026: ) expected

... which doesn't make sense to me at all. I don't see a missing or extra paren.

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on September 15, 2014 11:45

Hello,

what about to use the RequestStock as it is described in this article?

http://devnet.kentico.com/articles/separating-pages-in-a-transformation

Best regards,
Jan Hermann

0 votesVote for this answer Mark as a Correct answer

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