Forum

Thread tagged as: Question

custom content using blocks

I'm trying to load using custom_content from a page that has a single region with blocks, template is below:

<!--* Main heading and introduction and graphic *-->
<perch:before>
<perch:template path="content/blocks/introduction_block.html" />
</perch:before>

<perch:blocks divider-before="Choose content block to suit you're content as many or little as you want, you can even reorder them.">

    <!--* Brand logos *-->
    <perch:block type="brand-logos" label="Brand logos">
        <perch:template path="content/blocks/brand_logos_block.html" />
    </perch:block>

    <!--* Text *-->
    <perch:block type="title-text" label="Heading and Text">
        <perch:template path="content/blocks/text_block.html" />
    </perch:block>

    <!--* Lists used for things like Features or Benefits *-->
    <perch:block type="text" label="Information List">
        <perch:template path="content/blocks/list_block.html" />
    </perch:block>

</perch:blocks>

Perch will load all the data from the 'Product Details' region:

    perch_content_custom('Product Details', array(
        'page' => '/products-and-services/*',
    ));

I only want to load the 'Brand logos' data, if I use the below it doesn't load any data:

    perch_content_custom('Product Details', array(
        'page' => '/products-and-services/*',
        'template'  => 'blocks/brand_logos_block.html',
    ));

This is the brand logos template:

<ul class="brand-logos">
    <perch:repeater id="brand-logos" label="Add a brand logo">
    <li><img src="<perch:content id="brand-logo" type="image" width="125" label="Image" bucket="brands" />" alt="<perch:content type="text" id="alt" label="Description" required="true" help="Brand name" />" srcset="<perch:content id="brand-logo" type="image" width="125" label="Image" density="2" bucket="brands" /> 2x, <perch:content id="brand-logo" type="image" width="125" label="Image" density="3" bucket="brands" /> 3x"></li>
    </perch:repeater>
</ul>

How do I get only that block of data to display?

Mark Watts

Mark Watts 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Your brand logos template needs to include the perch:blocks tags and perch:block tag from the master.

Ok I've done that and it loads the data :o) BUT it repeats it twice?

<perch:blocks>
<perch:block type="brand-logos" label="Brand logos">
<ul class="brand-logos">
    <perch:repeater id="brand-logos" label="Add a brand logo">
    <li><img src="<perch:content id="brand-logo" type="image" width="125" label="Image" bucket="brands" />" alt="<perch:content type="text" id="alt" label="Description" required="true" help="Brand name" />" srcset="<perch:content id="brand-logo" type="image" width="125" label="Image" density="2" bucket="brands" /> 2x, <perch:content id="brand-logo" type="image" width="125" label="Image" density="3" bucket="brands" /> 3x"></li>
    </perch:repeater>
</ul>
</perch:block>
</perch:blocks>

Sorry, meaning it outputs the set of data twice on the page.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show me the code you're using?

hmm now I've added the perch:blocks tags and perch:block tag to the brand logos template the perch edit form has lost the ability to add any logos! I think I'm not understanding blocks which is a shame. I guess I'll have to go back to just regions :o(

The code I was using is above.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, I didn't notice you were including one template inside the other.

Clearly now you're importing a set of blocks tags inside an existing set, which isn't going to work. You'll need to split the templates down.

OK started with fresh eyes this morning…I'm still confused, can you clarify for me. I'm following the tutorial for Template includes and Blocks: https://docs.grabaperch.com/video/v/template-includes-and-blocks/

This all works correctly using the code at the start of this thread, I can add multiple blocks and content and reorder them. The only difference between mine and the video is that two of the templates uses repeaters. The content all displays correctly on the webpage, all good so far.

If I then try and use the custom_content tag to load just the list of brand logos on another page it finds the template but doesn't load the content?

So this loads all the content to a different page from the product page including brand logos and other content in blocks:

    perch_content_custom('Product Details', array(
        'page' => '/products-and-services/*',
    ));

I only want to load one of the blocks of content the 'brand logos':

    perch_content_custom('Product Details', array(
        'page' => '/products-and-services/*',
        'template'  => 'blocks/brand_logos_block.html',
    ));

As soon as I use the above no content loads, but if I use the below it will load the introduction text which as above is outside the blocks tag:

    perch_content_custom('Product Details', array(
        'page' => '/products-and-services/*',
        'template'  => 'content/blocks/introduction_block.html',
    ));

So to sum up… everything is working as per video and as I envisaged except I can't use perch_custom_content to load content from within blocks. I can send you the files if you want?

My diagnostics are… (local MAMP setup)

SUMMARY INFORMATION

