• Publicidad

Template toolkit no imprime hash

Aquí encontrarás todo lo que sea específicamente acerca de módulos de Perl. Ya sea que estás compartiendo tu módulo, un manual o simplemente tienes una duda acerca de alguno.

Template toolkit no imprime hash

Notapor danimera » 2012-08-03 07:51 @368

Siempre lo he hecho así, pero no descubro por qué no puedo obtener el valor de un array de hash que le paso el Template Toolkit,

Este es mi código:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. my $vars = {
  2.     items => $items,
  3.     total => $self->session->param('total_cesta')
  4. };
  5.  
  6. my $c = $self->tt_process(
  7.     'templates/' . $self->current_language . '/' . $self->cfg('template_name') . '/cart_display.html', $vars );
  8.  
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


Esta mi plantilla:
Sintáxis: [ Descargar ] [ Ocultar ]
Using html4strict Syntax Highlighting
  1.            <h2>ITEM EN CESTA</h2>
  2.            <table border="0">
  3.            [% FOREACH item = items %]
  4.             <tr>
  5.                       <td>[% item.id %] </td>
  6.                       <td>[% item.price %] </td>
  7.                       <td>[% item.cant %] [% item %] </td>
  8.             </tr>
  9.            [% END %]
  10.  
  11.            </table>
  12. Total en carro: [% total %]
  13.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


y este el código generado:
Sintáxis: [ Descargar ] [ Ocultar ]
Using html4strict Syntax Highlighting
  1.   <h2>ITEM EN CESTA</h2>
  2.            <table border="0">
  3.            
  4.             <tr>
  5.                       <td> </td>
  6.                       <td> </td>
  7.                       <td> HASH(0x35c6390) </td>
  8.             </tr>          
  9.             <tr>
  10.                       <td> </td>
  11.                       <td> </td>
  12.                       <td> HASH(0x35c6300) </td>
  13.             </tr>
  14.            
  15.             <tr>
  16.                       <td> </td>
  17.                       <td> </td>
  18.                       <td> HASH(0x35bbd98) </td>
  19.             </tr>          
  20.             <tr>
  21.                       <td> </td>
  22.                       <td> </td>
  23.                       <td> HASH(0x35bbd08) </td>
  24.             </tr>  
  25.  
  26.            </table>
  27.            Total en carro: 1054
  28.  
  29.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Este es mi dumper $vars:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. $VAR1 = {
  2.     'total' => 1054,
  3.     'items' => {
  4.         '4' => { 'name' => 'Bolso 2',           'price' => 1000, 'id'    => '4', 'cant' => 1 },
  5.         '3' => { 'name' => 'ssss',              'id'    => '3',  'price' => 22,  'cant' => 2 },
  6.         '7' => { 'name' => 'Bolso 5028',        'price' => 0,    'id'    => '7', 'cant' => 1 },
  7.         '5' => { 'name' => 'BOLSO PARA HOMBRE', 'price' => 10,   'id'    => '5', 'cant' => 1 }
  8.     }
  9. };
  10.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4


Pero en la plantilla no me está colocando los valores de name, ni price, ni id... No sé qué pueda ser...
Última edición por explorer el 2012-08-03 12:03 @544, editado 1 vez en total
Razón: Formateado de código con Perltidy
100% Telch - Perl Web Programming
Cali PerlMongers: http://cali.pm.org
Avatar de Usuario
danimera
Perlero frecuente
Perlero frecuente
 
Mensajes: 871
Registrado: 2005-06-23 19:02 @834
Ubicación: Colombia

Publicidad

Re: Template toolkit no imprime hash

Notapor explorer » 2012-08-03 12:01 @542

Es que no es un array de hash, si no un hash de hash.

