最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to convert perl hash to javascript hash - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Apr 5, 2018 at 8:56 Cœur 38.8k25 gold badges206 silver badges279 bronze badges asked Nov 23, 2012 at 12:35 KalaiKalai 811 silver badge3 bronze badges 5
  • 3 The question is interesting, but it's impossible to answer it without code. – Amir E. Aharoni Commented Nov 23, 2012 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: <a href="mailto:[% email %]">Email [% me.name %]</a> <p>This is version [% version %]</p> Id: [% me.id %] Name: [% me.name %] now i want the me hash to be accessible in javascript – Kalai Commented Nov 23, 2012 at 12:41
  • @Kalai Edit your question and add it (with code formatting) rather than adding it like that in a ment. It's hard to read and easier to miss because it's a ment. – Vala Commented Nov 23, 2012 at 12:43
  • Please edit your question. – simbabque Commented Nov 23, 2012 at 12:48
  • @simbabque : I have altered the code a bit... – Kalai Commented Nov 23, 2012 at 13:04
Add a ment  | 

2 Answers 2

Reset to default 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>

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}
发布评论

评论列表(0)

  1. 暂无评论