Given how central MARC is to Koha, it would be useful to be able to display MARC records directly in Template::Toolkit.
Created attachment 13863 [details] [review] Bug 9202: Add a MARC plugin for Template::Toolkit A Template::Toolkit plugin which given a MARC::Record object parses it into a hash that can be accessed directly in Template::Toolkit. === SYNOPSIS === [% USE record = MARC(mymarc) %] <!-- translate MARC::Record to T::T hash --> <h1>[% record.f245.sa %]</h1> <!-- subfield 245$a --> [% record.f245.all %] <!-- all subfields concatenated together --> [% FOREACH link IN record.f856s %] <!-- process each 856 field --> <a href="whatever/[% link.su %]">[% link.sy %]</a> <!-- create a link on 856$y --> [% END %] <!-- /FOREACH link IN record.856s --> [% FOREACH contents IN record.f505s %] <!-- process each 505 field --> [% FOREACH subf IN contents.subfields %] <!-- process each subfield --> [% SWITCH subf.code %] [% CASE 'a' %] <span class='contents'>[% subf.value %]</span> [% CASE 't' %] <span class='title'>[% subf.value %]</span> [% CASE 'r' %] <span class='responsibility'>[% subf.value %]</span> [% END %] [% END %] <!-- /FOREACH contents.subfields --> [% END %] <!-- /FOREACH contents IN record.f505s --> [% FOREACH subj IN record.f6xxs %] <a href="whatever/[% subj.s9 %]">[% subj.sa %]</a> <!-- create a link on 6[0-9]{2}$a --> [% END %] [% FOREACH field IN record.fields %] [% SWITCH field.tag %] [% CASE '600' %] Subject: [% field.all %] is what we are all about [% CASE '700' %] Co-author: [% field.all %], I presume? [% END %] [% END %] === ACCESSORS === By using some clever AUTOLOAD acrobatics, this plugin offers the user six types of accessors. ==== Direct accessors ==== [% record.f245.sa %] print $record->f245->sa; By prefixing field numbers with an 'f' and subfield codes with an 's', the first field/subfield with a given tag/code can be accessed. ==== Concatenated accessors ==== [% record.f245.all %] print $record->f245->all; A string consisting of all subfields concatenated together is accessible through the all member of field objects. ==== Subfield iterators ==== [% FOREACH subfield IN record.f245.subfields %] [% subfield.code %] = [% subfield.value %] [% END %] foreach my $subfield ($record->f245) { print $subfield->code, ' = ', $subfield->value; } Subfield iterators are accessible through the subfields member of field objects. ==== Field iterators ==== [% FOREACH field IN record.f500s %] [% field.all %] [% END %] foreach my $field ($record->f500s) { print $field->all; } Field iterators are accessible by adding an 's' to the end of field names: f500s, etc. ==== Section iterators ==== [% FOREACH field IN record.f5xxs %] [% field.all %] [% END %] foreach my $field ($record->f5xxs) { print $field->all; } All the fields in a section (identified by the first digit of the tag) can be accessed with 'fNxxs' and then iterated through. ==== Complete field list ==== [% FOREACH field IN record.fields %] [% field.all %] [% END %] foreach my $field ($record->fields) { print $field->all; } All the fields in a record can be accessed via the fields object method.
09:24 ~/kohaclone ((0573727...) $%)$ perl t/Template_Plugin_MARC.t 1..21 not ok 1 - use Koha::Template::Plugin::MARC; # Failed test 'use Koha::Template::Plugin::MARC;' # at t/Template_Plugin_MARC.t line 25. # Tried to use 'Koha::Template::Plugin::MARC'. # Error: Type of arg 1 to push must be array (not hash element) at /home/koha/kohaclone/Koha/Template/Plugin/MARC.pm line 225, near "$fieldobj;" # Type of arg 1 to push must be array (not hash element) at /home/koha/kohaclone/Koha/Template/Plugin/MARC.pm line 227, near "$fieldobj;" # Type of arg 1 to push must be array (not hash element) at /home/koha/kohaclone/Koha/Template/Plugin/MARC.pm line 228, near "$fieldobj;" # BEGIN not safe after errors--compilation aborted at /home/koha/kohaclone/Koha/Template/Plugin/MARC.pm line 296. # Compilation failed in require at (eval 12) line 2. # BEGIN failed--compilation aborted at (eval 12) line 2. ok 2 - load method returns correct class name ok 3 - Created expected object Undefined subroutine &Koha::Template::Plugin::MARC::marc called at t/Template_Plugin_MARC.t line 52. # Looks like you planned 21 tests but ran 3. # Looks like you failed 1 test of 3 run. # Looks like your test exited with 255 just after 3.
Created attachment 13949 [details] [review] Bug 9202: Add a MARC plugin for Template::Toolkit A Template::Toolkit plugin which given a MARC::Record object parses it into a hash that can be accessed directly in Template::Toolkit. === SYNOPSIS === [% USE record = MARC(mymarc) %] <!-- translate MARC::Record to T::T hash --> <h1>[% record.f245.sa %]</h1> <!-- subfield 245$a --> [% record.f245.all %] <!-- all subfields concatenated together --> [% FOREACH link IN record.f856s %] <!-- process each 856 field --> <a href="whatever/[% link.su %]">[% link.sy %]</a> <!-- create a link on 856$y --> [% END %] <!-- /FOREACH link IN record.856s --> [% FOREACH contents IN record.f505s %] <!-- process each 505 field --> [% FOREACH subf IN contents.subfields %] <!-- process each subfield --> [% SWITCH subf.code %] [% CASE 'a' %] <span class='contents'>[% subf.value %]</span> [% CASE 't' %] <span class='title'>[% subf.value %]</span> [% CASE 'r' %] <span class='responsibility'>[% subf.value %]</span> [% END %] [% END %] <!-- /FOREACH contents.subfields --> [% END %] <!-- /FOREACH contents IN record.f505s --> [% FOREACH subj IN record.f6xxs %] <a href="whatever/[% subj.s9 %]">[% subj.sa %]</a> <!-- create a link on 6[0-9]{2}$a --> [% END %] [% FOREACH field IN record.fields %] [% SWITCH field.tag %] [% CASE '600' %] Subject: [% field.all %] is what we are all about [% CASE '700' %] Co-author: [% field.all %], I presume? [% END %] [% END %] === ACCESSORS === By using some clever AUTOLOAD acrobatics, this plugin offers the user six types of accessors. ==== Direct accessors ==== [% record.f245.sa %] print $record->f245->sa; By prefixing field numbers with an 'f' and subfield codes with an 's', the first field/subfield with a given tag/code can be accessed. ==== Concatenated accessors ==== [% record.f245.all %] print $record->f245->all; A string consisting of all subfields concatenated together is accessible through the all member of field objects. ==== Subfield iterators ==== [% FOREACH subfield IN record.f245.subfields %] [% subfield.code %] = [% subfield.value %] [% END %] foreach my $subfield ($record->f245) { print $subfield->code, ' = ', $subfield->value; } Subfield iterators are accessible through the subfields member of field objects. ==== Field iterators ==== [% FOREACH field IN record.f500s %] [% field.all %] [% END %] foreach my $field ($record->f500s) { print $field->all; } Field iterators are accessible by adding an 's' to the end of field names: f500s, etc. ==== Section iterators ==== [% FOREACH field IN record.f5xxs %] [% field.all %] [% END %] foreach my $field ($record->f5xxs) { print $field->all; } All the fields in a section (identified by the first digit of the tag) can be accessed with 'fNxxs' and then iterated through. ==== Complete field list ==== [% FOREACH field IN record.fields %] [% field.all %] [% END %] foreach my $field ($record->fields) { print $field->all; } All the fields in a record can be accessed via the fields object method.
Created attachment 13950 [details] [review] Bug 9202: Add a MARC plugin for Template::Toolkit A Template::Toolkit plugin which given a MARC::Record object parses it into a hash that can be accessed directly in Template::Toolkit. === SYNOPSIS === [% USE record = MARC(mymarc) %] <!-- translate MARC::Record to T::T hash --> <h1>[% record.f245.sa %]</h1> <!-- subfield 245$a --> [% record.f245.all %] <!-- all subfields concatenated together --> [% FOREACH link IN record.f856s %] <!-- process each 856 field --> <a href="whatever/[% link.su %]">[% link.sy %]</a> <!-- create a link on 856$y --> [% END %] <!-- /FOREACH link IN record.856s --> [% FOREACH contents IN record.f505s %] <!-- process each 505 field --> [% FOREACH subf IN contents.subfields %] <!-- process each subfield --> [% SWITCH subf.code %] [% CASE 'a' %] <span class='contents'>[% subf.value %]</span> [% CASE 't' %] <span class='title'>[% subf.value %]</span> [% CASE 'r' %] <span class='responsibility'>[% subf.value %]</span> [% END %] [% END %] <!-- /FOREACH contents.subfields --> [% END %] <!-- /FOREACH contents IN record.f505s --> [% FOREACH subj IN record.f6xxs %] <a href="whatever/[% subj.s9 %]">[% subj.sa %]</a> <!-- create a link on 6[0-9]{2}$a --> [% END %] [% FOREACH field IN record.fields %] [% SWITCH field.tag %] [% CASE '600' %] Subject: [% field.all %] is what we are all about [% CASE '700' %] Co-author: [% field.all %], I presume? [% END %] [% END %] === ACCESSORS === By using some clever AUTOLOAD acrobatics, this plugin offers the user six types of accessors. ==== Direct accessors ==== [% record.f245.sa %] print $record->f245->sa; By prefixing field numbers with an 'f' and subfield codes with an 's', the first field/subfield with a given tag/code can be accessed. ==== Concatenated accessors ==== [% record.f245.all %] print $record->f245->all; A string consisting of all subfields concatenated together is accessible through the all member of field objects. ==== Subfield iterators ==== [% FOREACH subfield IN record.f245.subfields %] [% subfield.code %] = [% subfield.value %] [% END %] foreach my $subfield ($record->f245) { print $subfield->code, ' = ', $subfield->value; } Subfield iterators are accessible through the subfields member of field objects. ==== Field iterators ==== [% FOREACH field IN record.f500s %] [% field.all %] [% END %] foreach my $field ($record->f500s) { print $field->all; } Field iterators are accessible by adding an 's' to the end of field names: f500s, etc. ==== Section iterators ==== [% FOREACH field IN record.f5xxs %] [% field.all %] [% END %] foreach my $field ($record->f5xxs) { print $field->all; } All the fields in a section (identified by the first digit of the tag) can be accessed with 'fNxxs' and then iterated through. ==== Complete field list ==== [% FOREACH field IN record.fields %] [% field.all %] [% END %] foreach my $field ($record->fields) { print $field->all; } All the fields in a record can be accessed via the fields object method.
Created attachment 13964 [details] [review] Bug 9202: Add a MARC plugin for Template::Toolkit A Template::Toolkit plugin which given a MARC::Record object parses it into a hash that can be accessed directly in Template::Toolkit. === SYNOPSIS === [% USE record = MARC(mymarc) %] <!-- translate MARC::Record to T::T hash --> <h1>[% record.f245.sa %]</h1> <!-- subfield 245$a --> [% record.f245.all %] <!-- all subfields concatenated together --> [% FOREACH link IN record.f856s %] <!-- process each 856 field --> <a href="whatever/[% link.su %]">[% link.sy %]</a> <!-- create a link on 856$y --> [% END %] <!-- /FOREACH link IN record.856s --> [% FOREACH contents IN record.f505s %] <!-- process each 505 field --> [% FOREACH subf IN contents.subfields %] <!-- process each subfield --> [% SWITCH subf.code %] [% CASE 'a' %] <span class='contents'>[% subf.value %]</span> [% CASE 't' %] <span class='title'>[% subf.value %]</span> [% CASE 'r' %] <span class='responsibility'>[% subf.value %]</span> [% END %] [% END %] <!-- /FOREACH contents.subfields --> [% END %] <!-- /FOREACH contents IN record.f505s --> [% FOREACH subj IN record.f6xxs %] <a href="whatever/[% subj.s9 %]">[% subj.sa %]</a> <!-- create a link on 6[0-9]{2}$a --> [% END %] [% FOREACH field IN record.fields %] [% SWITCH field.tag %] [% CASE '600' %] Subject: [% field.all %] is what we are all about [% CASE '700' %] Co-author: [% field.all %], I presume? [% END %] [% END %] === ACCESSORS === By using some clever AUTOLOAD acrobatics, this plugin offers the user six types of accessors. ==== Direct accessors ==== [% record.f245.sa %] print $record->f245->sa; By prefixing field numbers with an 'f' and subfield codes with an 's', the first field/subfield with a given tag/code can be accessed. ==== Concatenated accessors ==== [% record.f245.all %] print $record->f245->all; A string consisting of all subfields concatenated together is accessible through the all member of field objects. ==== Subfield iterators ==== [% FOREACH subfield IN record.f245.subfields %] [% subfield.code %] = [% subfield.value %] [% END %] foreach my $subfield ($record->f245) { print $subfield->code, ' = ', $subfield->value; } Subfield iterators are accessible through the subfields member of field objects. ==== Field iterators ==== [% FOREACH field IN record.f500s %] [% field.all %] [% END %] foreach my $field ($record->f500s) { print $field->all; } Field iterators are accessible by adding an 's' to the end of field names: f500s, etc. ==== Section iterators ==== [% FOREACH field IN record.f5xxs %] [% field.all %] [% END %] foreach my $field ($record->f5xxs) { print $field->all; } All the fields in a section (identified by the first digit of the tag) can be accessed with 'fNxxs' and then iterated through. ==== Complete field list ==== [% FOREACH field IN record.fields %] [% field.all %] [% END %] foreach my $field ($record->fields) { print $field->all; } All the fields in a record can be accessed via the fields object method. Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net>
After discussion with Chris and some folks on the #evergreen IRC channel, I have decided that this feature is better as a standalone CPAN module and not built directly into Koha. I am in the process of uploading it, and will package it for Debian as well.
Just for the record, the module is now on CPAN as Template::Plugin::MARC: http://search.cpan.org/dist/Template-Plugin-MARC/lib/Template/Plugin/MARC.pm