[Dancer-users] Using utf-8 data from database and template toolkit

Deepak Gulati deepak.gulati at gmail.com
Tue Apr 6 17:40:45 UTC 2010


Hi,

Does it work for you without DBIx::Class?

I did the following:

1. Created a SQLite database:
sqlite3 names.sqlite

2. Executed the following SQL
sqlite> .read names.sql

where names.sql contains:
create table names (FirstName varchar(20), LastName varchar(20));
insert into names (FirstName, LastName) values ('Bedřich', 'Smetana');
insert into names (FirstName, LastName) values ('Antonín', 'Dvořák');
insert into names (FirstName, LastName) values ('Fréderyck', 'Chopin');

3. My mywebapp.pl looks like this (the database names.sqlite is in the
folder db under the default mywebapp created by dancer):

#!/usr/bin/perl
use Dancer;
use mywebapp;
use Data::Dumper;
use Template;
use DBI;

get '/names/' => sub {

	my $dbh = DBI->connect("dbi:SQLite:dbname=db/names.sqlite", "", "");
	my $sql = "select FirstName, LastName from names";
	my $sth = $dbh->prepare($sql);
	
	$sth->execute;
	
	my $names = $sth->fetchall_arrayref;

	template 'names' => { 'names' => $names };
};
dance;

4. And my TT2 template looks like this:
<table>
[% FOREACH n IN names %]
	<tr>
		<td>[% n.0 %]</td>
		<td>[% n.1 %]</td>
	</tr>
[% END %]
</table>

This setup works for me without any data corruption issues.

I am on Mac OS 10.6, with Perl 5.10.0, SQLite3 and Dancer 1.173.


More information about the Dancer-users mailing list