Forum

Thread tagged as: Question

Does perch:pages only work in navigation templates?

I'm struggling to get a page attribute I set for an image to show in anything other than navigation templates

<perch:pages id="pageImage" width="1280"/>

It works fine in navigation templates but not standard content templates.

Tony Astley

Tony Astley 0 points

  • 4 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

Please show all of your code, it's hard to help when you just post snippets.

Also - as always - the short Diagnostics Report.

I have a extended set of attributes that asks the user for a page image/thumbnail:

<perch:pages id="pageImage" label="Menu Image" type="image" divider-before="Set Menu Image for Page" suppress="true" width="480" />
<perch:pages id="pageImage" type="image" suppress="true" width="768" />
<perch:pages id="pageImage" type="image" suppress="true" width="960" />
<perch:pages id="pageImage" type="image" suppress="true" width="1280" />

I do this because to my knowledge the only way of accessing/looping through content from other pages is if it exists as an attribute.

This code in my navigation template works fine and the image is returned where required:

<perch:before>
<div class="homeMenu section">
    <div class="wrapper">
        <div class="serviceBlocks section">
</perch:before>

        <a href="<perch:pages id="pagePath" />" class="serviceBlock">

            <h6><perch:pages id="pageNavText" /></h6>
            <div class="image">
                <div class="bg service<perch:pages id="perch_item_index" /> clearfix"><span></span></div>
            </div><!--/.image-->

            <style type="text/css">
                @media only screen {    
                    .bg.service<perch:pages id="perch_item_index" /> {  
                        background-image: url(<perch:pages id="pageImage" width="960"/>);   
                    }   
                }   


                @media only screen and (max-width: 480px) { 
                    .bg.service<perch:pages id="perch_item_index" /> {  
                        background-image: url(<perch:pages id="pageImage" width="480"/>);   
                    }
                }
            </style>

        </a><!--/.serviceBlock-->

<perch:after>
        </div><!--/.serviceBlocks-->
    </div><!--/.wrapper-->
</div><!--/.homeMenu section-->
</perch:after>

However when using more or less the same code in a content template, the template fails to return anything for the <perch:pages id="pageImage" width="960"/> or <perch:pages id="pageImage" width="960"/>.

Passing them into the template as variables isn't really an option as the variable defaults to one image size.

<div class="image">
                <div class="bg service clearfix"><span></span></div>
            </div><!--/.image-->

            <style type="text/css">
                @media only screen {    
                    .bg.service {   
                        background-image: url(<perch:pages id="pageImage" width="960"/>);   
                    }   
                }   


                @media only screen and (max-width: 480px) { 
                    .bg.service {   
                        background-image: url(<perch:pages id="pageImage" width="480"/>);   
                    }
                }
            </style>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Page attributes aren't exposed to content templates - the content region doesn't know anything about the page context when it's rendered.

The only way to do it will be to template the page attribute and then pass it into the region, and render the region using perch_content_custom()

When passing the 'pageImage' attribute through it always defaults to the 480 width, is there a way of controlling which size gets sent through?