Displaying a variable

I am trying to display a variable. It is transformed in my config.ini file here: https://github.com/jgmac1106/gregkirbyv3/blob/master/site/config/config.php

and then trying to do this:
<?php if ($page->link()->isNotEmpty()) : ?>
<a class="<?= $page->posttype() ?>" href="<?= $page()->link() ?>"><?= $page->link()?></a>
<?php endif ?>

I thought I called the two variables correctly, but there must be something off in my PHP. Anyone have ideas.

You can only use ONE return-statement!

Delete

];
return [

and add there a comma ( “,”).

Here’s why: https://www.php.net/manual/en/function.return.php

<?php
return [
    'debug' => true,
   
     'panel' =>[
        'install' => true
      ],
    'posttype' => [
        'reply'       => 'u-in-reply-to',
        'bookmark' => 'u-bookmark-of',
        'following'  => 'u-follow-of',
    ]
];

Thank you @anon77445132 and @texnixe I was trying to infer the syntax for functions.

More surprised I did not mess up my template.

<?php if ($page->link()->isNotEmpty()) : ?>
<a class="<?= $page->posttype() ?>" href="<?= $page()->link() ?>"><?= $page->link()?></a>
<?php endif ?>

However, where is the link between your config and this code snippet? You are not calling the config options anywhere?

In $page()->link() $page should be without parentheses, anyway.

1 Like

Thank you I was just trying to recreate the syntax that called the text variable.

I will investigate how to call the config option. I just that happened based on the variable name posttype.

You can call config options with the option() helper.

E.g. to get the complete array:

$postOptions = option('posttype');
dump($postOptions);

Or a single option:

dump(option('posttype.reply'));

I must be over thinking this.

Here is my current ntoe template

<?php snippet('header') ?>
<?php $postOptions = option('posttype'); ?>
<main>
  <article class="h-entry note">
    <header class="note-header intro">
      <time class="dt-published note-date"><?= $page->date()->toDate('Y F d h:i a') ?></time>
      <?php if ($page->tags()->isNotEmpty()) : ?>
      <p class="note-tags tags"><?= $page->tags() ?></p>
      <?php endif ?>
      <a class="u-author" href="/"></a>
      <?php if ($page->link()->isNotEmpty()) : ?>
<a class="<?= $page->postOptions() ?>" href="<?= $page->link() ?>"><?= $page->link()?></a>

<?php endif ?>
    </header>

  <div class="p-content note-text text">
      <?= $page->text()->kt() ?>
    </div>
  </article>
</main>

<?php snippet('footer') ?>

I am assuming I am making a new variable postOptions which equals the posttype option selected in the panel…but I still can’t get a variable to display,

Maybe it would be easier to noteven try to transform the options selected in the panel?

Ok, let’s go through this step by step:

In your note.yml blueprint, you have defined a field posttype with three options:

  • reply
  • bookmark
  • following

I guess what you want to achieve is to print what you have defined in your config instead, so if the user selected reply you want to print u-in-reply-to.

The easiest way to achieve that, would be to use these options in your blueprint, so that the desired value gets saved in the content file:

postype:
  label: Type
  type: select 
  options:
    u-in-reply-to: reply
    u-bookmark-of: bookmark
    u-follow-of: following

This way, you would show what is after the colon to the user, while saving the key in the content file. You could then just print the value in your template:

<?= $page->posttype() ?>

The second option is to leave your field as it currently is and then to get the value you want to print from the config.

// fetch the array of options from the config
$postOptions = option('posttypes');
// get the value from the array index that corresponds to the value stored in the content file
$postOption = $postOptions[$page->posttype()->value()];
echo $postOption

So in case reply is the post type stored in the file, $postOption = $postOptions[$page->posttype()->value()] would translate to

$postOptions['reply']

Which would then output: u-in-reply-to.

Unless you have a good reason not to, I’d go with the first option.

1 Like

ohh thank you, I had no idea I could transfer the variable directly in the blueprint.

More importantly thanks for taking the time to write such a detailed support message. I know it is above and beyond.

Good luck at the graduation this weekend!!!