Str::ucfirst weird behaviour!

Str::ucfirst does not work and I don’t know why!

I retrieved a string from a snippet to feed Str::ucfirst:
$support = Str::ucfirst(snippet('oeuvre-support',[ 'support' => $page->support()],true));

Display is wrong as the first character is lowercase:

I then replace the call to snippet by harcoded string:
$support = Str::ucfirst('bois');

Display is OK as the first character is uppercase:

Echoing the result from the snippet show no extra character!!!

This is weird!

What’s in your snippet, can’t reproduce this.


$sortie = "";

switch ($support):
    case "AR_01":
      $sortie = "carton plume";
    case "CA_01":
    case "CL_01":
    case "CL_02":
    case "CL_03":
    case "CL_04":
    case "FB_01":
    case "FB_01a":
    case "FB_02":
    case "FB_03":
    case "FB_04":
    case "FB_06":
    case "FB_07":
    case "MO_01":
    case "MO_03":
    case "RP_01":
    case "NC_01":
    case "PW_02":
      $sortie = "papier";

    case 'toile':
      $sortie = "toile coton";
    // BOIS !
    case "B_01":   
    case "CP_10":
    case "CP_15":
    case "CPP_15":
    case "MDF_10":        
        $sortie = "bois";
    default: // Programmation Défensive
        $sortie = "????";


<?= $sortie ?>

I guess that this will cause a line break to be the first character in your returned value.

1 Like

Well done, bravo!

Thank you very much!

I think it would make much more sense to use a function or better still a custom field method instead of a snippet for this.

1 Like

Yes, you’re absolutely right!

This is an old portion of code, in my very first steps in Kirby!
I already use a custom field to store one piece of information and from this information I can compute others.

In this case the basic information is a reference to the support of a painting. From this reference I deduce the information I need. If it is wood or paper for example. The use of snippet is thus direct and quickly set up.
If I want to go further and according to my understanding of Kirby the best is to use a plugin to centralize this information.

By the way, you’ve indicated the use of a function but where do you put it?

This information should probably be stored either in a content file or some other file (even an array of values in the config would be an option).

OK. But using a content file leads to huge development only to store small and non important amount of data.

Then I’d store in array in config:

'support' => [
  'AR_01' => 'carton plume',
  'PW_02' => 'papier'

Then create a method that returns the value for the shortcut, using

$supports = option('support');
$sortie = $supports[$page->support()->value()] ?? null;

I think this will be more performant than your switch thingy as well. Also if you actually store this information in your function

Thanks for the example. I’ll look into it!