To display a task dashboard on the web I take information from a file with Perl, store that in a stacked hash and send it to HTML::Template::Compiled.
There I can only access the key of the hash but no further data.
Here is what I wrote in Perl
my $template = HTML::Template::Compiled->new(
filename => '../templates/task_dashboard.tmpl',
case_sensitive => 1,
search_path_on_include => 1,
loop_context_vars => 1,
use_query => 0,
default_escape => 'HTML',
);
$template->param(
TIME => $time,
VERSION => $version,
REPORTDAT => $reportdat,
INFOS => \%information,
);
print $template->output();
The hash %information looks like this:
%informmation{key} = ($RefToArrayOfHashes1, $RefToArrayOfHashes2, $Skalar1, $Skalar2);
Within HTML::Template::Compiled I want to loop through the hash %information and subsequently through its content (like Data::Dumper).
I wrote this in the template file:
<%each INFOS %>
<h2><%=__key__ %></h2>
<table>
...loop through RefToArrayOfHashes1
</table>
<table>
...loop through RefToArrayOfHashes2
</table>
<p>Skalar1, Skalar2</p>
<%/each %>
To access the keys of %information is not a problem, but I don't know how to access the lower levels of the data structure.
I read some things about to use Dot but still don't get how to do it.
Can anybody help me by giving an example?
Related
With the following Code I can fetch Data stored in a Hash from a Database and print it out:
use Data::Dumper;
my $fdx = $s->field(); # get Hashreference from Database
print Dumper($fdx); # print out Hash
The (important part of the) Output looks like this:
$VAR1 = bless( {
'selectable' => 'true',
'_PARENT_OBJECT' => bless( {
'dirtyFlag' => 1,
'path' => undef,
'testItems' => [],
'data' => {
'TEST_10' => {
'atm_rundatahistory' => {
'1523964918' => {
'atm_prid' => {
'content' => '',
'label' => 'Problem Report IDs',
'raw' => ''
}, ...
'1523964410' => {
'atm_prid' => {
'label' => 'Problem Report IDs',
'raw' => '23361234',
'content' => '23361234'
}, ...
'Test_10' is one of hundreds of Tests, '1523964918' is one of many unix timestamps, so basically its a 32 Bit Integer, but I dont know which numbers the timestamps contain.
How do I print out / access the values for 'content' (in this case '23361234') of the most inner Hashes, for all Tests and unix timestamps, if they exist?
from here on I will describe my thoughts and what I have tried, its not necessary to read any further for this question.
I think the code I am looking for is something like:
foreach my $val($fdx{_PARENT_OBJECT}{data}{"TEST_*"}{atm_rundatahistory}{"********"}{atm_prid}{content})
print("\n$val");
However I don't know the exact Syntax, and neither do I know which placeholders to set for "Test_10", since there are many tests numbers, e.g. "...Test_132...Test_134" and the Unix timestamps can be any 32 Bit Integer, so I guess I can use start as a placeholder? e.g. "********".
After some hours of searching on the web, I haven't found a understandable tutorial on how to access values from complex Perl hash structures, I guess there are some simple syntax-rules to know and you can get any value of even very complex data structures without to much effort.
I've already read perldoc_perlreftut. If there is any other easy to understandable tutorial for these kind of problems, please recommend them to me. I don't really know how I can learn to handle such complex data structures in Perl myself.
Template
<html>
<body>
<strong>Hi [% first_name %]</strong>,
<p>
This is to confirm your purchase of $ [% amt_due %].
</p>
<p>
Thank you!
</p>
</body>
</html>
`$params{first_name} = 'Frank';
$params{last_name} = 'Wiles';
$params{amt_due} = '24.99';
my $msg = MIME::Lite::TT::HTML->new(
From => 'admin#example.com',
To => 'frank#example.com',
Subject => 'Your recent purchase',
Template => {
text => 'test.txt.tt',
html => 'test.html.tt',
},
TmplOptions => \%options,
TmplParams => \%params,
);
How to save the mail locally before sending. It is having template as html which is populated with params and a pdf attachment.
Is it possible to save the Template with populated values.
MIME::Lite::TT is just a preprocessor; calling MIME::Lite::TT->new returns a normal MIME::Lite object. Just save that object in whatever way you like.
For example, you can print it to a filehandle:
my $email = MIME::Lite::TT->new(...);
$email->print(\*STDOUT);
$email->send;
To print the populated template we can use
$$email{data}
As $email is a reference to a hash and data is a key to the contents of body of email.
To print the whole mail use the above solution.
Can someone provide a sample code chunk of php using the sugarcrm API/nusoap for adding creating an acct. and then linking a lead to the acct?
I've got a sample function that adds a lead, and I can see how to create an acct, but I can't see how to tie a lead to the acct, to simulate the subpanel process in the sugarcrm acct/subpanel process.
thanks
// Create a new Lead, return the SOAP result
function createLead($data)
{
// Parse the data and store it into a name/value list
// which will then pe passed on to Sugar via SOAP
$name_value_list = array();
foreach($data as $key => $value)
array_push($name_value_list, array('name' => $key, 'value' => $value));
// Fire the set_entry call to the Leads module
$result = $this->soap->call('set_entry', array(
'session' => $this->session,
'module_name' => 'Leads',
'name_value_list' => $name_value_list
));
return $result;
}
$result = $sugar->createLead(array(
'lead_source' => 'Web Site',
'lead_source_description' => 'Inquiry form on the website',
'lead_status' => 'New',
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email1' => $_POST['email'],
'description' => $_POST['message']
));
You need to find the ID for the account and assign that ID to whatever the account_id field name is in the Lead Module. I have run into a couple things like this before and I have found it easier to go straight to the Sugar database. So, write a statement that will return the account is, for example: SELECT id WHERE something_in_the_account_table = something else;
Then you can assign that id in your $result array. I hope it helps. I didn't have any code or documentation in front of me or I would have helped more.
For example, if I process a form:
my $form_input = { input_data => '123' };
$form->process($form_input);
Then I want to alter the value of 'input_data':
my $clearme = $form->get_field('input_data');
$clearme->value("546"); # doesn't seem to work
..Before pushing the form object to TT:
template 'index' => { form => $form }; # using Dancer
'input_data' seems to retain it's original value (123). Any hints on what I'm doing wrong, or what I should be doing?
Thanks
After looking at the documentation and doing some testing, I think you want
$form->add_valid(input_data => '546');
I am using Rose::DB::Object, SQLite, and Chinese text. My classes look like this:
package My::DB;
use base qw(Rose::DB);
__PACKAGE__->use_private_registry;
__PACKAGE__->register_db(
driver => 'sqlite',
database => 'data/sqmple.db',
);
package Motorcycle;
use My::DB;
use base qw(Rose::DB::Object);
...
sub init_db { My::DB->new() };
The code used to store a record:
Motorcycle->new(
type => $self->param('type'),
brand => $self->param('brand'),
color => $self->param('color'),
)->save;
The code used to display the data (from within a Mojolicious app):
<td><%= Mojo::ByteStream->new($cycle->type)->decode("utf-8") %></td>
<td><%= Mojo::ByteStream->new($cycle->brand)->decode("utf-8") %></td>
<td><%= Mojo::ByteStream->new($cycle->color)->decode("utf-8") %></td>
How can I eliminate the decoding step? I'd like the display code to look like this instead:
<td><%= $cycle->type %></td>
<td><%= $cycle->brand %></td>
<td><%= $cycle->color %></td>
I think you need to get the sqlite_unicode => 1 configuration value down to SQLite, there was a similar question about UTF-8 and SQLite, setting sqlite_unicode did the trick there.
I don't think Rose::DB::SQLite supports this configuration parameter though. Based on this possibly similar issue with MySQL you might be able to patch Rose::DB::SQLite to add support for sqlite_unicode by adding this to the driver:
sub sqlite_unicode {
{
shift->dbh_attribute_boolean('sqlite_unicode', #_)
}
I'll leave a comment on John's answer so he can sanity check this.
If that works then you might want to send a patch to John Siracusa (who is not only already on this question but also the Rose::DB maintainer).
If you feed UTF8-encoded text into SQLite, it should give it right back to you in the same form. For example, given an SQLite database named test.db containing this schema:
CREATE TABLE things
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL
);
Run this code in a script in the same directory as the test.db database:
package My::DB;
use base qw(Rose::DB);
__PACKAGE__->use_private_registry;
__PACKAGE__->register_db
(
driver => 'sqlite',
database => 'test.db',
);
package My::Thing;
use base qw(Rose::DB::Object);
__PACKAGE__->meta->setup
(
table => 'things',
columns =>
[
id => { type => 'serial', primary_key => 1, not_null => 1 },
name => { type => 'text', length => 64, not_null => 1 },
],
);
sub init_db { My::DB->new }
package main;
# Set the name to a UTF8-encoded smiley: Unicode 0x263A
my $thing = My::Thing->new(name => "\x{e2}\x{98}\x{ba}")->save;
$thing = My::Thing->new(id => $thing->id)->load;
# This will print the UTF8-encoded smiley; make sure your
# terminal can handle UTF8 output.
print $thing->name, "\n";
If this is not working for you, then perhaps your calls to get the form parameters (e.g., $self->param('type')) are returning character strings instead of UTF8-encoded strings. That is, in the case of the smiley face string, perhaps $self->param('foo') is returning "\x{263a}" and not "\x{e2}\x{98}\x{ba}". In that case, the solution would be to encode the strings as UTF8 before setting the object attributes:
Motorcycle->new(
type => utf8::encode($self->param('type')),
brand => utf8::encode($self->param('brand')),
color => utf8::encode($self->param('color')),
)->save;