Forum
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
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!
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:
or just
Thanks
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.