Perch: 2.8.13, PHP: 5.5.3, MySQL: 5.5.33, with PDO
Server OS: Darwin, apache2handler
Installed apps: content (2.8.13), assets (2.8.13), categories (2.8.13), perch_forms (1.8.3)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'forms', );
PERCH_LOGINPATH: /admin
PERCH_PATH: /Applications/MAMP/htdocs/***/admin
PERCH_CORE: /Applications/MAMP/htdocs/***/admin/core
PERCH_RESFILEPATH: /Applications/MAMP/htdocs/***/admin/resources
Image manipulation: GD
PHP limits: Max upload 32M, Max POST 32M, Memory: 32M, Total max file upload: 32M
Resource folder writeable: Yes
HTTP_HOST: outlet.site
DOCUMENT_ROOT: /Applications/MAMP/htdocs/***
REQUEST_URI: /admin/core/settings/diagnostics/
SCRIPT_NAME: /admin/core/settings/diagnostics/index.php
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, if your templates are back to how they were at the start, I think you need to create a template with this:

<perch:blocks>
    <perch:block type="brand-logos" label="Brand logos">
        <perch:template path="content/blocks/brand_logos_block.html" />
    </perch:block>
</perch:blocks>

Then use this new template with your perch_content_custom() call. A block always needs to appear within <perch:blocks> tags whenever it's used.

Fantastic thanks! that works a treat… So effectively if you want to use blocks with custom_content then you have to do it with the perch:blocks inside the template that's included and NOT like the video?

The below now works:

<!--* Main heading and introduction and graphic *-->
<perch:before>
<perch:template path="content/blocks/introduction_block.html" />
</perch:before>

<!--* Brand logos *-->
<perch:template path="content/blocks/brand_logos_block.html" />

<!--* Text *-->
<perch:template path="blocks/text_block.html" />

<!--* Lists used for things like Features or Benefits *-->
<perch:template path="content/blocks/list_block.html" />
<perch:blocks>
    <perch:block type="brand-logos" label="Brand logos">
<ul class="brand-logos">
    <perch:repeater id="brand-logos" label="Add a brand logo">
    <li><img src="<perch:content id="brand-logo" type="image" width="125" label="Image" bucket="brands" />" alt="<perch:content type="text" id="alt" label="Description" required="false" help="Brand name" />" srcset="<perch:content id="brand-logo" type="image" width="125" label="Image" density="2" bucket="brands" /> 2x, <perch:content id="brand-logo" type="image" width="125" label="Image" density="3" bucket="brands" /> 3x"></li>
    </perch:repeater>
</ul>
    </perch:block>
</perch:blocks>

BUT there is a problem… I've separated the code into includes so I can reuse elsewhere, so for example the below is used on other pages:

<<perch:content id="heading-type" type="select" label="Heading type" help="Headings help prioritize the content, the further down the page the less important.  Heading 1 is used for the main heading of the page." options="Heading 2|h2, Heading 3|h3, Heading 4|h4, Heading 5|h5, Heading 6|h6" />><perch:content id="heading" type="text" label="Heading" required="false" title="true" /></<perch:content id="heading-type" />>
<perch:content id="text-para" type="textarea" label="Text" textile="true" html="true" size="s" />

So if I add the required code to this template:

<perch:blocks>
<perch:block type="title-text" label="Heading and Text">

CODE IN HERE

</perch:block>
</perch:blocks>

When this template is used elsewhere it doesn't show as a normal region but a block.

So essentially to use custom content with blocks and includes to reuse code, I would have to create two templates:

text_template.html

TEMPLATE TO BE REUSED ELSEWHERE
<perch:content id="text-para" type="textarea" label="Text" textile="true" html="true" size="s" />

text_block_template.html

IF BLOCKS SETUP REQUIRED
<perch:blocks>
<perch:block type="title-text" label="Heading and Text">
     <perch:template path="content/blocks/text_template.html" />
</perch:block>
</perch:blocks>

Is that correct?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's right - is it not working?

Fantastic thanks! that works a treat… thanks for your help :o)

Ok maybe not! I've just gone to add some more content and now the Blocks appear and you can click them to add new content but it just displays the grey bar and no fields?

This is what I'm using:

Product details region template:

<!--* Main heading and introduction and graphic *-->
<perch:before>
<perch:template path="content/blocks/introduction_block.html" />
</perch:before>

<!--* Brand logos *-->
<perch:template path="content/blocks/brand_logos_block.html" />

<!--* Text *-->
<perch:template path="content/blocks/text_block.html" />

<!--* Lists used for things like Features or Benefits *-->
<perch:template path="content/blocks/list_block.html" />

Included template:

<perch:blocks>
    <perch:block type="brand-logos" label="Brand logos">
<ul class="brand-logos">
    <perch:repeater id="brand-logos" label="Add a brand logo">
    <li><img src="<perch:content id="brand-logo" type="image" width="125" label="Image" bucket="brands" />" alt="<perch:content type="text" id="alt" label="Description" required="false" help="Brand name" />" srcset="<perch:content id="brand-logo" type="image" width="125" label="Image" density="2" bucket="brands" /> 2x, <perch:content id="brand-logo" type="image" width="125" label="Image" density="3" bucket="brands" /> 3x"></li>
    </perch:repeater>
</ul>
    </perch:block>
</perch:blocks>
Drew McLellan

Drew McLellan 2638 points
Perch Support

One set of <perch:blocks> tags should wrap your blocks.

So if you have one block:

<perch:blocks>
    <perch:block ...>
    </perch:block>
</perch:blocks>

If you have multiple blocks:

<perch:blocks>

    <perch:block ...>
    </perch:block>

    <perch:block ...>
    </perch:block>

    <perch:block ...>
    </perch:block>

    <perch:block ...>
    </perch:block>

</perch:blocks>

It's no more complex than that. That's all there is to it.

Ok remind me not to use Blocks and Custom Content again! :o(

This is now working by adding another template just for the using with custom_content and blocks…

Blocks added around ALL includes as there is multiple includes:

<!--* Main heading and introduction and graphic *-->
<perch:before>
<perch:template path="content/blocks/introduction_block.html" />
</perch:before>

<perch:blocks>
<!--* Brand logos *-->
<perch:template path="content/blocks/brand_logos_block.html" />

<!--* Text *-->
<perch:template path="content/blocks/text_block.html" />

<!--* Lists used for things like Features or Benefits *-->
<perch:template path="content/blocks/list_block.html" />
</perch:blocks>

Individual Blocks template WITHOUT blocks tag pair as this is included in a file that has the blocks tag pair:

    <perch:block type="brand-logos" label="Brand logos">
<ul class="brand-logos">
    <perch:repeater id="brand-logos" label="Add a brand logo">
    <li><img src="<perch:content id="brand-logo" type="image" width="125" label="Image" bucket="brands" />" alt="<perch:content type="text" id="alt" label="Description" required="false" help="Brand name" />" srcset="<perch:content id="brand-logo" type="image" width="125" label="Image" density="2" bucket="brands" /> 2x, <perch:content id="brand-logo" type="image" width="125" label="Image" density="3" bucket="brands" /> 3x"></li>
    </perch:repeater>
</ul>
    </perch:block>

This now displays as desired in the admin interface. Now for the custom_content bit:

To load only one of the blocks of content on another page:

NOTE THIS REFERENCES A DIFFERENT TEMPLATE THAT HAS THE PERCH:BLOCKS TAG PAIR
perch_content_custom('Product Details', array(
    'page' => '/products-and-services/*',
    'template'  => 'blocks/brand_logos_blocks.html',
));
THIS TEMPLATE IS THE SAME AS THE INCLUDED ONE WITH THE ADDITION OF PERCH:BLOCKS TAG PAIR
<perch:blocks>
    <perch:block type="brand-logos" label="Brand logos">
<ul class="brand-logos">
    <perch:repeater id="brand-logos" label="Add a brand logo">
    <li><img src="<perch:content id="brand-logo" type="image" width="125" label="Image" bucket="brands" />" alt="<perch:content type="text" id="alt" label="Description" required="false" help="Brand name" />" srcset="<perch:content id="brand-logo" type="image" width="125" label="Image" density="2" bucket="brands" /> 2x, <perch:content id="brand-logo" type="image" width="125" label="Image" density="3" bucket="brands" /> 3x"></li>
    </perch:repeater>
</ul>
    </perch:block>
</perch:blocks>

perch_custom_content now loads the data :o) Phew! have I made this complicated or is it just that's the only way to do it ?

Thanks again.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You seem to be making heavy weather of it, but I don't know what you're trying to achieve and if Blocks are the best tool for that or not.

Hi Mark and Drew,

i have a very similar problem, but for me Marks solution didn't work (my english is not so well, i know...).

If i include the content from a page with the original blocks-template, all blocks we displayed correctly. Fine. But i want to have only the content of ONE these blocks (like Mark).

So i include the content from a page with an other blocks-template that only contains the block that i want. Now i have only the content from this block - great - but: twice :-(

I have all tried out - Marks solution and some things more. Nothing. Or twice.

The code:

Original-blocks-template.html :

<perch:blocks>

    <perch:block ...>
    </perch:block>

    <perch:block ...>
    </perch:block>

    <perch:block type="test" label="Test">
    </perch:block>

    <perch:block ...>
    </perch:block>

</perch:blocks>

The used-template.html used in perch_content_custom:

<perch:blocks>

    <perch:block type="test" label="Test">
    </perch:block>

</perch:blocks>

PHP:

perch_content_custom('Content', array(
    'page' => '/correct_path/something.php',
    'template'  => 'used-template.html',
));

All fine but twice!

Thank for some ideas and help! Thomas

Thomas. After it working on my local copy and display just once. I have just moved it over to the live server and it loads the blocked content twice as well?!

Any ideas drew?

Hi Mark, i found a solution for me. Not really cool but it works.. I think, there is a little bug inside the "blocks". I changed all, that i found. But: Every time it displays the content twice.

OK. I set my original blocks region as a multiple region and use the blocks that i didn't want to reuse on another page in another item of that multiple region.

That works.

Fore your: You have to set your blocks region as multiple and "outsource" your brand logos to another item. I hope, it's not so much effort.

Best regards, Thomas