Hi - I have seen how to show text from the template based on a yes/no toggle in the blueprint.
<?php
if($page->fp_status()->bool()) {
echo "Offered for sale";
} else {
echo "Sold";
}
?>
However, I also want to display fields and other html as well… e.g.
Offered for sale (text as above code)
Price: £10,000 (field from blueprint)
ENQUIRE (button html)
Could someone offer help with correct syntax. The code was for Kirby 2 of course, but it works in Kirby 3 - but I am pretty sure there is a ‘newer/better’ way to do this.
Thank you!
Try…
<?php if($page->fp_status()->bool()): ?>
<!-- your code if yes here -->
<p>Offered for Sale</p>
<?php else: ?>
<!-- your code if no here -->
<p>Sold</p>
<?php endif ?>
Thank you!!
I tried the following and works perfectly!
<?php if($page->fp_status()->bool()): ?>
<h5>£ <?= $page->price() ?></h5>
<div class="button">
<a href="#" >ENQUIRE</a>
</div>
<?php else: ?>
<h5>SOLD</h5>
<?php endif ?>
Fantastic, thank you!
And now my next challenge, how to display the price as £10,000 and not £10000
!!
Is there a way to do that?
Kirby will clean the field value up if it ends in zeros. You need fix it with PHP if you really need them…
Something like…
$price = number_format((float)$page->yourfieldname()->value(), 4, '.', '')
echo $price;
Change the dot to a comma if you prefer.
You might also need to set the step option on the blueprint field.
Thank you! I will try this out.
Very much appreciated.
And here we go!
<?php
$price = number_format((float)$page->price()->value(), 0, '.', ',') ?>
£<?= $price ?>
this produces
£10,000
Thank you!
A bit shorter using toFloat()
:
<?php $price = number_format($page->price()->toFloat(), 0, '.', ',') ?>