Post Format

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.

WordPress

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 to it:

---
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) {
    // `type` data does not exist or has the value other than “Quote”, skip…
    if (!$this->type || 'Quote' !== $this->type) {
        return $content;
    }
    // Remove all HTML block tag(s) and normalize line-break
    $tags = 'a,abbr,b,br,cite,code,del,dfn,em,i,ins,kbd,mark,q,span,strong,sub,sup,time,u,var';
    $content = n(To::text($content, $tags, true));
    // Convert line-break into paragraph tag(s)
    $content = '<p>' . strtr($content, [
        "\n\n" => '</p><p>',
        "\n" => '<br>'
    ]) . '</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:

<?= self::before(); ?>
<!-- Quote page layout -->
<?php if ('Quote' === $page->type): ?>
<article id="page-<?= $page->id; ?>">
  <blockquote>
  <?php

  $tags = 'a,abbr,b,br,cite,code,del,dfn,em,i,ins,kbd,mark,q,span,strong,sub,sup,time,u,var';
  $content = n(To::text($content, $tags, true));
  $content = '<p>' . strtr($content, [
      "\n\n" => '</p><p>',
      "\n" => '<br>'
  ]) . '</p>';

  echo $content;

  ?>
  </blockquote>
</article>
<!-- Default page layout -->
<?php else: ?>
<article id="page-<?= $page->id; ?>">
  <header>
    <h3><?= $page->title; ?></h3>
  </header>
  <div><?= $page->content; ?></div>
  <footer>
    <p><?= i('Author') . ': ' . $page->author; ?></p>
  </footer>
</article>
<?php endif; ?>
<?= self::after(); ?>

0 Comments

No comments yet.