Concrete5 Code Snippets

Posted by admin on November 19, 2011

I've put together a selection of commonly needed code snippets that I found are useful to have around.  Many can be found at:

http://www.concrete5.org/documentation/introduction/cheat-sheet/

http://www.weblicating.com/c5/cheat-sheet/

http://wiki.razrlight.com/concrete5_cheatsheet

I'm putting this together with the ones I use most often, as a point of reference for myself and for others.

Most of the code is borrowed from the above sites, or from various docs / forum posts on http://concrete5.org

Areas / Blocks

Adding a block (with most of the components you may need)

<?php 
  $a = new Area('sidebar');
  $a->setBlockWrapperStart('<div class="sidebar-block-wrapper">');
  $a->setBlockWrapperEnd('</div>');
  $a->setBlockLimit(3);
  $a->display($c); 
?>

Add a block.  Sometimes referred to as hardcoding a block to a template.  Often used for 'auto-nav'.
* Example code from http://www.weblicating.com/c5/cheat-sheet/

<?php
  <div id="nav">
    $bt = BlockType::getByHandle('autonav');
    $bt->controller->displayPages = 'top';
    $bt->controller->orderBy = 'display_asc';               
    $bt->controller->displaySubPages = 'none';               
    $bt->render('view');
  </div>
?>

Adding a block from the scrapbook
* example from http://www.weblicating.com/c5/cheat-sheet/ 

<?php
  $block = Block::getByName('BLOCK NAME');
  if( is_object($block) ) $block->display();
?>

Adding an autonav block directly into the template... here are all the options:

<?php 
   $bt_main = BlockType::getByHandle('autonav'); 
   $bt_main->controller->displayPages = 'custom'; // top, above, below, second_level, third_level, custom (Specify the displayPagesCID below)
   $bt_main->controller->displayPagesCID = '62'; // <-- Specify the CID of the page named: REPERTOIRE ( Gets the first level of pages under that section )
   $bt_main->controller->orderBy = 'display_asc';  // display_asc, display_desc, chrono_asc, chrono_desc, alpha_desc 
   $bt_main->controller->displaySubPages = 'relevant';  // none,  relevant, relevant_breadcrumb, all
   $bt_main->controller->displaySubPageLevels = 'custom'; //custom, none
   $bt_main->controller->displaySubPageLevelsNum = '0'; // Specify how deep level 
   $bt_main->render('templates/sub_nav'); // Specify your template or type "view" to use default
?>

News