Forum

Thread tagged as: Question

Show regions based on page attribute

Hi Guys,

So I've set up the following in my page assets template:

<perch:pages id="is_landing_page" type="checkbox" label="Make landing page" help="Warning: Only enable this on landing pages" value="true" suppress="true"/>

Then on my content_sections.html template I have tried to do this:

<section class="landing <perch:if id="is_landing_page" value="false">collapsed</perch:if>">

However it doesn't seem to ever register as true. I've tried perch:showall and it doesn't seem to have any ability to see what the page attributes are. Is there a way to reference them?

Alex Ward

Alex Ward 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

What happens if you do this:

<section class="landing <perch:if exists="is_landing_page"><perch:else />collapsed</perch:if>">

Same thing unfortunately.

Drew McLellan

Drew McLellan 2638 points
Perch Support

How are you displaying the content_sections.html template?

Like this:

<?php include('perch/runtime.php');?>
<?php perch_layout('global.header'); ?>
    <?php perch_content('content_sections'); ?>
    <div class="main" id="main" role="main">
        <?php perch_content('parallax_section'); ?>
        <?php perch_content('testimonials_section'); ?>
        <?php perch_content('brands_section'); ?>
    </div>
<?php perch_layout('global.footer'); ?>

Is there such a thing as an 'if perch_page_attribute' statement? I suspect that although I've set 'is_landing_page' in page attributes, there is nothing to set that variable on the template itself.

Yoo could something like this in PHP:


$currentpage = perch_page_attribute('id', [], true); if ($currentpage == 'true') { echo "Yeah!"; } else {}

Ah I didn't realise I could make php templates. I thought they had to be html only. Thanks, I'll try it out :)

Hmm.. it seems they do have to be html. I can see the php template in the templates list but it wont allow me to select it.

Rachel Andrew

Rachel Andrew 394 points
Perch Support

PHP needs to be on the page not in the template. Perch template tags go in the template.

I can possibly achieve what I wanted in the page by switching templates in the page (using the conditional above), however I wanted to avoid repeating code, which I'd have to do if I used two templates.

Is there no way to check the existence of page attributes using template tags?

Would it be possible in your case to achieve what you want trough css selection like this:

    /*normal case*/
    .perchtemplate {
        color: #000;
    }

    /*conditinal case by page attribute*/
    .conditionalclass .perchtemplate {
        color: red;
    }

the conditional class is given in the page-template with the if-statement i posted above. Like this:

<!-- the container (in a perch page template) -->
<div class="page conditionalclass">

    <!-- the targeted element (perch content template) -->
    <div class="perchtemplate">
        <h1>Hello</h1>
    </div>

</div>

This may be the answer I was looking for.

https://docs.grabaperch.com/docs/templates/passing-variables-into-templates/

I'll try it and post back to let you know if it worked.

Manuel Hügel said:

Would it be possible in your case to achieve what you want trough css selection like this:

  /*normal case*/
  .perchtemplate {
      color: #000;
  }

  /*conditinal case by page attribute*/
  .conditionalclass .perchtemplate {
      color: red;
  }

the conditional class is given in the page-template with the if-statement i posted above.

Yes but I don't just want to change the page, I also want to prevent the fields appearing in the editor (I don't want the client to be confused and try to add information for a section which won't appear on some pages)

Rachel Andrew

Rachel Andrew 394 points
Perch Support

I'm really unclear as to what you are trying to do I'm afraid.

Can you explain what end result you are trying to achieve?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Ah right, I've just seen your reply to Manuel. That's not possible, the templates don't control what is seen in the editor. Use help text to describe what your user needs to do.

I think you should go with to separate content templates then.

Ah I see. Ok fair enough. Thanks anyway guys :)