2
votes

A custom page type is storing a multi choice form control property value as a pipe delimited string such as "foo|bar".

Within a page type ASCX transformation I'm able to successfully retrieve and display the foo|bar property value using:

<div><%# Eval("Foobar") %></div>

The goal is to split this string value on the pipe | character and output each value, but I'm unable to achieve this using the page type transformation ASCX syntax.

Trying the following gives me error "error CS0230: Type and identifier are both required in a foreach statement":

<ul>
  <% foreach (thing in Eval<string>("Foobar").Split('|')) { %>
    <li><%= thing %></li>
  <% } %>
</ul>

<ul class="list-unstyled">
  <% 
    things = Eval<string>("Foobar").Split('|');
    foreach (thing in things) { 
  %>
    <li><%= topic %></li>
  <% } %>
</ul>

Trying set type to string or var causes a system wide exception and prevents the site from loading:

<ul>
  <% foreach (string thing in Eval<string>("Foobar").Split('|')) { %>
    <li><%= thing %></li>
  <% } %>
</ul>

<ul>
  <% foreach (var thing in Eval<string>("Foobar").Split('|')) { %>
    <li><%= thing %></li>
  <% } %>
</ul>

<ul class="list-unstyled">
  <% 
    var things = Eval<string>("Foobar").Split('|');
    foreach (var thing in things) { 
  %>
    <li><%= thing %></li>
  <% } %>
</ul>

Trying a for loop and targeting the string[] topics results in an error of "CS0103: The name 'topics' does not exist in the current context":

<ul class="list-unstyled">
  <% 
    things = Eval<string>("Foobar").Split('|');
    for(int i = 0; i < things.Length; i++) { 
  %>
    <li><%= things[i] %></li>
  <% } %>
</ul>

How can I achieve retrieving the value for the page type/document, splitting on the pipe character, then display each resulting string[] array value? Should I be using a different type of transformation?

Thank you for any help you can provide!

2

2 Answers

4
votes

The easiest way to accomplish this is to use "Text/XML" Transformation Type rather than ASCX, and use K# Kentico Macro syntax to loop through your delimited string.

https://docs.kentico.com/k10/macro-expressions/macro-syntax

You should then be able to do something similar to your first example. Kentico's foreach example from the documentation is a bit simpler:

{% z = ""; foreach (x in "hello") {z += x.toupper()}; z %} (returns "HELLO")

but you should be able to modify it to meet your needs.

Here's another example from the documentation using nested loops and some HTML:

{%
orders = ECommerceContext.CurrentCustomer.AllOrders;
if (orders.Count > 0) {
    result = "<ul>";
    foreach (order in orders) {
        foreach (item in order.OrderItems)
            { result += "<li>" + item.OrderItemSKUName + "</li>" }
    };
    return result + "</ul>";
}%}
1
votes

Although the below method is not recommended but it does comes in handy many a times. Write your transformation as below

<ul>
  <%#GenerateLIs(Eval<string>("Foobar"))%>
</ul>

<script runat="server">
public string GenerateLIs(string foobarValue)
{

string liList = string.Empty;
string[] foobarSplit = "foobar".Split('|');
    foreach (string item in foobarSplit)
        liList += "<li>" + item + "</li>";

return liList;
}
</script>

The above code can help resolve your issue here but there are more efficient ways to do it for sure. You can also view Kentico 9 form macros and different notification recipient based on dropdown value for reference.