Jump to content

reusable block across multiple pages of same template


DarsVaeda
 Share

Recommended Posts

Hi,

I have created a template and will create several pages with it.
One block on these pages is always the same text that I need to reuse. But it should be editable.

What is the appropriate way to achieve this?

I thought I might create a separate template and create a page with it. Then include that page into the other pages via a field.
But I could not find a field that is working like this.

Any help is appreciated!

Link to comment
Share on other sites

All you need is a textarea field. You can store any kind of text (including HTML) inside textarea fields.

Now, you probably have body field created for you, if you do, use it, if not create a textarea field named markup, and under Details tab change its editor to CKEditor. 

Create a new template for the reusable block named common. Add markup field (or body) to this template. If you need images etc add that field too. 

Create a new page with this template. Add your content. 

Inside the templates that will be using this block get common content by

$common = $pages->get('template=common'):

$block = $common->markup; // (or body) 

Then echo this block in anywhere inside your html.

 

Link to comment
Share on other sites

the question is also, when you say editable, should the edits made to the reusable block be singular across the site (meaning if you edit the block on one page, should it change for all pages), or did you mean that the reusable block, if edited, that those edits only apply to the page where you have edited it.

if the former, then the answer to this:

Quote

"I thought I might create a separate template and create a page with it. Then include that page into the other pages via a field."

would be a page reference field.

Link to comment
Share on other sites

Hi,

You could, for example, have a page - child of the homepage - named Tools (hidden) that has a child named Custom blocks or General blocks (hidden?).

Custom blocks or General blocks would have children. One of them would be your reusable block (hidden?).

They will need a template (with the fields you want), but not a template file.

Where you want to insert the text, you could have something like:

<?=$pages->get('/tools/general-blocks/your-block/')->body?> 

or

<?=$pages->get(idofyourblock)->body?>

id that you can find in Your Block page if you have the following in /site/config.php:

$config->debug = true;

NB: answered too late...

Link to comment
Share on other sites

2 minutes ago, Macrura said:

the question is also, when you say editable, should the edits made to the reusable block be singular across the site (meaning if you edit the block on one page, should it change for all pages)

Yes the content should change on all pages when I change the content of the reusable block.

Link to comment
Share on other sites

seems like @Christophe's or @abdus would both work, unless you need to enable editing of that block within the editor of the other one.. in that case you would probably use some hook to include a link to edit that included block, and maybe a page reference on the block to select which templates to show it on..

Link to comment
Share on other sites

16 hours ago, Macrura said:

page reference field.

That's how I did it at the end.

$page->page_reference_field->someTextArea;

So the "page_reference_field" contains a reference to the page that contains the block aka "someTextArea".
It was fairly simple at the end.

Thanks people!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...