Gossamer Forum
Quote Reply
simple plugin
Hello,
I'm trying to understand how plugins work and I have been doing very simple plugin, but I'm totaly lost.

I want to add tag "display" to link.html page which will print URL of link (synonym for <%URL%>). But currently tag <%display%> generate
"Unknown Tag: 'display'".

Can you give me advice what I'm doing wrong? And yes, I was googling and searching this forum all this afternoon.

Miroslav Suchy

My Install.pm:
Code:
package Plugins::Test;
# ==================================================================
use strict;
use vars qw/$VERSION $DEBUG $NAME $META/;
use GT::Base;
use GT::Plugins qw/STOP CONTINUE/;
use Links qw/:objects/;

$VERSION = '1.0';
$DEBUG = 0;
$NAME = 'Test';
# Inhert from base class for debug and error methods
@Plugins::Test::ISA = qw(GT::Base);

$META = {
'author' => 'Miroslav Suchy',
'description' => '',
'license' => 'GPL',
'prog_ver' => '2.1.2',
'url' => '',
'version' => '1.0'
};


sub pre_install {
# -----------------------------------------------------------------------------
# This function displays an HTML formatted message that will display any
# instructions/information to the user before they install the plugin.
#
my $inst_msg = '';

return $inst_msg;
}

sub pre_uninstall {
# -----------------------------------------------------------------------------
# This function displays an HTML formatted message that will display any
# instructions/information to the user before they remove the plugin.
#
my $uninst_msg = '';

return $uninst_msg;
}

sub install {
# -----------------------------------------------------------------------------
# This function does the actual installation. Its first argument is a plugin
# manager which you can use to register hooks, install files, add menu options,
# etc. The second argument is a GT::Tar object which you can use to access any
# files in your plugin module.
#
# You should return an HTML formatted string that will be displayed to the
# user.
#
# If there is an error, return undef, and set the error message in
# $Plugins::Test::error
#
my ($mgr, $tar) = @_;

$mgr->install_hooks('Test', [['site_html_link', 'PRE', 'Plugins::Test::display', '']]);

return "The plugin has been successfully installed!";
}

sub uninstall {
# -----------------------------------------------------------------------------
# This function removes the plugin. Its first argument is also a plugin
# manager which you can use to register hooks, install files, add menu options,
# etc. You should return an HTML formatted string that will be displayed to the
# user.
#
# If there is an error, return undef, and set the error message in
# $Plugins::Thumbnail::error
#
my $mgr = shift;

$mgr->uninstall_hooks('Test', [['site_html_link', 'PRE', 'Plugins::Test::display', '']]);
;
return "The plugin has been successfully removed!";
}

1;

And Test.pm:
Code:
package Plugins::Test;
# ==================================================================

use strict;
use GT::Base;
use GT::Plugins qw/STOP CONTINUE/;
use Links qw/:objects/;

# Inherit from base class for debug and error methods
@Plugins::Test::ISA = qw(GT::Base);

# PLUGIN HOOKS
# ===================================================================


sub display {
# -----------------------------------------------------------------------------
# This subroutine will be called whenever the hook 'site_html_link' is run. You
# should call $PLG->action(STOP) if you don't want the regular
# 'site_html_link' code to run, otherwise the code will continue as normal.
#
my $args = shift;

return {'display' => $args->{'URL'} };
}

# Always end with a 1.
1;
Quote Reply
Re: [mirek] simple plugin In reply to
For what you're currently doing, you should be doing it as a global. You would want to use a plugin if you want to change the behaviour of the existing code. Your global would be something like:

Code:
sub {
my $vars = GT::Template->vars();
return { display => $vars->{URL} };
}

The GT::Template->vars() call returns all the variables available to the template, and returning a hash reference makes all the keys of the hash available to the template.

Adrian
Quote Reply
Re: [brewt] simple plugin In reply to
Quote:
For what you're currently doing, you should be doing it as a global.

I'm currently trying to understand how to make plugin. Yes, I know that this can be easily done with globals. But globals have one big disadvantage (as far as I know): You can not pack it and distribute it to customers. So I'm trying write plugin, but do not know how to add new tag. According to GT::Template::Tutorial it can just return hashref.

Can somebody show me example of simple plugin which add new tag to the page? Or point me to existing plugin, which do it?
Quote Reply
Re: [mirek] simple plugin In reply to
Writing a plugin to add variables to a page is very inefficient and isn't possible to do for all hooks.

In Reply To:
According to GT::Template::Tutorial it can just return hashref.
You can for a global, but you can't for a plugin hook because your plugin hook function is supposed to return what the hooked function is supposed to return.

Adrian
Quote Reply
Re: [brewt] simple plugin In reply to
I haven't looked into it, but maybe he could write a plugin that adds a global to the globals file on install (and does nothing else?).

Unimpressed

regan