Created
March 26, 2014 21:53
-
-
Save nordicdyno/9794465 to your computer and use it in GitHub Desktop.
perl handlebar primer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use strict; | |
| use warnings; | |
| use JSON::XS; | |
| use JavaScript::V8; | |
| use utf8; | |
| use open ":encoding(UTF-8)"; | |
| my $page_name = 'statuses/lenta'; | |
| my $context = JavaScript::V8::Context->new(); | |
| #slurp_and_eval_js('../handlebars.runtime-v1.3.0.js'); | |
| slurp_and_eval_js('../handlebars-v1.3.0.js'); | |
| slurp_and_eval_js('tests/render_page.js'); | |
| slurp_and_eval_js("helpers/extending.js"); | |
| slurp_and_eval_js("helpers/date.js"); | |
| #put_together_page_template | |
| #$context->eval( | |
| my $components = _put_together_page_template($page_name); | |
| #use Data::Dumper; print Dumper($components); | |
| my $template = $components->{partials}{ $components->{template} }; | |
| delete $components->{partials}{ $components->{template} }; | |
| my $hb_template = $context->eval("(function(partials, template) { | |
| for(var partial in partials) { if ( partials.hasOwnProperty( partial ) ) { | |
| Handlebars.registerPartial( partial, partials[partial] ); | |
| } } | |
| return Handlebars.compile(template); | |
| })")->($components->{partials}, $template); | |
| my $json_file = './tests/statuses_lenta_test_data.json'; | |
| my $r = $hb_template->(_read_json_file($json_file)); | |
| print $r; | |
| exit; | |
| sub _put_together_page_template { | |
| my $p_name = shift; | |
| my $file_prefix = "pages/$p_name"; | |
| unless (-f "$file_prefix.hbs") { | |
| die "Page template doesn't exists: $p_name"; | |
| } | |
| return _put_together_template($file_prefix); | |
| } | |
| sub _put_together_template { | |
| my ($file_prefix, $seen) = @_; | |
| $seen = {} unless $seen; | |
| my $res = { template => $file_prefix, partials => {} }; | |
| my $hbs_data_ref = slurp_file("$file_prefix.hbs"); | |
| $res->{partials}{$file_prefix} = $$hbs_data_ref; | |
| my $desc_path = "$file_prefix.desc.json"; | |
| unless (-f $desc_path) { | |
| return $res; | |
| } | |
| my $desc = _read_json_file($desc_path); | |
| #print Dumper($desc); | |
| for my $name (qw(blocks layouts)) { | |
| my $value = $desc->{$name}; | |
| next unless $value; | |
| for my $element (@{ ref $value ? $value : [$value] }) { | |
| my $sub_template_name = "$name/$element"; | |
| next if $seen->{$sub_template_name}; | |
| $seen->{$sub_template_name} = 1; | |
| my $sub_template = _put_together_template($sub_template_name, $seen); | |
| for my $partial (keys %{ $sub_template->{partials} }) { | |
| $res->{partials}{$partial} = $sub_template->{partials}{$partial}; | |
| $seen->{$partial} = 1; | |
| } | |
| } | |
| } | |
| return $res; | |
| } | |
| sub _read_json_file { | |
| my $json_data_ref = slurp_file(shift); | |
| #print $$json_data_ref; | |
| return JSON::XS->new->utf8(0)->decode($$json_data_ref); | |
| } | |
| sub slurp_and_eval_js { | |
| my $file = shift; | |
| my $content_ref = slurp_file($file); | |
| return $context->eval($$content_ref); | |
| } | |
| sub slurp_file { | |
| my $file = shift; | |
| $/ = undef; | |
| open my $fh, '<', $file or die "Can't open file $file: $!"; | |
| my $raw = readline $fh; | |
| return \$raw; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment