Echo blueprint fields label title

Hello Kirby-ers,

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:

year:
label: Year/Jaar
type:  text

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:

$page->year()->isNotEmpty()

As for language-specific labels, you could use this in your blueprint:

year:
  label: 
    en: Year
    nl: Jaar
  type:  text

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.

With 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).

1 Like

The name of the field is not the same as the label, though.
In his case the key is client, but the label is Year/Jaar.
I think what @lsg wants is to display Year/Jaar, so unfortunately the key isn’t of any help here.

1 Like

O, then it is the same as to get the label of an options of a Radiobuttons field.

That’s yet another story.

I have added an issue: Add $field->label to get the label of a panel field

As for me, the code Texnixe wrote works perfectly! That is exactly what I meant…
Thanks!

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:

c::set('year', 'Year/Jaar');

Then in your template, you can retrieve the desired value:

<?php echo c::get($page->year()->key()); ?>

Or as an array of values:

  c::set(array(
   '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.

en.php

<?php
  l::set('year', 'Year');
  l::set('text', 'Text');
?>

nl.php

<?php
  l::set('year', 'Jaar');
  l::set('text', 'Tekst');
?>

And in your template:

<?php echo l::get($page->year()->key()); ?>
3 Likes

You can use this plug-in for this kind of problems.

This helped me a lot.

Cheers

An alternative is https://github.com/jenstornell/kirby-blueprint-reader

1 Like