Search code examples
modxmodx-evolution

If/Else in Modx


I'm trying to wrap in my template a block in conditionals:

<div class="widget widget_categories trip_widget">
    <div class="kd-widget-title"><h4>Book online</h4></div>
    <div class="trip-book-button">
        <a href="[(site_url)]book-online.html/#id=[*trip-slug*]">Book now online</a>        
    </div>
</div>

I need to show that block only if [*trip-slug*] is present.

I've read through the forum and the documentation but I can't seem to get the syntax right.

What am I missing?


Solution

  • In Evolution you need the PHx plugin (https://github.com/Temus/PHx) to process resource or template variable tags. With that plugin you could use the following code:

    [*trip-slug:ne=`
      <div class="widget widget_categories trip_widget">
        <div class="kd-widget-title"><h4>Book online</h4></div>
        <div class="trip-book-button">
          <a href="[(site_url)]book-online.html/#id=[*trip-slug*]">Book now online</a>        
        </div>
      </div>
    `*]
    

    Placeholder tags inside Ditto are parsed with a (limited) PHx parser and you could use the following inside a Ditto template without the plugin:

    [+trip-slug:ne=`
      <div class="widget widget_categories trip_widget">
        <div class="kd-widget-title"><h4>Book online</h4></div>
        <div class="trip-book-button">
          <a href="[(site_url)]book-online.html/#id=[+trip-slug+]">Book now online</a>        
        </div>
      </div>
    `+]
    

    In both cases you could also work with the Evolution core if Snippet.

    [[if? &is=`[*trip-slug*]:not_empty` &then=`
      <div class="widget widget_categories trip_widget">
        <div class="kd-widget-title"><h4>Book online</h4></div>
        <div class="trip-book-button">
          <a href="[(site_url)]book-online.html/#id=[*trip-slug*]">Book now online</a>        
        </div>
      </div>
    `]]
    

    Replace the [* *] with [+ +] inside of Ditto template chunks.