Code reuse. it's all the rage. All the cool kids are doing it.

So, I've got a hunk of TT code in my CGI::Prototype application that I want to move to a separate file, and reuse. Fine, TT has INCLUDE and WRAPPER and all that cool stuff.

Except, the code calls "self.THIS" and "self.THAT" to do the heavy lifting, calling back to the "self" passed in to the template, corresponding to the same named ".pm" file next to the ".tt" file.

If I put that code into a separate template, the "heavy lifting" code will have to be moved all the way back up to the top of my application, if I'm planning on using this TT chunk in more than one page.

So, my idea is to "call" a subtemplate in a special way that creates a lightweight class (this is Class::Prototyped, so I can do that trivially) that creates a temporary new "self" that inherits from both the calling page (the original self) and the mix-in code (in a new same-named .pm file) to do the heavy lifting for the subpage. I figure the search order will be mix-in, then original, so that I don't have to worry that some other part of my app has a conflicting name.

But then, oops, how do I create defaults in my mixin code that can be overridden by the caller? It's not like a traditional inheritance, where I can override, because the mix-in code will go to its own default before it checks the rest of the tree?

Am I bonkers? How are mixins with defaults and overrides normally handled?

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


In reply to Mixins (problem in CGI::Prototype and Class::Protototyped with subtemplates) by merlyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.