Get variable from specific blueprint section

Hi,
I’m getting back to a previous question I had. My question was “how to get a color variable and display it depending on the month name”. So this worked very well

<?php $monthC = str::ascii($month->name());
      $color = $site->$monthC(); 
      echo $color 
      ?>

But this code display the same color for each month (e.g. January 2017, Januray 2018, January 2019 have the same color). I try to get a different variable name for each of my year table. My idea was to point on the section from the bueprint to select the right variable :

  couleurssection:   <---My first section
    label: Couleurs 2017
    type: headline
  janvier:
    label: Janvier
    type:  color
  fevrier:
    label: Février
    type:  color
  [.....etc.....]
  couleurssection2: <---My second section
    label: Couleurs 2018
    type: headline
  janvier2:
    label: Janvier
    type:  color
  fevrier2:
    label: Février
    type:  color
....

So, how can I select my variable depending on the section from blueprint, something like

<?php $monthC = str::ascii($month->name());
      $colorone = $site->couleurssection()->color();   <---My first section
      $colorone = $site->$monthC(); 
      echo $colorone 
      ?>

<?php $monthC = str::ascii($month->name());
      $colortwo = $site->couleurssection2()->color();   <---My second section
      $colortwo = $site->$monthC(); 
      echo $colortwo 
      ?>

This blueprint looks like it won’t possibly work. You can’t have repeated fields with the same name? What sort of option is “hero”?

Yes, you right, I have not copied well my code, sorry. “Hero” was useless, and each field has a unique name. I changed it above…

If they all have different names, you might as well refer to those fields by their names, no need for the sections at all. However, I’d probably use structure fields, anyway.

One question, though: For how many years do you want to add colors? What happens in two years’ time? Will you add more fields?

I want to add colors for three years. So I already have 36 different color fields.
The problems I see with structure field are

  1. it makes the back office page quite unreadable
  2. date picker return a specific date, we can’t choose a year value (maybe but probably not the propper way)

I want to make the most simple solution, not the most “user-friendly” one

Then I’d call these fields

janvier2017

etc. Then you could access them later like this

$year = '2017';
$field = $monthC.$year;
$color = $site->$field();

This seems to be exacly what I’m looking for. I renamed every fields, However I get

Notice: Undefined property: Site::$janvier in /Applications/MAMP/htdocs/MyProject/site/snippets/calendar.php on line 42
Fatal error: Uncaught Error: Call to undefined function 2017() in...

with

<?php $monthC = str::ascii($month->name());
                      $annee = '2017';
                      $color = $site->$monthC.$annee();             
                ?>

Maybe we have to change it a little bit:

<?php 
$monthC = str::ascii($month->name());
$annee = '2017';
$field = $monthC.$annee;
//let's check if this gives us what we think
dump($field);
$color = $site->$field();             
?>
1 Like

And…the dream comes true. This little code put me a pre tag but I can hide it with CSS.
Many thanks to you Texnixe !

You should remove the dump() stuff again after use…, not hide it via CSS :wink:.

1 Like