Testing c::get in templates / loop

Hi, I’m looking how to test if a c::set exists before calling c::get.

Context: I want to set a noindex variable on certain domains via the config.php file, each domain have its own config.php, some have the noindex variable, some have a variable set on false and some have nothing.

No matter what, I can’t make it work. When I get rid of the “not a function” error by setting a default value, the if test is always successful, whatever the variable content or its existence.

config.php:

c::set('globalconfignoindex', 'false');

code in template:

  <?php
  if(c::get('globalconfignoindex', false) || (!$site->noIndex()->empty() && $site->noIndex()->isTrue()) || $page->isInvisible() || (!$page->noIndex()->empty() && $page->noIndex()->isTrue())): ?>
  <meta name="robots" content="noindex">
  <?php endif ?>

(also for the orther tests, is there a quick way to check if the variable is not empty and true in one pass?)

Thank you for your help, I can’t understand how the c::get is working.

The way you use it, you get the option from the config, but if it is not set, you set false as the default value. So you would have to check if c::get is set:

if(c::get('globalconfignoindex') ) { 
///
}
  • will return false if the value is not set or set to false
  • will return true if the value is set to true
1 Like

OK thanks.

I made multiple tests, and I have found the problem. I don’t know why but the error depends on where I place the config variable in the test:

$globalnoindex = c::get('globalconfignoindex',false);
  if(($site->noIndex()->isNotEmpty() && $site->noIndex()->isTrue()) || ($page->isHomePage() && $page->isInvisible()) || ($page->noIndex()->isNotEmpty() && $page->noIndex()->isTrue()) || $globalnoindex): ?>
  <meta name="robots" content="noindex">
  <?php endif ?>

variable at the end = no problem

  $globalnoindex = c::get('globalconfignoindex',false);
  if($globalnoindex || ($site->noIndex()->isNotEmpty() && $site->noIndex()->isTrue()) || ($page->isHomePage() && $page->isInvisible()) || ($page->noIndex()->isNotEmpty() && $page->noIndex()->isTrue())): ?>
  <meta name="robots" content="noindex">
  <?php endif ?>

Variable at the beginning = Error thrown with message “Call to undefined function ()”

The problem seems to come with $site->noIndex()->isNotEmpty() && $site->noIndex()->isTrue()) but I can’t find why.

Any idea ?

Autoreply : there was a non breakable space in my code. Made me waste 1 day on it :smiley: