1
2
3
4
5
6
{
"countries": [
{"id":190,"label":"Afghanistan"},
{"id":2,"label":"\u00c4gypten"}
]
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/bin/false # # This code include is used to load countries for the flexmodule daad_country_facts.htms from the API. use strict; use vars qw(@parameters $new $mode $metainfo); use Project::Util::Api; sub main { if ($mode ne 'EDIT') { return; } my @items = _getData(); if (scalar @items == 0 || ! defined \@items) { return ""; } my $html = ""; # @TODO replace 1000 with actual array length for ($a = 0 ; $a <= 1000 ; $a++) { if (defined @items[0]->[$a]->{"id"}) { # option selection will be automatically set by Imperia $html .= sprintf( '<option value="%s">%s</option>', @items[0]->[$a]->{"id"}, encode_utf8(@items[0]->[$a]->{"label"}), ); } } return $html; } sub _getData { my $url = sprintf( "%s%s", $ENV{'API_BASE_URL'}, "/ajax/imperia/countrylist/de" ); my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 }); my $header = HTTP::Request->new(GET => $url); my $request = HTTP::Request->new('GET', $url, $header); my $response = $ua->request($request); if (!$response || $response->is_error || !$response->is_success) { return; } my $response = decode_json($response->content); my @items = $response->{"countries"}; return @items; } $new = main();
QuoteIch schaffe es nicht diese Struktur nun in einfache HTML-Options umzuwandeln, weil ich es nicht schaffe die Daten aus dem Array zu referenzieren.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use strict; use warnings; use Template::Mustache; my $countries = [ {id => 190, label => "Afghanistan"}, {id => 2, label => "Ägypten"} ]; my $template = q( <select name="countries"> {{#countries}} <option id="{{id}}" value="{{& label}}">{{& label}}</option> {{/countries}} </select> ); print Template::Mustache->render($template, {countries => $countries}); Ausgabe: <select name="countries"> <option id="190" value="Afghanistan">Afghanistan</option> <option id="2" value="Ägypten">Ägypten</option> </select>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
sub main { if ($mode ne 'EDIT') { return; } # Receive an array reference my $countries = _getData(); if ( 0 == scalar @$countries ) { return ""; } my $html = ""; # $#{$countries} is last index of array reference $countries for (my $i = 0 ; $i <= $#{$countries} ; $i++) { if (defined $countries->[$i]->{"id"}) { # option selection will be automatically set by Imperia $html .= sprintf( '<option value="%s">%s</option>', $countries->[$i]->{"id"}, encode_utf8($countries->[$i]->{"label"}), ); } } return $html; } sub _getData { my $url = sprintf( "%s%s", $ENV{'API_BASE_URL'}, "/ajax/imperia/countrylist/de" ); my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 }); my $header = HTTP::Request->new(GET => $url); my $request = HTTP::Request->new('GET', $url, $header); my $response = $ua->request($request); if (!$response || $response->is_error || !$response->is_success) { return; } my $response = decode_json($response->content); return $response->{"countries"}; }