View more context

 

Joe

I've got a page with children, I want to display an element if a condition is true of any of the children. I cant figure out the best way to do this in the template. For example: <% loop $Children %><% if $SalePrice %> THERE ARE ITEMS IN THE SALE<% end_if %><% end_loop %> How can I make it so that the message only appears once, rather than for every time the if is true for every page?

Joe

I’d probably have a separate controller method that:

• gets children • filters for any children with your condition true • checks ->count() to make sure you have children with that condition • returns a boolean indicating children with your condition

so you’d end up with something like:

  1. <% if $showSalePrice %>
  2. THERE ARE ITEMS IN THE SALE
  3. <% end_if %>
  4. <% loop $Children %>
  5. ...
  6. <% end_loop %>
😁 (1)
Joe
  1. public function showSalePrice() {
  2. $children = $this->Children()
  3. // dummy filter since I don't know your actual logic
  4. ->filter('SalePrice', true);
  5.  
  6. return $children->count() > 0;
  7. }
markcl

kind of like php artisan tinker for Laravel?

null

Run like this from your project directory: vendor/bin/sake interactive