Skip to content

English plugin dev 1 2

goodpic edited this page Nov 7, 2011 · 9 revisions

Extending the Registry using YAML

Introduction

The registry lies in the heart of Movable Type, and we use YAML-formatted file to extend the registry and through it to extend Movable Type

Introduction to the Registry

The registry is a mechanism for maintaining a hierarchical internal structure of Movable Type. Similar to the MS Windows registry and in the sense that it maintains a hierarchical configuration.

Take a look at MT::Core ($MT_DIR/lib/MT/Core.pm). There you will see all the default values and core properties of Movable Type. You don’t need to read it all right now, but when in doubt this is the authoritative reference

BEGIN {
    $core_registry = {
        version        => MT->VERSION,
        schema_version => MT->schema_version,
        object_drivers => {
            'mysql' => {
                label          => 'MySQL Database (Recommended)',
                dbd_package    => 'DBD::mysql',
                config_package => 'DBI::mysql',
                display =>
                    [ 'dbserver', 'dbname', 'dbuser', 'dbpass', 'dbport', 'dbsocket' ],
            },
(...)
        applications      => {
            'xmlrpc'   => { handler => 'MT::XMLRPCServer', },
            'atom'     => { handler => 'MT::AtomServer', },
            'feeds'    => { handler => 'MT::App::ActivityFeeds', },
            'view'     => { handler => 'MT::App::Viewer', },
            'notify'   => { handler => 'MT::App::NotifyList', },
            'tb'       => { handler => 'MT::App::Trackback', },
            'upgrade'  => { handler => 'MT::App::Upgrade', },
            'wizard'   => { handler => 'MT::App::Wizard', },
            'comments' => {
                handler => 'MT::App::Comments',
                tags => sub { MT->app->load_core_tags },
            },
            'search'   => {
                handler => 'MT::App::Search::Legacy',
                tags => sub { MT->app->load_core_tags },
            },

And it contains a great number of other things. For example, the default menu of the CMS application (contained in MT::App::CMS or $MT_DIR/lib/MT/App/CMS.pm) if defined inside the functions core_menus and core_compose_menus:

sub core_menus {
    my $app = shift;
    return {
        'website' => {
            label => "Websites",
            order => 50,
        },
        'blog' => {
            label => "Blogs",
            order => 100,
        },
        'entry' => {
            label => "Entries",
            order => 200,
        },
(...)
        'tools:do_export_theme' => {
            order      => 10000,
            mode       => 'do_export_theme',
            view       => [ 'blog', 'website' ],
            display    => 0,
        },
        'tools:backup' => {
            order      => 10000,
            mode       => 'backup',
            view       => [ "blog", 'website', 'system' ],
            display    => 0,
        },
    };
}
sub core_compose_menus {
    my $app = shift;

    return {
        compose_menus => {
            label => 'Create New',
            order => 100,
            menus => {
                'entry' => {
                    label      => "Entry",
                    order      => 100,
                    mode       => 'view',
                    args       => { _type => 'entry' },
                    permission => 'create_post',
                    view       => "blog",
                },
(...)
                'blog:create' => {
                    label         => "Blog",
                    order         => 400,
                    mode          => 'view',
                    args          => { _type => 'blog' },
                    permit_action => 'use_blog:create_menu',
                    view          => "website",
                },
            },
        },
    };
}

To change the display settings and plug-ins, add to or overwrite the registry entries. You can customize Movable Type by it.

YAML?! what the hack is YAML?

YAML (short for “YAML Ain’t a Markup Language”) is a format to describe data and structure of the data in a hierarchical way. Similar to XML, but much less verbose and is actually humanly readable. compare the two samples below, one with XML and one with YAML, that contain the same data:

XML

<xml version="1.0">
<address>
  <first_name>Taro</first_name>
  <last_name>YAMADA</last_name>
  <email>ytaro@example.com</email>
  <company>
    <name>Six Apart Ltd.</name>
    <street_address>
      5-2-39 Akasaka, Entsuji-Gadelius Bldg. 7F, Minato-ku, Tokyo, 107-0052, Japan
    </street_address>
  </company>
</address>
YAML

address:
    first_name: Taro
    last_name: YAMADA
    email: ytaro@example.com
    company:
        name: Six Apart Ltd.
        street_address: >
                        5-2-39 Akasaka, Entsuji-Gadelius Bldg. 7F,
                        Minato-ku, Tokyo, 107-0052, Japan

Movable Type is using YAML in the development of plug-ins and theming (using config.yaml, theme.yaml, etc.)

id: MyPlugin01
name: Sample plugin registration
version: 1.0
description: Sample plugin registration
author_name: Plugin author
author_link: http://www.example.com/about/
doc_link: http://www.example.com/docs/

The Syntax of YAML

Writing YAML is very simple. just note that the indentation is important and need to be aligned exactly

Indentation (usually two spaces, but can be four or more. TAB is not allowed)

parent:
    child:
        grandchild: hoge

Example of bad indentation: foo and child are in the same level, so they should have been in the same indent level. so are grandchild and bar.

parent:
    child:
        grandchild: hoge
  foo:
    bar: foobar

Array

- foo
- bar

Hash table

child01: foo
child02: bar

Multi-line string – keep the newlines

description: |
        StyleCatcher lets you easily browse through styles and
        then apply them to your blog in just a few clicks.


=>
StyleCatcher lets you easily browse through styles and
then apply them to your blog in just a few clicks.

Multi-line string – but remove the newlines

description: >
        StyleCatcher lets you easily browse through styles and
        then apply them to your blog in just a few clicks.

=>
StyleCatcher lets you easily browse through styles and then apply them to your blog in just a few clicks.

Summary

It is not necessary to be a YAML expert to develop Movable Type plugins, and these basics will help you with most of the things that you need. You will learn more about the Movable Type registry system later in this tutorial, so let’t continue to the next chapter !

Navigation

Last:First step in developing plug-ins << Index >> Next:Adding configuration directives

Clone this wiki locally