Concrete5 – Blocks within Templates

Template that contains a block

When you create a new theme you sometimes need a custom autonav layout to make sure the navigation matches the theme. You can of course create a custom template and publish it somewhere. You could also create a Concrete5 package that puts everything into a single directory. But you would still have to select that custom template a few times.

As a theme create, you can actually create a theme that contains everything out of the box. No need for the user to add a navigation!

If you’re not familiar with the process of creating a theme, you should read this tutorial first: Create a Concrete5 Theme

We’re going to modify a template. It doesn’t matter which one you choose as long as there’s a little space left for a navigation. In this tutorial, we modify this template:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$this->inc('elements/header.php');
?>
<div id="logo">
    <a href="<?php echo DIR_REL?>/"><img src="<?php echo $this->getThemePath()?>/images/logo.gif" alt="Micropark Pilatus"/></a></div>
<div id="menu">
    <?php
    $nav = new Area('Navigation');
    $nav->display($c);
    ?></div>
<div id="nebenlinks">
    <?php
    $sub_nav = new Area('Sub Navigation');
    $sub_nav->display($c);
    ?></div>
<div id="page">
    <?php
    $content = new Area('Content');
    $content->display($c);
    ?></div>
<?php
$this->inc('elements/footer.php');
?>

There are two area’s we’re going to replace “Navigation” and “Sub Navigation”. The users shouldn’t be able to modify anything within these areas.

The new template looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$this->inc('elements/header.php');
?>
<div id="logo">
    <a href="<?php echo DIR_REL?>/"><img src="<?php echo $this->getThemePath()?>/images/logo.gif" alt="Micropark Pilatus"/></a></div>
<div id="menu">
    <?php
    $bt = BlockType::getByHandle('autonav');
    $bt->controller->displayPages = 'top';
    $bt->controller->orderBy = 'display_asc';                    
    $bt->controller->displaySubPages = 'none';                    
    $bt->render('templates/header_nav');
    ?></div>
<div id="nebenlinks">
    <?php
    $bt_links = BlockType::getByHandle('autonav');
    $bt_links->controller->displayPages = 'top';
    $bt_links->controller->orderBy = 'display_asc';                    
    $bt_links->controller->displaySubPages = 'none';                    
    $bt_links->render('view');
    ?></div>
<div id="page">
    <?php
    $content = new Area('Content');
    $content->display($c);
    ?></div>
<?php
$this->inc('elements/footer.php');
?>
  • Line 9 – Instead of an area, we get the handle to our block. autonav in this case
  • Line 10-12 – These are block specific settings we have to set. Unfortunately you have to look into the code to learn about these properties (until I publish a list of them)
  • Line 13 – Output the block! The default template is called “view”. In this case we specified a custom template which we want to use. Whatever suits you!

The same thing again with our sub navigation on lines 22-26.

Done! When you now create a new page, it will always contain our navigations. And if you want to replace it, just edit your template or custom template. You’ll notice that once you’ve added a new page, you don’t see the “red rectangle” around the navigation area. The user can’t (doesn’t have to) edit the navigation anymore!

block-in-template

Please note – if you’re using a block template which has its own css file – it doesn’t get included automatically anymore! You must include it in your theme.

I showed you two different methods to add default content. Which one you choose depends on your needs and probably also on your background. If you have any further questions/problems with this tutorials, leave me a message and I’ll try to modify the tutorials if possible.




Seguir leyendo: 1, 2


17 Comments

Das ist abhängig vom Template. Im Standard-Template wird zum Beispiel die CSS Klasse “nav-selected” verwendet. Also ganz einfach im CSS eine Anweisung wie diese einfügen:

.nav-selected {
font-weight: bold;
}

Sprekenzie deutsch? No, not really, but I am learning C5 thanks to these tutorials. Thanks!

