Forum

Thread tagged as: Question

Layout vars loading another perch_layout

I'm trying to load in different additional layout files using layout variables:

I have several css/js tags and code snippets I'd like to load for different pages. All stored in the layouts folder with different names.

I'd like to just specify which ones to load here:

perch_layout('global.footer', array(
            'scripts'   => 'scripts.contact'
        ));

I can retrieve one like so:

perch_layout('global.footer', array(
            'scripts'   => true
        ));

 if (perch_layout_var('scripts', true)) {
        perch_layout('scripts.contact');
        }

But I want to specify which ones:

perch_layout('global.footer', array(
            'scripts'   => 'scripts.contact'
        ));

     if (perch_layout_var('scripts', true)) {
        perch_layout('(perch_layout_var('scripts', true)');
        }

Any ideas please?

Mark Watts

Mark Watts 0 points

  • 5 years ago

Can you get the content of the layout variable and use that?

$script = perch_layout_var('scripts', true);

include('path/to/script/folder' . $script);

Hi Alex,

Your code sets the $script variable but doesn't apply it to the include so nothing loaded, but I've managed to get it to work using your code with layouts, so thank you for pointing me in the right direction and solving the problem :o)

Here's the working code:

Code on the page:

perch_layout('global.footer', array(
    'scripts'   => 'scripts.contact'
));

Code in the global.footer layout file:

$pagescripts = perch_layout_var('scripts', true);
    perch_layout($pagescripts);

Thanks again

Drew McLellan

Drew McLellan 2638 points
Perch Support

Or just:

if (perch_layout_var('scripts', true)) {
        perch_layout(perch_layout_var('scripts', true));
}

That worked perfect too, of course!

Thanks Drew