Shared Region in Blocks

  • I'm super stuck... I tried following This Thread which outlined exactly what I'm trying to do; which is allow my editor to select shared regions and place them on the page using blocks.


    Here's where I ended up, and I can't get it to work.


    Then, when editing the region 'Content - Main' I choose my blocks filled template called block test



    The shared-test.html looks like this:


    HTML: shared-test.html
    1. <perch:if exists="Recognition">
    2. <perch:content id="Recognition" type="checkbox" label="Show MarTech Recognition" suppress>
    3. <perch:content id="sharedregion_recognition" type="hidden" html>
    4. </perch:if>
    5. <perch:if exists="Trusted_by">
    6. <perch:content id="Trusted_by" type="checkbox" label="Show Trusted By" suppress>
    7. <perch:content id="sharedregion_trusted_by" type="hidden" html>
    8. </perch:if>


    And just if the information is needed, my region keys are "martech_recognition" and "Trusted_by"


    I've seen places that say something along the lines of <?php perch_content('Content - Main'); ?> should be perch_content_custom - but then I have no clue what array options to have or which other region I would edit that would get my show.


    Any help would be super appreciated!!!

  • Hi,


    There is more than one way to achieve this, but you can continue using the same approach. Because you are passing the content from outside the Content - Main region, you need to use perch_content_custom():


    PHP
    1. perch_content_custom('Content - Main');



    By the way, you can simplify how you get the region's HTML:


    PHP
    1. // if you have other regions with the same name as the shared region, use perch_content_custom()
    2. $recognition_html = perch_content_custom('martech_recognition', [
    3. 'page' => '*',
    4. ], true);
    5. // otherwise use perch_content()
    6. $recognition_html = perch_content('martech_recognition', true);


    So your code can be simplified into this:

    PHP
    1. $recognition_html = perch_content('martech_recognition', true);
    2. $trusted_by_html = perch_content('Trusted_by', true);
    3. PerchSystem::set_vars([
    4. 'sharedregion_recognition' => $recognition_html,
    5. 'sharedregion_trusted_by' => $trusted_by_html,
    6. ]);
    7. perch_content_custom('Content - Main');


    You don't even need to use PerchSystem::set_vars(). You can use the data option instead:


    PHP
    1. $recognition_html = perch_content('martech_recognition', true);
    2. $trusted_by_html = perch_content('Trusted_by', true);
    3. perch_content_custom('Content - Main', [
    4. 'data' => [
    5. 'sharedregion_recognition' => $recognition_html,
    6. 'sharedregion_trusted_by' => $trusted_by_html,
    7. ]
    8. ]);
  • Thanks for help, but I still can't get it working.


    I've tried two things


    First like this:


    With the code like that above, I edit my region <?php perch_content('Content - Main'); ?> and give it the template block-test.html - And I'm able to have it output the content almost as I want, but it duplicates the non shared region. To clarify, I have a block that is some text, then a shared region selected, then some more text, then a second shared region - and it outputs as:

    First text
    First shared
    second text
    second shared

    first text - again

    second text - again


    I also tried the code as follows:



    But when I do it like that, I don't know how to edit 'Content - Main' as it doesn't show up as a region.


    If I select a the 'Content-Top' region and apply block-test.html template to it then the text I add shows up in the region, but then where <?php perch_content_custom('Content - Main'); ?> is just the following:

    The template <code></code> could not be found.

    The template <code></code> could not be found.


    I feel like I'm soo close, but obviously still missing something.

  • But when I do it like that, I don't know how to edit 'Content - Main' as it doesn't show up as a region.


    There are 3 content functions:


    1. perch_content_create() - creates a region if it doesn't exists
    2. perch_content() - creates a region if it doesn't exists and outputs cached HTML; Perch template is rendered when you save the region and then cached - so the template is not evaluated every time someone visits the page
    3. perch_content_custom() - outputs the region; Perch template is evaluated at runtime (i.e. when someone visits the page)




    So to use perch_content_custom(), you need to create the region first with perch_content_create() (or perch_content() where applicable):


  • When I use the above php with perch_content(), the output for my non shared blocks is repeated.

    Like this:

    Block 1

    Block 2


    Block 1

    martech_recognition

    Block 2

    Trusted By


    So the second part is correct, I just don't want the first part to show


    And when I use perch_content_create('Content - Main'); - I don't know where in the CMS the editor would edit the region?