The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <title>ClearPress Framework</title>
  <link rel="stylesheet" type="text/css" href="clearpress.css"/>
</head>

 <body>
  <h1>ClearPress Framework</h1>
<div class="main">

<p>
 Let's say ClearPress doesn't support our favourite format
 out-of-the-box. YAML for example - pretty common really but it hasn't
 (yet) been included as a standard response for views. We want to add
 YAML output using ClearPress' standard method naming (read_yml,
 list_yml etc.)  and tidy file extensions (.yml), just like we do for
 XML.
</p>

<p>
First in your view subclass, set any aspects ending in _yml to serve content in text/plain.

<pre><code>sub init {
  my $aspect = $self-&gt;aspect;

  $self-&gt;{content_type} ||= ($aspect =~ /_yml$/smx)?'text/plain':q[];

  return 1;
}
</code></pre></p>

<p>
Next, again in the view subclass, disable page decoration for aspects ending in _yml.

<pre><code>sub decor {
  my $self   = shift;
  my $aspect = $self-&gt;aspect;

  if($aspect =~ /_yml$/smx) {
   return 0;
  }
  return $self-&gt;SUPER::decor();
}
</code></pre></p>

<p>
Set up a template, data/templates/read_something_yml.tt2,  to use <a href="http://search.cpan.org/~rclamp/Template-Plugin-YAML">Template::Plugin::YAML</a>.

<pre><code>[%- USE YAML %]
[% yaml.dump(model) %]
</code></pre>

Note that as this stands it will dump the model's data and a lot of other junk from the template engine and the database handle etc.
</p>

<p>
So far what we've set up will allow us to access URLs like http://example.com/application/something/id;read_yml but that's pretty ugly, so last but not least we want to allow access via clean URLs like http://example.com/application/something/id.yml so in controller.pm we do:
<pre><code>sub accept_extensions {
  my $self = shift;
  return [
          @{$self-&gt;SUPER::accept_extensions},
          {'.yml'  =&gt; q[_yml]},
         ];
}</code></pre></p>

<p>
 Et voila! A YAML-formatted object dump with clean URL formatting. For extra credit you can make a clean data structure to dump using something like this:

<pre><code>[%- USE YAML %]
[%- PERL %]
  $stash->{clean} = {
    map { $_ => $stash->{model}->$_() }
    $stash->{model}->fields()
  }
[% END %]
[%- YAML.dump(clean) %]
</code></pre></p>

</div><!--end main-->

<div id="menu">
 <ul>
  <li><a href="/">About</a></li>
  <li><a href="/installing.html">Installing</a></li>
  <li><a href="/basics.html">The Basics</a></li>
  <li><a href="/rest.html">REST</a></li>
  <li><a href="/orm.html">ORM / Models</a></li>
  <li><a href="/views.html">Views</a></li>
  <li><a href="/routes.html">Routes</a></li>
  <li><a href="/streaming.html">Streamed content</a></li>
  <li><a href="/mimetypes.html">Custom response types</a></li>
  <li><a href="/paging.html">Paging Lists</a></li>
  <li><a href="/ajax.html">Working with AJAX</a></li>
  <li><a href="/authen.html">Authentication</a></li>
  <li><a href="/authz.html">Authorisation</a></li>
  <li><a href="/pdf.html">Exporting PDFs</a></li>
 </ul>

 <br/><br/><b>Still to come:</b>
 <ul style="font-style:italic">
  <li>Transactions</li>
  <li>Posting XML</li>
  <li>Automated Testing</li>
  <li>Internationalisation</li>
 </ul>
</div>


<div class="footer" style="margin-left:-50px">
<SCRIPT charset="utf-8" type="text/javascript" src="http://ws.amazon.co.uk/widgets/q?ServiceVersion=20070822&MarketPlace=GB&ID=V20070822/GB/gardmalt-21/8001/e9b44bbc-a251-45f1-8386-10affeb751cc"> </SCRIPT> <NOSCRIPT><A HREF="http://ws.amazon.co.uk/widgets/q?ServiceVersion=20070822&MarketPlace=GB&ID=V20070822%2FGB%2Fgardmalt-21%2F8001%2Fe9b44bbc-a251-45f1-8386-10affeb751cc&Operation=NoScript">Amazon.co.uk Widgets</A></NOSCRIPT>
</div>

<div class="footer">
  <address><a href="mailto:rmp@psyphi.net">rmp</a></address>
  2017-10-24 12:40:12 +0100
</div>

  </body>
</html>