Forum

Thread tagged as: Question, Problem, Runway

User setting Black or White page

I want my user to choose if the page is a White page or a Black page, at the moment I've got that set as an attribute so they can change it themselves and it changes the stylesheet.

This works fine as a rule, however I also need to change things in Layouts and PHP pages. But the attribute is set in a Content Template I don't see how I can effectively use that in the pages above (as it were).

The only way I can think of doing it is to have two versions of every Master Page, one with a Black attribute set and another with a White one, obviously that doubles changes work so does anyone have a better idea?

Thanks, Chris.

Chris Comben

Chris Comben 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you using a page attribute for this?

Yeah, at the moment I've got the following in a page attribute.

<link rel="stylesheet" type="text/css" href="/perch/burrells/styles/<perch:pages id="style_type" label="Page Colour" type="select" options="Black|black, White|white" default="black" required="true" divider-before="Page Layout" />.css" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok - and in which context are you having problems using that?

That works fine but only changes the stylesheet. I need the whole page to be aware that it is black or white so that it can get images from another directory.

I sort of need it as a system variable I suppose, but then I wouldn't know where the user could set it per page or how to then use that in both php and HTML templates.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use perch_page_attribute() to get that attribute back:

perch_page_attribute('style_type');

or

if (perch_page_attribute('style_type', true)=='black') {
    .... black ...
}else{
    ... white ...
}

or assign it into a template:

PerchSystem::set_var('style_type', perch_page_attribute('style_type', true));

and then use it in the template:

<perch:if id="style_type" match="eq" value="black">
   ... black ...
<perch:else />
   ... white ...
</perch:else>

https://docs.grabaperch.com/docs/pages/page-attributes/page-functions/perch-page-attribute/

Great thanks, thats almost worked, if I use it as a simple replacement ie

/img/<?php perch_page_attribute('style_type',true) ?>/logo.png

But it always prints to the screen, so I can't use it for if else statements or even add it to a system variable. When I do I just get 'white' or 'black' appear in text at the top of the page.

Drew McLellan

Drew McLellan 2638 points
Perch Support

This will return:

 perch_page_attribute('style_type',true);

This will echo:

 perch_page_attribute('style_type');

So this:

/img/<?php perch_page_attribute('style_type',true) ?>/logo.png

should be this:

/img/<?php perch_page_attribute('style_type'); ?>/logo.png

I agree that should be the case but it isn't, the true doesn't appear to change anything, it always echos.

In one of my templates that I use in a layout file its coded like this:

<li>
    <a href="watches/<perch:content id="slug"/>" title="<perch:content id="brand"/>">
    <perch:if id="style_type" match="eq" value="white">
        <img src="<perch:content id="nav_white"/>" alt="<perch:content id="brand"/>"/>
    <perch:else />
        <img src="<perch:content id="nav_black"/>" alt="<perch:content id="brand"/>"/>
    </perch:else>   
    </a>
</li>

However the if else doesn't work and it outputs both images and at the top of the page before <html> there is simply 'black'.

I've set this in the header layout file, which is where the template should be reading it from.

$tempstyle = perch_page_attribute('style_type', true);
PerchSystem::set_var('style_type', $tempstyle);

The only reason I've added it to a variable is just to test that wasn't the problem.

In fact more reason seems to suggest that this line is what gets echo'd.

PerchSystem::set_var('style_type', perch_page_attribute('style_type', true));
Drew McLellan

Drew McLellan 2638 points
Perch Support

Oh, sorry, my mistake. The second parameter is an options array. Set true as the third parameter to return.

Awesome, nailed it thanks!