Nathan Elg
Nathan Elg

Reputation: 187

Kentico 10 Transformation Reference

I am trying to dynamically display data if it is not empty and set the header to empty if there is no data for that item.

Below is my transformation code. I am trying to hide any h5 headings that don't have data.

For example, if my CurrentDocument.EVAl("Client") comes back empty I would like to hide the entire class "client-heading". I assume it has something to do with !IfEmpty.

<div class="left-data small-6 medium-3 large-3 columns">
    <div class="location-heading">
      <h5 class="data-heading">Location:</h5>
      <h5>{% CurrentDocument.GetValue("City") #%}, {% CurrentDocument.GetValue("State") #%}</h5>
    </div>
    <div class="client-heading">
      <h5 class="data-heading">Client:</h5>
      <h5>{% CurrentDocument.EVAL("Client") #%}</h5>
    </div>
    <div class="year-heading">
      <h5 class="data-heading">Year Completed:</h5>
      <h5>{% CurrentDocument.GetValue("yearCompleted") #%}</h5>
    </div>

Upvotes: 1

Views: 237

Answers (2)

Trevor F
Trevor F

Reputation: 1437

First quick question i would ask is you are referencing the "CurrentDocument" which is NOT the transformed object, it is the actual current document. So if you want to get the City value of the transformed object, you should use {% City %} instead of {% CurrentDocument.GetValue("City") %}

As for testing if something is null or empty, if it's a string, simply use

{% string.IsNullOrWhiteSpace(TheValue) ? "It's null" : "It's not null" %}

or

{% if(string.IsNullOrWhiteSpace(TheValue)) { %}
  It's null
{% } %}
{% if(!string.IsNullOrWhiteSpace(TheValue)) { %}
  It's not null
{% } %}

Or specificaly to your transformation:

<div class="left-data small-6 medium-3 large-3 columns">
{% if(!string.isnullorwhitespace(CurrentDocument.GetValue("City")) { %}
    <div class="location-heading">
      <h5 class="data-heading">Location:</h5>
      <h5>{% CurrentDocument.GetValue("City") #%}
{% !string.isnullorwhitespace(CurrentDocument.GetValue("State")) ? ","+CurrentDocument.GetValue("State") : "" #%}</h5>
    </div>
{% } %}
{% if(!string.isnullorwhitespace(CurrentDocument.GetValue("Client")) { %}
    <div class="client-heading">
      <h5 class="data-heading">Client:</h5>
      <h5>{% CurrentDocument.GetValue("Client") #%}</h5>
    </div>
{% } %}
{% if(!string.isnullorwhitespace(CurrentDocument.GetValue("yearCompleted")) { %}
    <div class="year-heading">
      <h5 class="data-heading">Year Completed:</h5>
      <h5>{% CurrentDocument.GetValue("yearCompleted") #%}</h5>
    </div>
{% } %}

Keep in mind another alternative is to just add a hidden CSS if the value is null or white space on the div, so it renders HTML but the user doesn't see it.

Upvotes: 0

Brenden Kehren
Brenden Kehren

Reputation: 6117

It would look something like this:

{% if(Client != "") { %}  
<div class="client-heading">
    <h5 class="data-heading">Client:</h5>
    <h5>{% Client %}</h5>
</div>
{% } %}

Also know your transformation is combining both ASCX and Macro transformation methods. Might want to make sure you clean this up.

Upvotes: 2

Related Questions