transformationweb-partskentico-11

Kentico 11: using variables inside text/xml transformations


I'm working on a carousel webpart with a text/xml transformation.
Simply trying to create a unique ID for each instance on the page.
Next I'd like to have the first item set to active with a CSS class.


For the section Webpart container:

{% uniqueId = string.FormatString("carousel-{0}", InstanceGuid.Substring(0,5)); #%}
<div id="{%uniqueId%}" class="carousel slide" data-ride="carousel">
    <div class="carousel-inner">
    □
    </div>
    <a class="carousel-control-prev" href="#{%uniqueId%}" role="button" data-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#{%uniqueId%}" role="button" data-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="sr-only">Next</span>
    </a>
</div>

enter image description here enter image description here

This seems to work at first but for some reason it generates a text-node in HTML. How can we do this without generating the variable output?


For a single carousel item in the Transformations section:

{% CssActive = IsFirst() ? "active" : string.Empty %}
<div class="carousel-item {%CssActive%}">
    <img src="{%Image%}" alt="" class="w-100 d-block" />
    <div class="carousel-caption d-none d-md-block">
        <h3>{%Title%}</h3>
        <div>{%Body%}</div>
    </div>
</div>

enter image description here enter image description here

This doesn't output anything? Is this even possible to use IsFirst() in a repeater?

Any help is much appreciated!


Solution

  • For the transformation you can use the following code:

    <div class="carousel-item{% if (DataItemIndex == 0) { " active" } #%}">
      <div class="card">
        <img src="{%Image%}" alt="" class="card-img-top" />
        <div class="card-body">
          <h3 class="card-title">{%Title%}</h3>
          <div class="card-text">{%Body%}</div>
        </div>
      </div>
    </div>
    

    Doc