Forum

Thread tagged as: Question, Problem

How do I add content that can be shown or hidden on a page?

I can't figure out how to make this work. I have structured pages for a website. The top of the pages remains the same for each page. the bottom sections can be different. I have created shared content for the bottom sections. But I have no idea how to allow them to be shown or hidden on any given page.

Here is the site. Notice the [about page] (https://iswebdev.com/LC/about.php) 2 bottom sections and the [services page] (https://iswebdev.com/LC/services.php) bottom section. How can I allow the editor to choose from shared content for the bottom of my pages when using ONE master template?

Kim Mazzola

Kim Mazzola 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I would use page attributes.

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

Set up an attribute to turn the item on or off and then test for it in the master page.

if (perch_page_attribute('show_whatever', [], true) {
    perch_content('My content');
}

Following your advice I took the steps below. But, pages created with my master template are blank. So, something is wrong with my code.

1 - Set up a page attribute template called section_attributes.html and saved it to the perch/pages/attributes directory 2 - Added this to the default.html template in the percha/pages/attributes directory 3 - The content of the section_attributes.html file is

<perch:pages id="showlist" label="Specialties List" help="Select the option to show or hide specialty listing on your page" type="select" options="true, false" divider-before="Specialty Listing" />

4 - Added the code you provided above and altered to fit my naming as below:

<!--* specialties section *-->
    <?php
        if (perch_page_attribute('showlist', [], true)
       {perch_content('Specialty Listing');
       });
?>

What am I missing? I'm sorry, but I'm not a php programmer. So, anything that is beyond html/css or perch tags is not within my knowledge.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Where did you add the code? You're using template comments which suggests to me you might have added PHP to your template. It should be in your master page.

That is added on my master page. I used comments so that I know where the structured part of the master page ends and where the other sections will begin.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, so what problem are you seeing?

It returns a blank page when the code is in the master template. When I remove the code, the page resolves.

<!--* specialties section *-->
    <?php
        if (perch_page_attribute('showlist', [], true)
       {perch_content('Specialty Listing');
       });
?>

Here is the full master page template:

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
<?php perch_layout('global.head'); ?>

<body id="inBody">
<section>
    <header>
        <div class="container">
            <div class="row">
                <div class="col-sm-4">
                   <?php perch_content_custom ('Contact Icons', array (
                        'template' => 'contact-icons.html'
                        )
                    );
                 ?>
                </div>
                <div class="col-sm-3">
                  <?php perch_content_custom ('Reservation Button', array (
                        'template' => 'reservation-link.html'
                        )
                    );
                 ?>  
                </div>
                <div class="col-sm-5">
                    <?php perch_content_custom ('Social Media Icons', array (
                        'template' => 'social-media-icons.html'
                        )
                    );
                 ?>  
                </div>
             </div>
   </div><!--* ./container *-->
  </header>
</section>

  <section id="navigation" class="blue_section">
    <div class="container">
        <div class="row">
        <div class="col-sm-4">
             <?php perch_content('LC Logo'); ?>
          </div>
          <div class="col-sm-8">
            <?php perch_pages_navigation(array('levels'=>1)); ?>
         </div>
       </div><!--* ./row *-->
    </div><!--* ./container *-->
</section>   

<section id="topBanner" class="hidden-xs">
    <div class="container">
        <div class="row">
         <div class="col-sm-6">
             <p><?php perch_content('Interior Banner Text'); ?></p>
         </div>
         <div class="col-sm-6">
             <?php perch_content('Interior Banner Fleet Image'); ?>
         </div>
        </div>
   </div><!--* ./container-fluid *-->
</section>
<section id="contentInterior" class="light_section">
    <div class="container">
        <div class="row">
            <div class="col-sm-8">
                <?php perch_content('Interior Left Column'); ?>
                </div><!--* ./ col-sm-8 *-->
               <div class="col-sm-4">
                <div class="sidebar">
                    <?php perch_content('Interior Right Column'); ?>
                </div>
               </div><!--* ./col-4 *-->
           </div><!--* ./row *-->
       </div><!--* ./container *-->
</section>   

    <!--* specialties section *-->
    <?php
        if (perch_page_attribute('showlist', [], true)
       {perch_content('Specialty Listing');
       });
?>



<!--* testimonials section *-->
    <section id="testimonialArea" class="dark_section parallax">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
            <?php perch_content('Testimonial Intro'); ?>
            <?php perch_content('Testimonials'); ?>
          </div><!--* ./row col 12 *-->
        </div><!--* ./row *-->
    </div><!--* ./container *-->
</section>


    <?php perch_layout('global.footer'); ?>
</body>
</html>

I'm not sure if this matters or not, but the Specialties Listing is set up as a shared item. Does that have anything to do with it?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

If you have a "blank page" then you have a PHP error so you will need to look in your error log to see what the error is.

Yes, but the only addition to the page is the PHP code supplied by Drew in this thread. So, that is the problem and since he was helping me, I thought he might spot the error in the PHP since I am not a programmer. I was hoping since he asked what the output was, that he would see something that might help me.

I'm noticing that you are adding a parenthesis and semicolon after the brace where Drew's example ends with the brace (curly bracket).

Drew's example:

if (perch_page_attribute('show_whatever', [], true) {
    perch_content('My content');
}

Your example:

    <?php
        if (perch_page_attribute('showlist', [], true)
       {perch_content('Specialty Listing');
       });
?>
Rachel Andrew

Rachel Andrew 394 points
Perch Support

The error log is how you debug PHP so if you need further help then we need to see what the actual error is. If you don't want to provide that then the end result is we have to guess, and it will take longer for you to get help.

Here is my error log:

~~~

Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 82 [16-Jul-2016 08:27:04 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 82 [16-Jul-2016 08:29:46 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 79 [18-Jul-2016 07:25:26 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 79 [18-Jul-2016 07:26:09 America/New_York] PHP Parse error: syntax error, unexpected '?>' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 81 [18-Jul-2016 07:27:49 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 80 [18-Jul-2016 07:28:52 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 80 [18-Jul-2016 07:28:55 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 80 [18-Jul-2016 07:30:22 America/New_York] PHP Parse error: syntax error, unexpected '?>' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 82 [18-Jul-2016 07:30:24 America/New_York] PHP Parse error: syntax error, unexpected '?>' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 82 [18-Jul-2016 07:31:36 America/New_York] PHP Parse error: syntax error, unexpected '?>' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 81 [18-Jul-2016 07:32:11 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 80 [18-Jul-2016 07:32:37 America/New_York] PHP Parse error: syntax error, unexpected '?>' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 81 [18-Jul-2016 07:33:50 America/New_York] PHP Parse error: syntax error, unexpected ';' in /Volumes/OWC_Work_files/work/PHP-Sites/dev.limousineconnection/perch/templates/pages/structured-interior-page.php on line 79

Here is the code on the master page.

<?php if (perch_page_attribute('showlist', [], true) { perch_content('specialty list'); } ?>

~~~

I tried removing the semi-colon and then I get the error of the unexpected error of the question mark and closing bracket.

So, I'm not sure how to proceed.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You're just missing a bracket:

<?php if (perch_page_attribute('showlist', [], true)) { perch_content('specialty list'); } ?>

Thank you, that gets me a bit closer. But now, it turns on, but when set to false it doesn't turn off. And, the top of my output page shows the word "true" or "false" -- from the setting of the page attributes in page details in the CMS. I'm not sure what code to show you from here.

Other odd behavior, when I remove the call to section_attributes.html from my default.php file in perch/pages/attributes the calls to jquery, bootstrap css and other files that belong in between my head tags are there.

When I include the call to section_attributes.html in the default template all the files in the head tags move to the body of the page rather than in between the head tags as it should be. I am using a feather to include the call to bootstrap and jquery.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show us the exact code you're now using?

Sure:

added to default.php in pages/attributes:

<perch:template path="pages/attributes/section_attributes.html" />

created section-attributes.html in pages/attributes:

<perch:pages id="showlist" label="Specialties List" help="Select the option to show or hide specialty listing on your page" type="select" options="true, false" divider-before="Specialty Listing" />

the full master page template:

<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php'); ?>
<?php perch_layout('global.head'); ?>


<body id="inBody">
<section>
    <header>
        <div class="container">
            <div class="row">
                <div class="col-sm-4">
                   <?php perch_content_custom ('Contact Icons', array (
                        'template' => 'contact-icons.html'
                        )
                    );
                 ?>
                </div>
                <div class="col-sm-3">
                  <?php perch_content_custom ('Reservation Button', array (
                        'template' => 'reservation-link.html'
                        )
                    );
                 ?>  
                </div>
                <div class="col-sm-5">
                    <?php perch_content_custom ('Social Media Icons', array (
                        'template' => 'social-media-icons.html'
                        )
                    );
                 ?>  
                </div>
             </div>
   </div><!--* ./container *-->
  </header>
</section>

  <section id="navigation" class="blue_section">
    <div class="container">
        <div class="row">
        <div class="col-sm-4">
             <?php perch_content('LC Logo'); ?>
          </div>
          <div class="col-sm-8">
            <?php perch_pages_navigation(array('levels'=>1)); ?>
         </div>
       </div><!--* ./row *-->
    </div><!--* ./container *-->
</section>   

<section id="topBanner" class="hidden-xs">
    <div class="container">
        <div class="row">
         <div class="col-sm-6">
             <p><?php perch_content('Interior Banner Text'); ?></p>
         </div>
         <div class="col-sm-6">
             <?php perch_content('Interior Banner Fleet Image'); ?>
         </div>
        </div>
   </div><!--* ./container-fluid *-->
</section>
<section id="contentInterior" class="light_section">
    <div class="container">
        <div class="row">
            <div class="col-sm-8">
                <?php perch_content('Interior Left Column'); ?>
                </div><!--* ./ col-sm-8 *-->
               <div class="col-sm-4">
                <div class="sidebar">
                    <?php perch_content('Interior Right Column'); ?>
                </div>
               </div><!--* ./col-4 *-->
           </div><!--* ./row *-->
       </div><!--* ./container *-->
</section>   

<!--* specialties section *-->
    <?php if (perch_page_attribute('showlist', [], true)) { perch_content('Specialty Listing'); } ?>



<!--* testimonials section *-->
    <section id="testimonialArea" class="dark_section parallax">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
            <?php perch_content('Testimonial Intro'); ?>
            <?php perch_content('Testimonials'); ?>
          </div><!--* ./row col 12 *-->
        </div><!--* ./row *-->
    </div><!--* ./container *-->
</section>


    <?php perch_layout('global.footer'); ?>
</body>
</html>

global.head

<!DOCTYPE html>
    <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]*-->
    <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]*-->
    <!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]*-->
    <!--[if gt IE 8]><!*--> <html class="no-js"> <!--<![endif]*-->

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title><?php perch_pages_title(); ?></title>
    <?php perch_page_attributes(); ?>
    <?php perch_get_css(); ?>
    <?php perch_get_javascript(); ?>


    <!--* font Awesome *-->

        <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">

        <!--* REVOLUTION STYLE SHEETS *-->
        <link rel="stylesheet" type="text/css" href="css/settings.css">
        <!--* REVOLUTION LAYERS STYLES *-->
        <link rel="stylesheet" type="text/css" href="css/layers.css">

        <!--* REVOLUTION NAVIGATION STYLES *-->
        <link rel="stylesheet" type="text/css" href="css/navigation.css">

        <!--* REVOLUTION JS FILES *-->
        <script type="text/javascript" src="js/jquery.themepunch.tools.min.js"></script>
        <script type="text/javascript" src="js/jquery.themepunch.revolution.min.js"></script>

        <link rel="stylesheet" href="css/owl.carousel.css">

         <!--* Default Theme *-->
        <link rel="stylesheet" href="css/owl.theme.css">

        <!--* Include owl js plugin *-->
        <script src="js/owl.carousel.min.js"></script>

        <script type="text/javascript">
        $(document).ready(function(){
             $("#testimonials").owlCarousel({
                navigation : true, // Show next and prev buttons
              slideSpeed : 300,
              paginationSpeed : 400,
              singleItem:true

             });
          });
        </script>
</head>

And I'm using a feather to include the Bootstrap calls for CSS & JQuery

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think the issue is that your two options true, false are both truthy to PHP.

So try this instead:

if (perch_page_attribute('showlist', [], true) == 'true') { perch_content('Specialty Listing'); }

That did it!! Thank you so much for all your patience and help. It is GREATLY appreciated.