<html><head><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>... wow. Template::Alloy is ... wow. </div><div><br></div><div>Check it out if you've always wished Perl 5's context stuff was more complicated.  :)</div><div><br></div><div>Perl++ # you can do ANYTHING.  :)</div><div><br></div><div>j</div><div><br></div><div><br><div><div><br><div>Begin forwarded message:</div><blockquote type="cite"><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>From: </b></span><span style="font-family:'Helvetica'; font-size:medium;">Paul Seamons <<a href="mailto:mail@seamons.com">mail@seamons.com</a>><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>Subject: </b></span><span style="font-family:'Helvetica'; font-size:medium;"><b>Re: [Templates] force resultset as array</b><br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>Date: </b></span><span style="font-family:'Helvetica'; font-size:medium;">October 18, 2013 7:33:45 AM MDT<br></span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;"><span style="font-family:'Helvetica'; font-size:medium; color:rgba(0, 0, 0, 1.0);"><b>To: </b></span><span style="font-family:'Helvetica'; font-size:medium;"><a href="mailto:templates@template-toolkit.org">templates@template-toolkit.org</a><br></span></div><br>
  
    <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
  
  <div bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 10/18/2013 05:03 AM, Spevak, Martin
      wrote:<br>
    </div>
    <blockquote cite="mid:1382094200.4332.10.camel@singer.svk.hp.com" type="cite">
      <pre wrap="">Hello,

I have small problem with TT while getting DBIx::ResultSet. For instance
I have stash variable team which ich DBIx::Result (one entry). In DBIx
exists relations team_accesses as has_many. So in TT I am using:

team_accesses = team.team_accesses;

Problem is: 
        when team has no accesses, variable team_accesses is empty string ('')
        when 1 access exists, variable contain hash which is result
DBIx::TeamAccess. 
        when exists more than 1 access, variable contains array of
DBIx::TreamAccess.
Is the way how to say TT, that result needs to be converted to array?

I tried: team_access = [ team.team_accesses ], but my results are,
[ '' ], [DBIx::Result], [[DBIx::Result]]


