5

I am working in template toolkit framework. I have got an perl hash datatype in my tt file. I want to convert this hash datatype to javascript hash datatype.

code: template:

        [% PERL %]
        use JSON qw(encode_json);

        my $vars = {

            'version'  => 3.14,
            'days'     => [ qw( mon tue wed thu fri sat sun ) ],
            'cgi'      => CGI->new(),
            'me'       => {
                'id'     => 'abw',
                'name'   => 'Andy Wardley',
            },
        };

        my $json = encode_json($vars->{'me'});
    [% END %]


 <script>
   function callme(){
   var me = [% $json %]
  }
</script>

now i want the me hash to be accessible in javascript

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kalai
  • 81
  • 1
  • 3
  • 3
    The question is interesting, but it's impossible to answer it without code. – Amir E. Aharoni Nov 23 '12 at 12:36
  • Perl: my $vars = { 'version' => 3.14, 'days' => [ qw( mon tue wed thu fri sat sun ) ], 'cgi' => CGI->new(), 'me' => { 'id' => 'abw', 'name' => 'Andy Wardley', }, }; template: Email [% me.name %]

    This is version [% version %]

    Id: [% me.id %] Name: [% me.name %] now i want the me hash to be accessible in javascript
    – Kalai Nov 23 '12 at 12:41
  • @Kalai Edit your question and add it (with code formatting) rather than adding it like that in a comment. It's hard to read and easier to miss because it's a comment. – Vala Nov 23 '12 at 12:43
  • Please [edit] your question. – simbabque Nov 23 '12 at 12:48
  • @simbabque : I have altered the code a bit... – Kalai Nov 23 '12 at 13:04

2 Answers2

3

There are several TT plugins available to do this, any of which would be a better solution than embedding raw perl into your template. Personally, I prefer JSON::Escape, but there are a few others. In more than 5 years of writing TT on a more or less daily basis, I've never yet had to resort to using the [% PERL %] directive. I'm not writing CGI though, I suppose. YMMV.

[%- USE JSON.Escape( pretty => 1 );
    SET me = { id => 'abw', name => 'Andy Wardley' };
...
-%]

<script>
    function callme() {
    var me = [% me.json %]
    ...
</script>
RET
  • 9,100
  • 1
  • 28
  • 33
2

Try using JSON from CPAN. It's JavaScript Simple Object Notation and you directly use it in JavaScript.

use JSON qw(encode_json);

my $vars = {

    'version'  => 3.14,
    'days'     => [ qw( mon tue wed thu fri sat sun ) ],
    'cgi'      => CGI->new(),
    'me'       => {
        'id'     => 'abw',
        'name'   => 'Andy Wardley',
    },
};
print encode_json($vars->{'me'});

Output:

{"name":"Andy Wardley","id":"abw}
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • If you've `use`d it, you should be able to say `JSON::encode_json` in your tt. – simbabque Nov 23 '12 at 12:54
  • it throws me the following error , once i put this code in my tt file "encode_json" is not exported by the JSON module – Kalai Nov 23 '12 at 12:56
  • 1
    @Kalai I'm not familiar with TT. Try `require` and `JSON::encode_json()` instead. `use` is processed at compile time. There might be a problem with that in the template. Also, please don't change your question to reflect things you've done after an answer has been posted. Just stick to the comments. It makes it hard for people to follow. – simbabque Nov 23 '12 at 13:26