[%# # IMPORTANT NOTE # This documentation is generated automatically from source # templates. Any changes you make here may be lost. # # The 'docsrc' documentation source bundle is available for download # from http://www.template-toolkit.org/docs.html and contains all # the source templates, XML files, scripts, etc., from which the # documentation for the Template Toolkit is built. -%] [% META book = 'Modules' page = 'Plugin_XML_Style' %] [% WRAPPER toc; PROCESS tocitem title ="SYNOPSIS" subs = []; PROCESS tocitem title ="DESCRIPTION" subs = []; PROCESS tocitem title ="AUTHOR" subs = []; PROCESS tocitem title ="VERSION" subs = []; PROCESS tocitem title ="COPYRIGHT" subs = []; PROCESS tocitem title ="SEE ALSO" subs = []; END %] [% WRAPPER section title="SYNOPSIS" -%]

        [% tt_start_tag %] USE xmlstyle
               table = {
                   attributes = {
                       border      = 0
                       cellpadding = 4
                       cellspacing = 1
                   }
               }
        [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle [% tt_end_tag %]
        <table>
        <tr>
          <td>Foo</td> <td>Bar</td> <td>Baz</td>
        </tr>
        </table>
        [% tt_start_tag %] END [% tt_end_tag %]

[%- END %] [% WRAPPER section title="DESCRIPTION" -%]

This plugin defines a filter for performing simple stylesheet based transformations of XML text.

Named parameters are used to define those XML elements which require transformation. These may be specified with the USE directive when the plugin is loaded and/or with the FILTER directive when the plugin is used.

This example shows how the default attributes `'border="0"'` and `'cellpadding="4"'` can be added to <table> elements.

        [% tt_start_tag %] USE xmlstyle
               table = {
                   attributes = {
                       border      = 0
                       cellpadding = 4
                   }
               }
        [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle [% tt_end_tag %]
        <table>
           ...
        </table>
        [% tt_start_tag %] END [% tt_end_tag %]

This produces the output:

        <table border="0" cellpadding="4">
           ...
        </table>

Parameters specified within the USE directive are applied automatically each time the `'xmlstyle'` FILTER is used. Additional parameters passed to the FILTER directive apply for only that block.

        [% tt_start_tag %] USE xmlstyle
               table = {
                   attributes = {
                       border      = 0
                       cellpadding = 4
                   }
               }
        [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle
               tr = {
                   attributes = {
                       valign="top"
                   }
               }
        [% tt_end_tag %]
        <table>
           <tr>
             ...
           </tr>
        </table>
        [% tt_start_tag %] END [% tt_end_tag %]

Of course, you may prefer to define your stylesheet structures once and simply reference them by name. Passing a hash reference of named parameters is just the same as specifying the named parameters as far as the Template Toolkit is concerned.

        [% tt_start_tag %] style_one = {
              table = { ... }
              tr    = { ... }
           }
           style_two = {
              table = { ... }
              td    = { ... }
           }
           style_three = {
              th = { ... }
              tv = { ... }
           }
        [% tt_end_tag %]

        [% tt_start_tag %] USE xmlstyle style_one [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle style_two [% tt_end_tag %]
           # style_one and style_two applied here
        [% tt_start_tag %] END [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle style_three [% tt_end_tag %]
           # style_one and style_three applied here
        [% tt_start_tag %] END [% tt_end_tag %]

Any attributes defined within the source tags will override those specified in the style sheet.

        [% tt_start_tag %] USE xmlstyle
               div = { attributes = { align = 'left' } }
        [% tt_end_tag %]

        [% tt_start_tag %] FILTER xmlstyle [% tt_end_tag %]
        <div>foo</div>
        <div align="right">bar</div>
        [% tt_start_tag %] END [% tt_end_tag %]

The output produced is:

        <div align="left">foo</div>
        <div align="right">bar</div>

The filter can also be used to change the element from one type to another.

        [% tt_start_tag %] FILTER xmlstyle
                  th = {
                      element = 'td'
                      attributes = { bgcolor='red' }
                  }
        [% tt_end_tag %]
        <tr>
          <th>Heading</th>
        </tr>
        <tr>
          <td>Value</td>
        </tr>
        [% tt_start_tag %] END [% tt_end_tag %]

The output here is as follows. Notice how the end tag `'</th>'` is changed to `'</td>'` as well as the start tag.

        <tr>
          <td bgcolor="red">Heading</td>
        </tr>
        <tr>
          <td>Value</td>
        </tr>

You can also define text to be added immediately before or after the start or end tags. For example:

        [% tt_start_tag %] FILTER xmlstyle
                  table = {
                      pre_start = '<div align="center">'
                      post_end  = '</div>'
                  }
                  th = {
                      element    = 'td'
                      attributes = { bgcolor='red' }
                      post_start = '<b>'
                      pre_end    = '</b>'
                  }
        [% tt_end_tag %]
        <table>
        <tr>
          <th>Heading</th>
        </tr>
        <tr>
          <td>Value</td>
        </tr>
        </table>
        [% tt_start_tag %] END [% tt_end_tag %]

The output produced is:

        <div align="center">
        <table>
        <tr>
          <td bgcolor="red"><b>Heading</b></td>
        </tr>
        <tr>
          <td>Value</td>
        </tr>
        </table>
        </div>

[%- END %] [% WRAPPER section title="AUTHOR" -%]

Andy Wardley <abw@andywardley.com>

[% ttlink('http://www.andywardley.com/', 'http://www.andywardley.com/') -%]

[%- END %] [% WRAPPER section title="VERSION" -%]

2.35, distributed as part of the Template Toolkit version 2.14, released on 04 October 2004.

[%- END %] [% WRAPPER section title="COPYRIGHT" -%]

      Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
      Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

[%- END %] [% WRAPPER section title="SEE ALSO" -%]

[% ttlink('Template::Plugin', 'Template::Plugin') -%]

[%- END %]