Article · Mecha CMS


Post Format

Author: Taufik Nurrohman · 635 Views


Add post format feature.

A Post Format is a piece of meta information that can be used by a theme to customize its presentation of a post. The Post Formats feature provides a standardized list of formats that are available to all themes that support the feature.


It is possible to create a post format feature using Mecha by utilizing the type value:

title: Page Title
description: Page description.
author: Taufik Nurrohman
type: HTML

Page content goes here.

You might think that you can only set the type value as HTML or Markdown. But, actually you can set any value into it, as you like:

title: Page Title
description: Page description.
author: Taufik Nurrohman
type: Quote

Page content goes here.

The custom type value in the example above will not give any effects unless you set a hook to the page content based on it:

Hook::set('page.content', function($content, $lot) {
    // `type` data does not exist or has the value other than “Quote”, skip…
    if (!isset($lot['type']) || $lot['type'] !== 'Quote') {
        return $content;
    // Remove all HTML block tag(s) and normalize line-break
    $content = n(To::text($content, HTML_WISE_I, true));
    // Convert line-break into paragraph tag(s)
    $content = '<p>' . str_replace(["\n\n", "\n"], ['</p><p>', '<br>'], $content) . '</p>';
    // Surround the page content with `<blockquote>` tag
    return '<blockquote>' . $content . '</blockquote>';

You can also add the post format support by customizing the page.php file instead of hooking the page content:

<?php Shield::get('header'); ?>
<!-- quote page template -->
<?php if ($page->type === 'Quote'): ?>
<article class="page" id="page-<?php echo $page->id; ?>">

  $content = n(To::text($page->content, HTML_WISE_I, true));
  $content = '<p>' . str_replace(["\n\n", "\n"], ['</p><p>', '<br>'], $content) . '</p>';

  echo $content;

<!-- default page template -->
<?php else: ?>
<article class="page" id="page-<?php echo $page->id; ?>">
  <header class="page-header">
    <h3 class="page-title">
      <?php echo $page->title; ?>
  <section class="page-body">
    <?php echo $page->content; ?>
  <footer class="page-footer">
    <p><?php echo $language->author . ': ' . $page->author; ?></p>
<?php endif; ?>
<?php Shield::get('footer'); ?>

If you have panel extension installed, you can also insert this snippet to enable Quote option in the Type field:

Config::set('', 'Quote');

Or, add a Quote property to the o_type language item:

  Markdown: Markdown
  Quote: Quote # ← this




No comments yet.