Tienes una estructura complicada (el id hace de clave de un hash secundario). Si te valiese con el id dentro del hash, te serviría esto:

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. use Template;
  3.  
  4. my $vars = {
  5.     items => [
  6.         {
  7.             id    => 'F1',
  8.             cant  => 2,
  9.             price => 10,
  10.         },
  11.         {
  12.             id    => 'F2',
  13.             cant  => 5,
  14.             price => 100,
  15.         },
  16.     ],
  17.     total => 42,
  18. };
  19.  
  20. my $template = Template->new();
  21.  
  22. my $plantilla = <<'EOP';
  23. <h2>ITEM EN CESTA</h2>
  24. <table border="0">
  25. [% FOREACH item = items %]    <tr>
  26.         <td>[% item.id %]</td>
  27.         <td>[% item.price %]</td>
  28.         <td>[% item.cant %]</td>
  29.     </tr>
  30. [% END %]</table>
  31.  
  32. Total en carro: [% total %]
  33. EOP
  34.  
  35. $template->process(\$plantilla, $vars);
  36.  
  37. __END__
  38. <h2>ITEM EN CESTA</h2>
  39. <table border="0">
  40.     <tr>
  41.         <td>F1</td>
  42.         <td>10</td>
  43.         <td>2</td>
  44.     </tr>
  45.     <tr>
  46.         <td>F2</td>
  47.         <td>100</td>
  48.         <td>5</td>
  49.     </tr>
  50. </table>
  51.  
  52. Total en carro: 42
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Con la estructura de datos que muestras, hay que dar un paso intermedio para acceder a los valores:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/env perl
  2. use Template;
  3.  
  4. my $vars = {
  5.     'total' => 1054,
  6.     'items' => {
  7.         '4' => { 'name' => 'Bolso 2',           'price' => 1000, 'id'    => '4', 'cant' => 1 },
  8.         '3' => { 'name' => 'ssss',              'id'    => '3',  'price' => 22,  'cant' => 2 },
  9.         '7' => { 'name' => 'Bolso 5028',        'price' => 0,    'id'    => '7', 'cant' => 1 },
  10.         '5' => { 'name' => 'BOLSO PARA HOMBRE', 'price' => 10,   'id'    => '5', 'cant' => 1 }
  11.     }
  12. };
  13.  
  14. my $template = Template->new();
  15.  
  16. my $plantilla = <<'EOP';
  17. <h2>ITEM EN CESTA</h2>
  18. <table border="0">
  19. [% FOREACH item = items %]    <tr>
  20.         <td>[% item.key %]</td>
  21.         <td>[% item.value.price %]</td>
  22.         <td>[% item.value.cant %]</td>
  23.     </tr>
  24. [% END %]</table>
  25.  
  26. Total en carro: [% total %]
  27. EOP
  28.  
  29. $template->process(\$plantilla, $vars);
  30.  
  31. __END__
  32. <h2>ITEM EN CESTA</h2>
  33. <table border="0">
  34.     <tr>
  35.         <td>3</td>
  36.         <td>22</td>
  37.         <td>2</td>
  38.     </tr>
  39.     <tr>
  40.         <td>4</td>
  41.         <td>1000</td>
  42.         <td>1</td>
  43.     </tr>
  44.     <tr>
  45.         <td>5</td>
  46.         <td>10</td>
  47.         <td>1</td>
  48.     </tr>
  49.     <tr>
  50.         <td>7</td>
  51.         <td>0</td>
  52.         <td>1</td>
  53.     </tr>
  54. </table>
  55.  
  56. Total en carro: 1054
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Bueno, esta es una manera de hacerlo, naturalmente.
JF^D Perl programming & Raku programming. Grupo en Telegram: https://t.me/Perl_ES
Avatar de Usuario
explorer
Administrador
Administrador
 
Mensajes: 14475
Registrado: 2005-07-24 18:12 @800
Ubicación: Valladolid, España

Re: Template toolkit no imprime hash

Notapor danimera » 2012-08-03 12:32 @564

Gracias como siempre, explorer.

Lo mejor será entonces modificar ese hash de hash y volver a un array de hash.

:D
100% Telch - Perl Web Programming
Cali PerlMongers: http://cali.pm.org
Avatar de Usuario
danimera
Perlero frecuente
Perlero frecuente
 
Mensajes: 871
Registrado: 2005-06-23 19:02 @834
Ubicación: Colombia


Volver a Módulos

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 3 invitados

cron