Setting default image for a list and detail page.

  • Wondering if there's a way of achieving the following...


    I have a list and detail page where the user can upload a banner image to override a default banner image (set for the whole list) that I've uploaded to a resources folder outside of Perch CMS (for those cases when an image is not available when adding a new item to the list).


    Which is easily achieved in a template...


    Code
    1. <perch:if exists="bannerimage">
    2. <div class="static-banner" style="background-image: url(<perch:content id="bannerimage" type="image" width="1400" height="200" crop="true" help="Add a full width banner image with a minimmum with of 1400px" />);">
    3. <perch:else>
    4. <div class="static-banner" style="background-image: url(../default-static-banner.jpg)">
    5. </perch:if>
    6. </div>

    However is there a way of the user changing the default banner themselves within Perch?


    Currently the default banner image has to appear on the list page as the default banner image but would also show on a single item detail page when nothing has been uploaded to override it.

  • Could you not manage the default banner with another region?

    The default image banner template is in another region. It's used in the list page (product_page.php) and the other template is using detail_template_with_banner.html in the (product_page.php?p=every_other_page).


    Is there a way of getting a field from a region in (product_page.php) into a region in (product_page.php?p=every_other_page) to do a compare with perch:if in a template?


    I thought they had to be in the same runtime?


    Or should the default banner template be in a shared region to make it available?

  • Hello David,


    Is there a way of getting a field from a region in (product_page.php) into a region in (product_page.php?p=every_other_page) to do a compare with perch:if in a template?


    These two are technically the same page. Anyway, if you want to get a region from another page, perch_content_custom() has a page option which allows you to do this.


    I thought they had to be in the same runtime?


    perch_content() outputs the region as evaluated and rendered when saved in the control panel. perch_content_custom() outputs the region as evaluated and rendered at run time (when the page is visited).


    You have 2 regions, (A) a banner (B) a list of products. To make something from (A) available in (B), (B) has to be output with perch_content_custom().


    Assuming (A) only contains the banner with the same HTML you need:


    PHP
    1. perch_content_custom('Products', [
    2. 'data' => [
    3. 'default_banner' => perch_content('Banner', true),
    4. ],
    5. ]);


    perch_content('Banner', true) gets you the rendered template (HTML included). Below the default_banner outputs the HTML too:


    HTML
    1. <perch:if exists="bannerimage">
    2. <div class="static-banner" style="background-image: url(<perch:content id="bannerimage" type="image" width="1400" height="200" crop="true" help="Add a full width banner image with a minimmum with of 1400px" />);">
    3. </div>
    4. <perch:else>
    5. <perch:content id="default_banner" type="hidden" html>
    6. </perch:if>



    If you only need to grab a field value, you'll have to use perch_content_custom() and the skip-template option to grab the field in PHP.

  • Thanks. I think I've got this correct and the default banner field is now available in the 'Products' region.


    However when I use...


    Code
    1. perch_content_custom('Products', [
    2. 'data' => [
    3. 'default_banner' => perch_content('Banner', true),
    4. ],
    5. ]);

    This as it stands outputs the detail page as a full list and not getting a single item on say page (product_page.php?p=my-page)


    How do I sort 'Products' on a single slug query?

  • Thanks it sort of works as the comparison can be made in the 'Products' region.


    However the Image banner being `suppress="true"` in the in the detail template is being drawn and published to another place on the page using...


    Code
    1. if (perch_get('p')) {
    2. perch_content_custom('Products', array(
    3. 'page' => '/product_page.php',
    4. 'template' => '_page_banner_image.html',
    5. 'filter' => 'slug',
    6. 'match' => 'eq',
    7. 'value' => perch_get('p'),
    8. 'count' => 1,
    9. ));

    ...and the 'default_banner' ID is not turning up in the _page_banner_image.html template.


    How to I get 'default_banner' in that template?