Forum

Thread tagged as: Question, Comments

Switch elements of a template on or off

I have two templates - they're the same apart from one of them has Comments and the other one doesn't. Is there a way I can just use the same template? Obviously, I could just put the common stuff into a Layout, which might be sensible anyway, but I wondered if there's another way?

Ideally, I guess I'd have it switchable from the CMS interface? Maybe a simple select field with Yes/No for comments, and they're only shown if that is set to "Yes". (Thinking out loud I could get that value via perch_content_custom and then switch it). Also, is there a unique ID for a page, that I could use to associate comments with - at the moment I've just hardcoded a value in:

            <?php
                perch_comments_form('refugee-suggestions', 'Refugee suggestions');
                perch_comments('refugee-suggestions');
            ?>

Is there a "Page ID" value available? I guess I could use the Page title, and a slugged version of it (retrieved with Perch Content Custom? I'd quite like to be able to stick a Page ID on the end of it to guarantee uniqueness.

Paul Bell

Paul Bell 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use page attributes for this.

https://docs.grabaperch.com/docs/pages/page-attributes/

Ah, thanks, Drew - that makes sense. Here's what I've come up with:

templates/pages/attributes/default.html - I've added the "show_comments" field.

<perch:pages id="show_comments" type="radio" label="Show comments" options="Yes|y,No|n" default="n" suppress="true" />
<perch:template path="pages/attributes/seo.html" />

And then on my page I've got

            <?php
                $show_comments = perch_page_attribute('show_comments',array(
                    'skip-template' => 'true'
                ));

                if($show_comments === 'y')
                {
                    perch_comments_form($_SERVER['REQUEST_URI'], perch_pages_title(true));
                    perch_comments($_SERVER['REQUEST_URI']);
                }
            ?>

Skip template isn't in the docs for page functions, but seems to work - here it doesn't return an array, just the value itself, which is exactly what I needed. I guess I could split "show_comments" and have another option for "accept_comments" so the form could be switched independently from the comments themselves.

As an aside - on comments, is there an option to have them pre-moderated (so they don't show up on the site unless they've been approved). We've got Akismet on, but I guess the client still might like this option - I can see that there is a "PENDING" status, but I don't know how to set all comments to Pending prior to approval.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Comments won't show up unless they're approved - unless you post a comment as a logged in user.

Ah - OK. Makes sense. I hadn't posted without being logged in. Thanks for clarifying.