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
?>

MySQL Misc.

Find and replace a piece of text.  Can be useful.  Can be dangerous.  I used to need it when moving Wordpress to a new server, the links are hardcoded into the database.

Replace TABLENAME, FIELD, FROM & TO with your info.

  1. update TABLENAME set FIELD = replace(FIELD, 'FROM', 'TO')

Menu navigation - disable a link with jQuery

So, I have a nice menu, but one of the links needs to be disabled, and just the submenu needs to be clickable.  I could probably edit the autonav template and use the PHP code to make sure that the menu link didn't have an "a" tag...  that didn't seem super friendly...

Instead, I decided to use jQuery to search for the link and remove the href attribute from it.  From what I can tell, this works very well, and doesn't ruin the styling.  Here's my code.

  1. <script>
  2. $(document).ready(function(){
  3. $('a[href$="/your-link-that-you-want-removed/"]').removeAttr('href');
  4. });
  5. </script>

News