kazu Posted August 28, 2015 Share Posted August 28, 2015 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 More sharing options...
LostKobrakai Posted August 28, 2015 Share Posted August 28, 2015 Whenever you need to have snippets of markup included I'd suggest using wireRenderFile and just call the snippets in. With this function it doesn't matter if these are purely static content or not. Link to comment Share on other sites More sharing options...
Craig Posted August 28, 2015 Share Posted August 28, 2015 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. 4 Link to comment Share on other sites More sharing options...
teppo Posted August 28, 2015 Share Posted August 28, 2015 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 More sharing options...
Martijn Geerts Posted August 28, 2015 Share Posted August 28, 2015 The simplest solution I see: Create a template file content_block.php Create a folder in your /site/templates/ folder called content-blocks Put in all content blocks you want (can be just .text or .PHP, whatever you wish) Download and install: FieldtypeSelectFile, give it a name. Go to templates add new template with the field you just created, name it content_block. Set the setting of the field to the folder content-blocks Check the Change Page Template setting. Create for every content block a page with the content_block template. 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. 6 Link to comment Share on other sites More sharing options...
mr-fan Posted August 28, 2015 Share Posted August 28, 2015 or if you need to point out some rules where such blocks or widgets are should be visible...you could setup a kind of widget system. https://processwire.com/talk/topic/8635-simple-example-for-widget-management/ solution depends on your needs, the project and your preference. regards mr-fan Link to comment Share on other sites More sharing options...
kazu Posted August 28, 2015 Author Share Posted August 28, 2015 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! 1 Link to comment Share on other sites More sharing options...
Jim Bailie Posted May 28, 2019 Share Posted May 28, 2019 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 More sharing options...
Gideon So Posted May 29, 2019 Share Posted May 29, 2019 Hi @Jim Bailie, You can use a page reference field. Gideon Link to comment Share on other sites More sharing options...
Jim Bailie Posted May 29, 2019 Share Posted May 29, 2019 @Gideon So Yeah, it looks like that will work. It also looks like Text Blocks in ProFields could accomplish this as well. But it would be nice just to have a simple a dropdown for editors to select the block. Ok, now I have a starting point. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now