July 8, 2015, 2:04pm
I have a set of custom fields in my blueprints. Is there a way to display the title/label of a blueprint field conditionally?
Suppose I have:
How can I get to show ‘Year/Jaar’? Something like <?php echo $page->year()->label() ?>, does that exist? Obviously I can use html to put it in my template, but the thing is I would like to show it conditionally only when the field is actually filled in.
Can somebody help out?
You can’t get the label of a field, because it only exists in the panel, not in the text file. However, you could still do it conditionally (at least if I got you right)
<?php if($page->year() != "") :?>
<span>Year/Jaar</span><span><?php echo $page->year() ?></span>
<?php endif ?>
BTW: Is that a multi-lingual site? Then you could use language specific labels in the panel and also use language variables for the template output.
Kirby has a field method to check if it’s not empty: In this case it would be:
As for language-specific labels, you could use this in your blueprint:
But you would need to enable multi-language support in order to use it, as it will only show the according field label when the language is switched in the panel.
Kirky docs: Advanced > Custom field methods > The field object:
In a snippet or a template I think we can get the “the name of the field” (label) with the code
$field->key (not yet tested).
The name of the field is not the same as the label, though.
In his case the key is
client, but the label is
I think what @lsg wants is to display Year/Jaar, so unfortunately the key isn’t of any help here.
O, then it is the same as to get the label of an options of a Radiobuttons field.
That’s yet another story.
July 8, 2015, 4:02pm
As for me, the code Texnixe wrote works perfectly! That is exactly what I meant…
Concerning the multilingual aspect, we have not yet decided which direction to take, but thank you for informing me on this possibility, I was not aware of that option!
A work-around for getting those labels is mapping the desired key-value pairs in your config, like so:
Then in your template, you can retrieve the desired value:
<?php echo c::get($page->year()->key()); ?>
Or as an array of values:
'year' => 'Year',
'title' => 'Title',
'text' => 'Text'
This is also useful if you want to get the “readable” strings for your radio button/select/checkbox values.
And if you are on a multi-language site, you can define those key=>value pairs in your language files.
And in your template:
<?php echo l::get($page->year()->key()); ?>
June 9, 2017, 11:30pm
You can use this plug-in for this kind of problems.
This helped me a lot.