How to pass arguments to a subroutine in template toolkit function - perl

I my document foo.tt i would like to write something like this:
[% INCLUDE header('str', 1, 2, 3, 5, 10) %]
My aim is to do some string manipulation on str and then with foreach / for iterates over all the numbers.
unfortunately i was unable to find that this type of syntax in Template toolkit is allowed.
What is Template toolkit way for passing arguments to a subroutine?
Any ideas how to do it?

You can pass arguments, but you need to give them names. Example:
outer.tt2:
[% INCLUDE header.tt2 header_string="str", items=[ 1, 2, 3, 5, 10 ] -%]
header.tt2:
String: [% header_string %]
[% FOREACH item IN items -%]
Item: [% item %]
[% END -%]
output:
String: str
Item: 1
Item: 2
Item: 3
Item: 5
Item: 10

Check out MACRO definitions:
[% MACRO header(str, items) BLOCK -%]
[% FOREACH i IN items; -%]
... your item code here ...
[% END -%]
[% END -%]
[% header('str',[1, 2, 3, 5, 10]) %]
If TT exposed the raw arguments list at the template level, you could call it as you indicated (e.g. header('str', 1, 2, 3, 5, 10)), but this is pretty close.

Related

Using M4, how to extract code from a string and increase indentation?

I need to write a M4 macro that extracts + transforms code between curly braces.
I want to transform
{
import math
a_list = [1, 4, 9, 16]
if val:
print([math.sqrt(i) for i in a_list])
else:
print("val is False")
print("bye bye")
}
to
import math
a_list = [1, 4, 9, 16]
if val:
print([math.sqrt(i) for i in a_list])
else:
print("val is False")
print("bye bye")
The macro has to trim the whitespace before the first { and after the last }.
Because this is python code, nside the curly braces, the relative indentation must be preserved.
Because the output of the macro will be outputted somewhere, needed a certain level of indentation.
The macro should also be able to add extra indentation (=some number of spaces), e.g. given as an argument.
The project is already using m4sugar, so the quotes are [ and ].
Thanks.

show some data in koha ILS authority normal view

in opac-authoritiesdetail.pl
i add
$template->param( "tag_781_z" => $record->subfield('781','z') );
and in opac-auth-detail.tt
[% IF( tag_781_z ) %]
<span class="">
<span class="label">Geographic Subdivision
</span>
[% tag_781_z %]
</span>
[% END %]
the result
the first value only shown
but I add 3 value
If field 780$z is repeatable you must add value this field to array.
Regards

How can I paste the IPython output in IPython?

The question's title tells it; I'm reading a book and I'd like to try the code on the fly using IPython but all the code is structured like this:
right = DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'],
....: 'key2': ['one', 'one', 'one', 'two'],
....: 'rval': [4, 5, 6, 7]})
I'd like to copy it directly from the book inside the terminal but even using %paste I receive an Invalid Syntax error. I could use %cpaste but for longer inputs it is kind of frustrating.
Thanks for your help
So it should work, but you need to be sure that when pasting the ....: are well aligned. Meaning that you need to copy carefully.
The following should work for instance :
right = DataFrame({'key1': ['foo', 'foo', 'bar', 'bar'],
....: 'key2': ['one', 'one', 'one', 'two'],
....: 'rval': [4, 5, 6, 7]})
Or this one too (where we see the alignment with the semi column of In []: :
In [68]: a = [1,
....: 2,
....: 3]
My guess that if you cannot copy better than you did, is that the book did a bad formatting when pasting. If so, you still can open a basic text editor and find and replace ....: with nothing.
Instead of opening an issue, it should be more probably related to a feature of the %paste function which implement something that deals with bad indentation, but then it starts being messy, IMO.
Hope this helps.

Using a variable to access hash keys in Template Toolkit

I have an array whose contents is
$VAR1 = {
'1' => 'May 05, 2011',
'0' => 'Jul 22, 2009',
'2' => 'Jun 13, 2012'
};
I am trying to display it in the catalyst template, the code is
[% x = 0 %]
[% FOREACH mortgage IN mortgages %]
<table width=40% border=1 cellspacing="0" cellpadding="10">
<tr>
<td>Date</td>
<td><b>[% dateformat.x %]</b></td>
</tr>
</table>
[% x = x+1 %]
[% END %]
The dateformat.x should display May 05, 2011 or Jul 22, 2009 or Jun 13, 2012 according to the value of x but the error is that it displays nothing. It shows a blank .
The error I think is that the key in the array is a string while the value of x that is used with the dateformat is numeric. If I add 0 or 1 with the dateformat then it displays correctly([% dateformat.0 %]).
[% dateformat.x %] looks in the dateformat hash for a key of x. To tell template toolkit that x is a variable, prefix it with $:
[% dateformat.$x %]
To access a hash entry using a key stored in another variable, prefix the key variable with '$' to have it interpolated before use (see Variable Interpolation).
I appreciate this question has already been asked and answered, but a handy alternative is the item() VMethod. This is particularly effective when you have hash keys that conflict with VMethods:
[%- SET myhash = { last => 'Blues', first => 'Elwood',
address => '1060 West Addison', city => 'Chicago' };
myhash.first; # doesn't do what you want,
# because first is a VMethod for 1st element in an array
myhash.item('first'); # displays "Elwood"
-%]
VMethods like first, last, size and sort are common traps for the unwary here.

Hash Dereference in Template Toolkit

I've got a multi-dimensional hash that I'm trying to print out in a table. I can't get the referencing / dereferencing right.
I'm putting an excel spreadsheet into the hash and I want to print out the corresponding rows and columns in html and match the rows/columns of the spreadsheet (some of which are empty).
I'm using Perl Dancer and Template Toolkit. On the server side the hash works fine. print $big_table{$column}{$row}; on the server side and it prints the correct column and row with NO issues.
On the client side, the 0, 1, 2... are supposed to be the columns. Some columns are blank so I can't just print the contents.
The way it is now it prints ARRAY(0x3e5389c). I tried a different way and it printed HASH...
I know I've got some reference/dereference issues. Any advice would be welcome.
Server Side Code:
my %big_table = ();
# $cell->value() is the text ripped from the excel cell at that location
$big_table{$column}{$row} = $cell->value();
template 'index', { big_table => \%big_table };
Client Side:
<Table border="3">
<% FOREACH n IN big_table.0 %>
<TR><TD>&nbsp<% big_table.0.keys %>&nbsp<TD>&nbsp<% big_table.1.keys %>
&nbsp<TD>&nbsp<% big_table.2.keys %>&nbsp<TD>&nbsp<% big_table.3.keys %>
&nbsp <TD>&nbsp<% big_table.4.keys %>
&nbsp<TD>&nbsp<% big_table.5.keys %>&nbsp
<% END %>
</Table>
Thanks in advance!
Got it working.
Changed to an array. '$big_table[$col][$row] = $cell->value();' and populated a second array with all the row #'s.
The Client looks like
<% FOREACH r IN row_numbers %>
<TR><TD> &nbsp <% big_table.0.$r %> &nbsp <TD> &nbsp <% big_table.1.$r %>...
<% END %>
Works great but it's probably crazy in-effecient :(. The spreadsheet is 800 rows long so it's a 2nd array with 800 elements just to iterate over 'FOREACH' loop.