Templa...:Style(3pm)

πŸ“¦
version 2.17 Β· 2 versions
  • 2.17 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 2.17 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Okay so bestie, welcome to the page for Template::Plugin::XML::Style β€” she does simple XML stylesheet transfomations ✨ (yes that's how it's spelled up top, we love her exactly as she is πŸ’…).

And for the receipts: this lil beauty lives in the User Contributed Perl Documentation, section 3pm, and the page header is literally Templa...:Style(3pm). Iconic. Short. Mysterious. We stan a truncated title πŸŽ€


🌸 SYNOPSIS 🌸

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

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

✨ DESCRIPTION ✨

So this plugin defines a filter that does simple, stylesheet-based transformations of XML text. That's the whole vibe. It's giving "CSS but make it Template Toolkit" πŸ¦‹

πŸŽ€ How you tell it what to do

You use named parameters to define which XML elements need transforming. You can specify them in two places:

  • with the USE directive, when the plugin gets loaded, and/or
  • with the FILTER directive, when the plugin actually gets used.

Both. Either. Whatever your heart says πŸ’–

🧚 Adding attributes (the glow-up example)

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

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

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

And this produces the output:

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

Look at that glow-up 😭✨

πŸ’… USE params vs FILTER params (the important lore)

Parameters specified within the USE directive are applied automatically every single time the xmlstyle FILTER is used. They're the forever-girlfriend settings 🫢

Additional parameters passed to the FILTER directive apply for only that block. Situationship params. Temporary. Just for that moment πŸ™ˆ

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

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

πŸ‘‘ Naming your styles (reusable fit checks)

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. Same energy, less typing, main character behavior 🌈

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

[% USE xmlstyle style_one %]

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

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

πŸ”₯ Source tags win (no cap)

Any attributes defined within the source tags will override those specified in the style sheet. The source is the boss. Periodt.

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

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

The output produced is:

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

See? The one with its own align="right" kept it. She said "I already have one, thanks" πŸ’€

πŸ¦‹ Changing an element into a whole different element

The filter can also be used to change the element from one type to another. Full transformation arc, honestly.

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

The output here is as follows. Notice how the end tag </th> is changed to </td> as well as the start tag. Both tags got the memo, bestie β€” nobody's getting left behind 🫢

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

🌟 Injecting raw text around tags

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

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

The output produced is:

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

Four little knobs β€” pre_start, post_start, pre_end, post_end β€” and suddenly your markup is wearing a whole outfit πŸ’…βœ¨


πŸ’œ AUTHOR πŸ’œ

Andy Wardley


Copyright (C) 2001-2006 Andy Wardley. All Rights Reserved.

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


🌈 SEE ALSO 🌈

  • Template::Plugin

And that's the whole page, bestie πŸ’– This one shipped as part of perl v5.34.0, dated 2022-07-04 β€” so go forth, filter your XML, and slay βœ¨πŸ‘‘πŸ¦‹

πŸ“¦ where this page comes from