Mecha CMS

Mecha CMS blog and documentation.

Shield Chunks in Mecha 1.2.x

Updated: Sunday, 07 August 2016

Safe template loader.

We have a new shield method called Shield::chunk() for making cleaner shield parts. This makes possible for you to load the template files safely because it is better than using the vanilla PHP include and require method. We also have enough built-in shield chunks to make the shield development becomes easier. There are some markup examples you can use, stored in lot\extends\chunk folder.

Loading a Chunk

This code will tries to include header.php file from your current shield folder if any, or from the extends\chunk folder if any. Otherwise, this code will outputs nothing:

<?php Shield::chunk('header'); ?>

The code above is somewhat similar to this, but included with usable hooks and filters:

if(file_exists(SHIELD . DS . $config->shield . 'header.php')) {
    require SHIELD . DS . $config->shield . 'header.php';
} else if(file_exists(CHUNK . DS . 'header.php')) {
    require CHUNK . DS . 'header.php';
} else if(file_exists(ROOT . DS . 'header.php')) {
    require ROOT . DS . 'header.php';
} else {
    // do nothing!
}

Examples

header.php

<!DOCTYPE html>
<html dir="<?php echo $config->language_direction; ?>" class="page-<?php echo $config->page_type ? $config->page_type : 'home'; ?>">
  <head>
    <?php Weapon::fire('SHIPMENT_REGION_TOP'); ?>
    <?php Weapon::fire('shell_before'); ?>
    <?php echo Asset::stylesheet(array(
        'assets/shell/atom.css',
        'assets/shell/layout.css'
    )); ?>
    <?php Weapon::fire('shell_after'); ?>
  </head>
  <body>
    <?php Weapon::fire('cargo_before'); ?>
    <div class="blog-wrapper">
      <?php Shield::chunk('block.header'); ?>
      <?php Shield::chunk('block.navigation'); ?>
      <div class="blog-content">

footer.php

      </div> <!-- .blog-content -->
      <?php Shield::chunk('block.footer'); ?>
    </div> <!-- .blog-wrapper -->
    <?php Weapon::fire('cargo_after'); ?>
    <?php Weapon::fire('sword_before'); ?>
    <?php echo Asset::javascript('assets/sword/layout.js'); ?>
    <?php Weapon::fire('sword_after'); ?>
    <?php Weapon::fire('SHIPMENT_REGION_BOTTOM'); ?>
  </body>
</html>

sidebar.php

<aside class="blog-sidebar widgets">
  <?php Shield::chunk('block.widget', array(
      'title' => $speak->widget->tags,
      'content' => Widget::tag()
  )); ?>
  <?php Shield::chunk('block.widget', array(
      'title' => $speak->widget->archives,
      'content' => Widget::archive()
  )); ?>
</aside>

article.php

<?php Shield::chunk('header'); ?>
<?php Shield::chunk('sidebar'); ?>
<div class="blog-main posts">
  <article class="post" id="post-<?php echo $article->id; ?>">
    <?php Shield::chunk('article.header'); ?>
    <?php Shield::chunk('article.body'); ?>
    <?php Shield::chunk('article.footer'); ?>
  </article>
  <?php Shield::chunk('pager'); ?>
  <?php Shield::chunk('comments'); ?>
</div>
<?php Shield::chunk('footer'); ?>

page.php

<?php Shield::chunk('header'); ?>
<?php Shield::chunk('sidebar'); ?>
<div class="blog-main posts">
  <article class="post" id="post-<?php echo $page->id; ?>">
    <?php Shield::chunk('page.header'); ?>
    <?php Shield::chunk('page.body'); ?>
    <?php Shield::chunk('page.footer'); ?>
  </article>
</div>
<?php Shield::chunk('footer'); ?>

index.php

<?php Shield::chunk('header'); ?>
<?php Shield::chunk('sidebar'); ?>
<div class="blog-main posts">
  <?php if($config->total_articles > 0): ?>
  <?php foreach($articles as $article): ?>
  <?php Shield::lot(array('article' => $article)); ?>
  <article class="post post-index" id="post-<?php echo $article->id; ?>">
    <?php Shield::chunk('article.header.index'); ?>
    <?php Shield::chunk('article.body.index'); ?>
    <?php Shield::chunk('article.footer.index'); ?>
  </article>
  <?php endforeach; ?>
  <?php else: ?>
  <article class="post">
    <?php Shield::chunk('article.body.204'); ?>
  </article>
  <?php endif; ?>
  <?php Shield::chunk('pager'); ?>
</div>
<?php Shield::chunk('footer'); ?>

404.php

<?php Shield::chunk('header'); ?>
<?php Shield::chunk('sidebar'); ?>
<div class="blog-main posts">
  <article class="post">
    <?php Shield::chunk('page.body.404'); ?>
  </article>
</div>
<?php Shield::chunk('footer'); ?>

manager.php

<?php Shield::chunk('header'); ?>
<?php Shield::chunk('sidebar'); ?>
<div class="blog-main posts">
  <article class="post">
    <?php Shield::chunk('page.title'); ?>
    <?php Shield::chunk('page.body'); ?>
  </article>
</div>
<?php Shield::chunk('footer'); ?>

comments.php

<?php if($config->comments->allow): ?>
<section class="comments">
  <?php Shield::chunk('comments.header'); ?>
  <?php Shield::chunk('comments.body'); ?>
  <?php Shield::chunk('comments.footer'); ?>
</section>
<?php endif; ?>

Hooks

Filter

FilterDescription
chunk:pathWill affect the generated chunk path.
chunk:lotWill affect the generated lot data.
chunk:inputWill affect the chunk input when output buffer is enabled.
chunk:outputWill affect the chunk output when output buffer is enabled.

Weapon

HookDescription
chunk_lot_beforeWill be triggered before lot data extraction.
chunk_lot_afterWill be triggered after lot data extraction.
chunk_beforeWill be triggered before chunk output.
chunk_afterWill be triggered after chunk output.
Donation and Email Subscription