I really need to think of a clever name for my blog

Code samples, geeky links and other musings. Note this blog has recently moved from Posterous, so isn't fully back up to speed yet!

Pixie – user editable blocks

Pixie is a simple, lightweight CMS. I had to knock something up double quick, and Pixie did the job nicely. It’s pretty light on modules and plugins, but ripe for customisation as being so lightweight it’s very easy to get into quickly.

In Pixie ‘blocks’ are small sidebar-type elements for additional comments. These are easy enough to put in for a coder, but the developers admit that easily editable block content for users is on their list to do.

Well that just won’t do will it? I need to get some user editable content in there today!

So here’s how to do it.

Create a new page in admin:

Settings > Create a new pagePage Type: Static[Create Page]

Give it a slug and Display NameDon’t give it any blocks

Set:Public: Yes

If you don’t want this content to be accessible from the menu:In Navigation: No

[Save]

Edit the content of the new page:

Publish > {your new page title}Edit page and [Update]

That’s our content ready to go. You can obviously include as much or as little as you like, but as blocks are generally sidebars it’s probably best to keep it simple and minimal.

Next we need to create a new block.This is a REALLY simple file – we leverage the Pixie static page system and hack it into a block file called admin/blocks/block_{myblock}.phpThe {myblock} name can be whatever you want – just make sure it doesn’t clash with any existing blocks. This will be the block name you add to the page you want it to appear in.

And here’s the code for your block. Simply edit $block_static to the value of the page’s slug.

<?phpif (!defined('DIRECT_ACCESS')) {    header('Location: ../../');    exit();}//Edit this variable with the static page Slug/Url of the page you want to include in the block$block_static = 'serviceinc';/*** Pixie: The Small, Simple, Site Maker.* * Licence: GNU General Public License v3* Copyright (C) 2010, Scott Evans** This program is free software: you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation, either version 3 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program. If not, see http://www.gnu.org/licenses/** Title: Static Block** @package Pixie* @copyright 2011 David Benson* @author David Benson* @link http://blog.absolutedisaster.co.uk/pixie-user-editable-blocks* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3**/if ($page_type != 'static') {    include('admin/modules/static.php');}extract(safe_row('*', 'pixie_core', "page_name='$block_static'"));?><div id="block_static" class="block">    <div class="block_header">        <h4><?php echo $page_display_name;?></h4>    </div>    <div class="block_body">        <?php echo $page_content;?>    </div>    <div class="block_footer"></div></div><?php$s = $page_s;?>

You can use this method to create as many different editable blocks as you need – just change the $block_static value and the filename. You can probably adapt the same method pretty easily to use dynamic content with a bit of tweaking – I may look at that another day. I may not. Depends how far I go with Pixie.

Hmm. I know a couple of girls named Pixie. I should probably rephrase that…

[update201105251920]Later that evening I improved this slightly, so that the $block_static takes its value from the filename. This means that anyone with ftp access can very quickly and easily add a new block. Just keep a spare copy in the admin/blocks folder – possibly name it block_rename_me.php so it’s easily recognised, then site admins can copy and rename your master block_rename_me.php file to block_{newblock}.php where {newblock} is the name of the… new… block.

The changes:

Replace:

//Edit this variable with the static page Slug/Url of the page you want to include in the block$block_static = 'serviceinc';

with:

$block_static = substr(strrchr(__FILE__, '/'), 1);$block_static = substr($block_static, 6); $block_static = substr($block_static, 0, strpos($block_static, ".php"));

You can also replace:

<div id="block_static" class="block">

with:

<div id="block_<?php echo $block_static;?>" class="block">

and you now have a dynamic id you can target for styling and manipulation.

Complete with changes, with the $block_static definition moved for tidiness and consistency:

<?phpif (!defined('DIRECT_ACCESS')) {    header('Location: ../../');    exit();}/*** Pixie: The Small, Simple, Site Maker.* * Licence: GNU General Public License v3* Copyright (C) 2010, Scott Evans** This program is free software: you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation, either version 3 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program. If not, see http://www.gnu.org/licenses/** Title: Static Block** @package Pixie* @copyright 2011 David Benson* @author David Benson* @link http://blog.absolutedisaster.co.uk/pixie-user-editable-blocks* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3**/$block_static = substr(strrchr(__FILE__, '/'), 1);$block_static = substr($block_static, 6); $block_static = substr($block_static, 0, strpos($block_static, ".php"));if ($page_type != 'static') {    include('admin/modules/static.php');}extract(safe_row('*', 'pixie_core', "page_name='$block_static'"));?><div id="block_<?php echo $block_static;?>" class="block">    <div class="block_header">        <h4><?php echo $page_display_name;?></h4>    </div>    <div class="block_body">        <?php echo $page_content;?>    </div>    <div class="block_footer"></div></div><?php$s = $page_s;?>

[/update]