Jump to content

static content blocks


kazu
 Share

Recommended Posts

Is it possible to create content block/s, which can be called up via echo (or in another way) in a page, or template?

<?php echo $name-of-content-block; ?>

It often happens that I have in pages recurring static objects, which must not be edited by the page editor. I've done it in the past so, that I create a content block, which I call up in a page, or in the sidebar at any position. This could for example be a block with opening times, which must not be permanently changed.

Link to comment
Share on other sites

Hi kazu!

Like with most thing ProcessWire, there are a couple of approaches to this.

One thing I do often is to make a content block template, so they can be stored and edited in the CMS. Use the ProcessWire roles to restrict access if you need to. I then use a little helper function to call these, like so:

function getContentBlock($param = '') {
	if (strpos($param, '/') === FALSE) {
		// $block is name
		$param = wire('sanitizer')->selectorValue($param);
		$block = wire('pages')->get("template=content-block, name=$param");
	} else {
		$param = str_replace("//", "/", "/{$param}/");
		$block = wire('pages')->get("/settings/content-blocks{$param}");
	}

	if ($block->id) {
		return $block->body;
	}
}

As long as that function is included, I can do

echo getContentBlock('/contact/address/');    // using path to page
echo getContentBlock('opening-times');    // using page name

If static files are more suitable, then you can do something more like this:

echo wireRenderFile('blocks/opening-times');

Where the path to that file is site/templates/blocks/opening-times.php.

  • Like 4
Link to comment
Share on other sites

One more option would be the Hanna Code module, which allows you to define and embed the content blocks directly from within the Admin interface. You don't have to allow your content editors access to creating new content blocks, but they can still embed them.

The obvious benefit of include files or wireRenderFile() over Hanna Code is that you can have these under version control system (if you use one), and thus it's easier to keep track of changes and harder to accidentally delete content.

Link to comment
Share on other sites

The simplest solution I see:

  1. Create a template file content_block.php
  2. Create a folder in your /site/templates/ folder called content-blocks
  3. Put in all content blocks you want (can be just .text or .PHP, whatever you wish)
  4. Download and install: FieldtypeSelectFile, give it a name.
  5. Go to templates add new template with the field you just created, name it content_block.
  6. Set the setting of the field to the folder content-blocks
  7. Check the Change Page Template setting.
  8. Create for every content block a page with the content_block template.
  9. Select in every page the block you want to render.
Then the magic:
// name of the content block template.
$blocks = $pages->find("template=content_block");

foreach ($blocks as $block) {
    echo $block->render();
}

Or you can make a pagefield (multiple) allowing al pages with template content_block.

  • Like 6
Link to comment
Share on other sites

Wow, I see, there are many many possibilities. Because I just start to learn, I'll start with the easiest way, and perhaps it is then also the right ;-)

Thank you for your tips to all of you!

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

Hello All,

Can we pick this thread up again?

What if I wanted to have a dropdown box of choices linking to 30-40 static content blocks for page editors to populate in a certain region of a given template. What would be the best approach?

That's my chief complaint about PW. Everything works so efficiently, that I'm always afraid I'm missing the most efficient way to do things.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...