</pre>
    </blockquote>
    I had tried to get CALL_CONTEXT added to Template::Toolkit, but as I
    was giving patches for adding various features, it was deemed that
    that would make the grammar grow too large.  So, while it isn't a
    good direct answer for how to do it in Template::Toolkit, it is
    possible to do this in Template::Alloy, but either way this portion
    from the Template::Alloy documentation does give more information
    about Template::Toolkit smart context:<br>
    <br>
    <br>
    <dl>
      <dt><a name="CALL_CONTEXT_(Not_in_TT)">CALL_CONTEXT (Not in TT)</a></dt>
      <dd><p>Can be one of 'item', 'list', or 'smart'. The default type is
          'smart'. The CALL_CONTEXT configuration specifies in what Perl
          context coderefs and methods used in the processed templates
          will be called. TT historically has avoided the distinction of
          item (scalar) vs list context. To avoid worrying about this,
          TT introduced 'smart' context. The <code>@()</code> and <code>$()</code>
          context specifiers make it easier to use CALL_CONTEXT in some
          situations.</p><p>The following table shows the relationship between the
          various contexts:</p>
        <pre class="sh_perl sh_sourceCode">       <span class="sh_keyword">return</span> <span class="sh_keyword">values</span>      smart context   list context    item context
       <span class="sh_symbol">-------------</span>      <span class="sh_symbol">-------------</span>   <span class="sh_symbol">------------</span>    <span class="sh_symbol">------------</span>
    A   <span class="sh_string">'foo'</span>              <span class="sh_string">'foo'</span>           <span class="sh_symbol">[</span><span class="sh_string">'foo'</span><span class="sh_symbol">]</span>         <span class="sh_string">'foo'</span>
    B   <span class="sh_keyword">undef</span>              <span class="sh_keyword">undef</span>           <span class="sh_symbol">[</span><span class="sh_keyword">undef</span><span class="sh_symbol">]</span>         <span class="sh_keyword">undef</span>
    <span class="sh_function">C</span>   <span class="sh_symbol">(</span>no <span class="sh_keyword">return</span> value<span class="sh_symbol">)</span>  <span class="sh_keyword">undef</span>           <span class="sh_symbol">[]</span>              <span class="sh_keyword">undef</span>
    <span class="sh_function">D</span>   <span class="sh_symbol">(</span><span class="sh_number">7</span><span class="sh_symbol">)</span>                <span class="sh_number">7</span>               <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">]</span>             <span class="sh_number">7</span>
    <span class="sh_function">E</span>   <span class="sh_symbol">(</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">)</span>            <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>         <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>         <span class="sh_number">9</span>
    F   <span class="sh_variable">@a</span> <span class="sh_symbol">=</span> <span class="sh_symbol">(</span><span class="sh_number">7</span><span class="sh_symbol">)</span>           <span class="sh_number">7</span>               <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">]</span>             <span class="sh_number">1</span>
    G   <span class="sh_variable">@a</span> <span class="sh_symbol">=</span> <span class="sh_symbol">(</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">)</span>       <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>         <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>         <span class="sh_number">3</span>
    <span class="sh_function">H</span>   <span class="sh_symbol">(</span><span class="sh_cbracket">{</span>b<span class="sh_symbol">=></span><span class="sh_string">"c"</span><span class="sh_cbracket">}</span><span class="sh_symbol">)</span>         <span class="sh_cbracket">{</span>b<span class="sh_symbol">=></span><span class="sh_string">"c"</span><span class="sh_cbracket">}</span>        <span class="sh_symbol">[</span><span class="sh_cbracket">{</span>b<span class="sh_symbol">=></span><span class="sh_string">"c"</span><span class="sh_cbracket">}</span><span class="sh_symbol">]</span>      <span class="sh_cbracket">{</span>b<span class="sh_symbol">=></span><span class="sh_string">"c"</span><span class="sh_cbracket">}</span>
    <span class="sh_function">I</span>   <span class="sh_symbol">([</span><span class="sh_number">1</span><span class="sh_symbol">])</span>              <span class="sh_symbol">[</span><span class="sh_number">1</span><span class="sh_symbol">]</span>             <span class="sh_symbol">[[</span><span class="sh_number">1</span><span class="sh_symbol">]]</span>           <span class="sh_symbol">[</span><span class="sh_number">1</span><span class="sh_symbol">]</span>
    <span class="sh_function">J</span>   <span class="sh_symbol">([</span><span class="sh_number">1</span><span class="sh_symbol">],[</span><span class="sh_number">2</span><span class="sh_symbol">])</span>          <span class="sh_symbol">[[</span><span class="sh_number">1</span><span class="sh_symbol">],[</span><span class="sh_number">2</span><span class="sh_symbol">]]</span>       <span class="sh_symbol">[[</span><span class="sh_number">1</span><span class="sh_symbol">],[</span><span class="sh_number">2</span><span class="sh_symbol">]]</span>       <span class="sh_symbol">[</span><span class="sh_number">2</span><span class="sh_symbol">]</span>
    K   <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>            <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>         <span class="sh_symbol">[[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]]</span>       <span class="sh_symbol">[</span><span class="sh_number">7</span><span class="sh_symbol">,</span><span class="sh_number">8</span><span class="sh_symbol">,</span><span class="sh_number">9</span><span class="sh_symbol">]</span>
    <span class="sh_function">L</span>   <span class="sh_symbol">(</span><span class="sh_keyword">undef</span><span class="sh_symbol">,</span> <span class="sh_string">"foo"</span><span class="sh_symbol">)</span>     <span class="sh_keyword">die</span> <span class="sh_string">"foo"</span>       <span class="sh_symbol">[</span><span class="sh_keyword">undef</span><span class="sh_symbol">,</span> <span class="sh_string">"foo"</span><span class="sh_symbol">]</span>  <span class="sh_string">"foo"</span>
    M   <span class="sh_keyword">wantarray</span><span class="sh_symbol">?</span><span class="sh_number">1</span><span class="sh_symbol">:</span><span class="sh_number">0</span>      <span class="sh_number">1</span>               <span class="sh_symbol">[</span><span class="sh_number">1</span><span class="sh_symbol">]</span>             <span class="sh_number">0</span></pre><p>Cases F, H, I and M are common sticking points of the smart
          context in TT2. Note that list context always returns an
          arrayref from a method or function call. Smart context can
          give confusing results sometimes, especially the I and J
          cases. Case L for smart match is very surprising.</p><p>The list and item context provide another feature for method
          calls. In smart context, TT will look for a hash key in the
          object by the same name as the method, if a method by that
          name doesn't exist. In item and list context Alloy will die if
          a method by that name cannot be found.</p><p>The CALL_CONTEXT configuration item can be passed to new or
          it may also be set during runtime using the CONFIG directive.
          The following method call would be in list context:</p>
        <pre class="sh_perl sh_sourceCode">    <span class="sh_symbol">[%</span> CONFIG CALL_CONTEXT <span class="sh_symbol">=></span> <span class="sh_string">'list'</span><span class="sh_symbol">;</span>
       results <span class="sh_symbol">=</span> my_obj<span class="sh_symbol">.</span>get_results<span class="sh_symbol">;</span>
       CONFIG CALL_CONTEXT <span class="sh_symbol">=></span> <span class="sh_string">'smart'</span>
    <span class="sh_symbol">%]</span></pre><p>Note that we needed to restore CALL_CONTEXT to the default
          'smart' value. Template::Alloy has added the <code>@()</code>
          (list) and the <code>$()</code> (item) context specifiers.
          The previous example could be written as:</p>
        <pre class="sh_perl sh_sourceCode">    <span class="sh_symbol">[%</span> results <span class="sh_symbol">=</span> @<span class="sh_symbol">(</span> my_obj<span class="sh_symbol">.</span>get_results <span class="sh_symbol">)</span> <span class="sh_symbol">%]</span></pre><p>To call that same method in item (scalar) context you would
          do the following:</p>
        <pre class="sh_perl sh_sourceCode">    <span class="sh_symbol">[%</span> results <span class="sh_symbol">=</span> $<span class="sh_symbol">(</span> my_obj<span class="sh_symbol">.</span>get_results <span class="sh_symbol">)</span> <span class="sh_symbol">%]</span></pre>
      </dd>
    </dl>
  </div>

_______________________________________________<br>templates mailing list<br><a href="mailto:templates@template-toolkit.org">templates@template-toolkit.org</a><br>http://mail.template-toolkit.org/mailman/listinfo/templates<br></blockquote></div><br><div apple-content-edited="true">
<span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><span class="Apple-style-span" style="font-family: monospace; "><br class="Apple-interchange-newline"><br></span></span></span></span>
</div>
<br></div></div><br><br>
<br></body></html>