Changeset 220

Show
Ignore:
Timestamp:
03/21/07 11:16:30 (2 years ago)
Author:
jallen
Message:

Update to HelloWorld? plugin bringing it up to modern standards. Still needs documentation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/HelloWorld/package.xml

    r118 r220  
    11<?xml version="1.0"?> 
    2 <plugin version="0.01"> 
     2<plugin version="0.5"> 
    33    <name>Hello World</name> 
    44    <version_url>http://code.sixapart.com/svn/mtplugins/trunk/HelloWorld/version.xml</version_url> 
  • trunk/HelloWorld/plugins/HelloWorld/HelloWorld.pl

    r160 r220  
    11package MT::Plugin::HelloWorld; 
    22# 
    3 # This code is transcribed from "Hacking Movable Type" by Allen, 
    4 # Choate, Hammersley and Haughey, chapter 10: Writing Plugins with 
    5 # slight modifications by Gene Boggs.  If you spot an inaccuracy 
    6 # or improvement in the context of The Book or PluginManager 
    7 # requirements, patches are welcome! 
     3# This code is a modified version of that listed in "Hacking Movable 
     4# Type" by Allen, Choate, Hammersley and Haughey, chapter 10: Writing 
     5# Plugins.  Gene Boggs added the plugin manager compatiblity files. 
    86# 
    9 # $Id$ 
    10  
    11 $VERSION = '0.01'; 
     7# If you spot an inaccuracy or improvement in the context of The Book 
     8# or PluginManager requirements, patches are welcome! 
     9
    1210use strict; 
    1311use warnings; 
     
    1715use MT::Template::Context; 
    1816 
    19 {   # Main block of executed code: 
    20     my $name = 'HelloWorld'; 
    21     my $plugin = MT::Plugin->new(); 
    22     $plugin->name($name); 
    23     $plugin->description(__PACKAGE__); 
    24     MT->add_plugin($plugin); 
    25     MT::Template::Context->add_tag($name => \&HelloWorld); 
    26     MT::Template::Context->add_container_tag( 
    27         $name .'Container' => \&HelloWorldContainer, 
    28     ); 
    29     MT::Template::Context->add_conditional_tag( 
    30         $name .'IfNameProvided' => \&HelloWorldIfNameProvided, 
    31     ); 
    32     MT::Template::Context->add_global_filter( 
    33         to_uppercase => \&to_uppercase, 
    34     ); 
    35 
     17use base 'MT::Plugin'; 
     18 
     19our $plugin; 
     20our $VERSION = '0.5'; 
     21MT->add_plugin($plugin = __PACKAGE__->new({ 
     22        name            => "Hello World", 
     23        version         => $VERSION, 
     24        description     => "Enables friendly greetings.", 
     25        author_name     => "Ima Hacker", 
     26        author_link     => "http://example.com/", 
     27    template_tags => { 
     28        'HelloWorld' => \&HelloWorld, 
     29        'HelloWorldGreeted' => \&HelloWorldGreeted, 
     30    }, 
     31    container_tags => { 
     32        'HelloWorldContainer' => \&HelloWorldContainer, 
     33    }, 
     34    conditional_tags => { 
     35        'HelloWorldIfNameProvided' => \&HelloWorldIfNameProvided, 
     36    }, 
     37    global_filters => { 
     38        'to_uppercase' => \&to_uppercase, 
     39    }, 
     40})); 
     41 
    3642 
    3743sub HelloWorld { 
     
    5460    } 
    5561 
    56     my $glue = $args->{glue} || ','; 
     62    my $glue = $args->{glue} || '[\s,]+'; 
    5763    my @names = split /$glue/, $name; 
    5864 
     
    6975sub HelloWorldGreeted { 
    7076    my($ctx, $args) = @_; 
    71     my $out = $ctx->stash('HelloWorldGreeted') || $ctx->error( 
    72         'MTHelloWorldGreeted must be used in a MTHelloWorldContainer element.' 
    73     ); 
     77    my $out = $ctx->stash('HelloWorldGreeted')  
     78           || $ctx->error('MTHelloWorldGreeted must be used in a '. 
     79                          'MTHelloWorldContainer element.'); 
    7480    return $out; 
    7581} 
     
    8894sub _get_name { 
    8995    my($ctx, $args) = @_; 
    90     return $args->{name} || ( 
    91            $ctx->stash('entry') && 
    92            $ctx->stash('entry')->author && 
    93            $ctx->stash('entry')->author->name 
    94     ) ? $ctx->stash('entry')->author->name : 'world'; 
    95 
    96  
    97 1; 
     96    my $e = $ctx->stash('entry'); 
     97 
     98    return  $args and defined $args->{name}      ?  $args->{name} 
     99        :   $e && $e->author && $e->author->name ?  $e->author->name 
     100        :                                           'world'; 
     101     
     102
    98103 
    99104__END__ 
     
    137142=back 
    138143 
     144=head1 EXAMPLES 
     145 
     146    ### To all of you 
     147 
     148        <$MTHelloWorld$> 
     149 
     150    ### Dirified 
     151 
     152        <$MTHelloWorld dirify="1"$> 
     153 
     154 
     155    ### Showing excitement with a global attribute! 
     156 
     157        <$MTHelloWorld to_uppercase="1"$> 
     158 
     159    ### Hi Morty! 
     160 
     161        <$MTHelloWorld name="Mortimer"$> 
     162 
     163    ### Containers 
     164 
     165    <MTHelloWorldContainer name="Byrne, Anil, Byron, Jay, world"> 
     166        Hello <$MTHelloWorldGreeted$>! 
     167    </MTHelloWorldContainer> 
     168 
     169    ### Containers with a conditional 
     170 
     171    <MTHelloWorldContainer name="Byrne, Anil, Byron, Jay, world"> 
     172      <MTHelloWorldIfNameProvided> 
     173        Hello <$MTHelloWorldGreeted$>! Nice to see you! 
     174      <MTElse> 
     175        Oh, it's you again, world.  Hi. 
     176      </MTElse> 
     177      </MTHelloWorldIfNameProvided> 
     178    </MTHelloWorldContainer> 
     179 
     180    ### In an entries loop 
     181 
     182    <MTEntries> 
     183        <$MTHelloWorld$>  Thanks for writing this entry. 
     184    </MTEntries> 
     185 
     186    ### Container inside of entry without tag 
     187 
     188    <MTEntries> 
     189    <MTHelloWorldContainer> 
     190        Hello <$MTHelloWorldGreeted$>! 
     191    </MTHelloWorldContainer> 
     192    </MTEntries> 
     193 
     194    ### Container inside of entry with tag as name variable 
     195 
     196    <MTEntries> 
     197    <MTHelloWorldContainer name="[MTEntryTags glue=','][MTTagName][/MTEntryTags]"> 
     198        This entry is tagged: <$MTHelloWorldGreeted$> 
     199    </MTHelloWorldContainer> 
     200    </MTEntries> 
     201 
    139202=head1 SEE ALSO 
    140203 
  • trunk/HelloWorld/version.xml

    r118 r220  
    22<plugin xmlns="http://code.sixapart.com/svn/mtplugins/trunk/HelloWorld/schema.xml"> 
    33    <name>Hello World</name> 
    4     <version>0.01</version> 
     4    <version>0.5</version> 
    55    <download_url>http://code.sixapart.com/dists/HelloWorld.tgz</download_url> 
    66</plugin>