Forum

Thread tagged as: Question

Global includes in layout files

Hi,

On my index.php page, I have included a vars.php file that holds php variables, but my layout files on index.php (global.header and global.footer) can't access the variables.

Do I have to include the vars.php file on all the layout templates as well? Is there a better way to do all of this?

Thanks,

-Rob

Robert Yedlin

Robert Yedlin 1 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can pass them into scope using layout variables

https://docs.grabaperch.com/docs/layouts/variables/

Ok, thanks Drew.

Is there a way to declare an array as a layout variable?

Like:

<?php 

$snippets['phone'] = '123-456-7890';

perch_layout('global.header', array(
   'class' => 'index',
   'snippets' => $snippets[];
));

?>

And then, on the layout page something like:

<?php perch_layout_var('snippet[phone]'); ?>

I know that won't work, but is something like that possible?

Drew McLellan

Drew McLellan 2638 points
Perch Support

$snippets['phone'] = '123-456-7890';

$layout_vars = $snippets; // copy snippets array
$layout_vars['class'] = 'index'; // add any additional values

perch_layout('global.header', $layout_vars);

Then in your layout

$snippets = perch_layout_var('snippets', true);
echo $snippets['phone'];

Awesome. Thanks!

This part threw the error:

Illegal string offset 'phone'

echo $snippets['phone'];

But this works:

<?php perch_layout_var('phone'); ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Err, yes, sorry.