Here are the autonav attributes and values I found by looking at the code:
controller->orderBy = ‘display_asc’;
//$bt_links->controller->displayUnavailablePages = ‘false’;
$bt_links->controller->displayPages=’second_level’;
$bt_links->controller->displaySubPages = ‘none’;
$bt_links->controller->displaySubPageLevels = ‘enough’;
$bt_links->render(‘view’);
?>

‘orderBy’
‘display_asc’ – in their sitemap order
‘display_desc’ – in reverse sitemap order
‘chrono_desc’ – with the most recent first
‘chrono_asc’ – with the earliest first
‘alpha_asc’ – in alphabetical order
‘alpha_desc’ – in reverse alphabetical order

‘displayUnavailablePages’
?’true’/’false’? (not sure if value be quoted)

‘displayPages’
‘top’ – at the top level
‘second_level’ – at the second level
‘third_level’ – at the third level
‘above’ – at the level above
‘current’ – at the current level
‘below’ – at the level below
‘custom’ – beneath a particular page

‘displaySubPages’
‘none’ – none
‘relevant’ – Relevant sub pages
‘relevant_breadcrumb’ – Display breadcrumb trail
‘all’ – Display all

‘displaySubPageLevels’
‘enough’ – Display sub pages to current
‘enough_plus1’ – Display sub pages to current +1
‘all’ – Display all
‘custom’ – Display a custom amount

‘render’
‘view’ – use default view
‘templates/custom_view’ – apply custom template

I used this method to modify the “full width” page type, adding a phone number above the navigation. But when I view the site following the update, the phone number is located below the navigation. If I go back to the “full width” template, the phone number is in the correct location. Why is it flipping positions on the child pages?

I doubt that’s related to this block. You should probably use a forum and post a link to the site – it’s a lot easier to help you if it isn’t just about theory.

DEUTSCH:
Ich möchte bei einem Theme-Page-Type einen vorgegebenen nicht lösch- oder verschiebbaren Block (z.B. den Blocktyp “Slideshow”) verpassen, welcher aber Einstellbar ist (z.B. Bilderauswahl).

ENGLISH:
How to implement a Blocktype (for example “slideshow”) in a pagetype of a theme permanently which is still customizable (assortment of pictures)?

I was having a hard time trying to implement changes to child pages. Until I read this tutorial. Thank you again.

Hi there!

I defiened a block slideshow on every page using the Page Type method, I also limited the number of Blocks to one (using setBlockLimit), but someone is still able to add more than one block (through deleting and re-adding) as well as blocks of an other type.

is ist possible to prohibit the deleting of the block but still allow to change it (i.e. change the pictures by an other user, not admin).

setBlockLimit only means that there can never be more than one block at the same time. This doesn’t mean that only the admin can remove it or anything like that.

There are some patches in the forums that allow you to specify the block types allowed in an area but as far as I know they never made it into the core.

The only thing you can do out of the box is to use the “advanced permission mode”. Check this site: http://www.concrete5.org/documentation/general-topics/simple-permissions/ There’s a line you have to insert in site/config.php to enable it.

But be careful: The advanced permission mode gives you a lot of power and increases the complexity as well. Just give it a try, you can always remove the line and go back to the basic permission mode if you don’t want to use the advanced settings.

Defaults doesnt really work does it? If Im in page type default and add a html block and some html within it, I’d expect the html that I wrote to be in every page that uses this page type. But it doesn’t do that.

So what exactly default is I dont know and no one has be any to match their description with what it actually does. (same as composer)

That’s probably because you’ve created the page first and then added blocks to the page defaults. Concrete5 can’t know if you’ve manually added that block on the previously created page first in which case it should put the block from page defaults there.

However, when you’re editing the page defaults and add a block, you can click on it again and you’ll see a menu entry which allows you to set up the block on existing pages as well.
Open it and you’ll see a list of all pages from the same type, mark those where you’d like to place the block and you’re done.

Leave a Reply

Your email address will not be published. Required fields are marked *