Forum

Thread tagged as: Question

Perch_layout PHP scope

Hi,

I am trying to set some variables to be used within templates. Here is my code:

In my master page:

<php perch_layout('global-header'); ?>
<title><?php echo $site_name ?></title>

Contents of global-header:

<?php
          $site_name     = "The Victoria Pub, Hitchin";
          PerchSystem::set_var('site_name',$site_name);
          perch_page_attributes(array( 'template' => 'default.html' ));
    ?>

I'm getting no output from trying to echo out $site_name. I think this is related to PHP's scope. Does perch_layout() affect the scope?

Thanks

Adam Menczykowski

Adam Menczykowski 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, layouts have their own variable scope.

Can I call them by using global? What is best practice with variables to use site-wide, i.e. in templates and in php layouts? Thanks!

Drew McLellan

Drew McLellan 2638 points
Perch Support

You could probably use global, but I wouldn't.

You could put them in a PHP file and include them from somewhere like your config.php.

Would you define variables like this:

define("PERCH_DB_PREFIX", "perch2_");

or just

$variable = "value";

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

The first example is a constant and not a variable. As the name suggests, a constant doesn't change. So it depends on what you're doing.