yanick/Template-Mustache

Support the dotted notation

Closed this issue · 11 comments

See #1.

By the way, just giving my usual spiel for slumbering modules: if you want, you can give me co-auth and I can bundle and push the patches as a new release on CPAN without any problem. Just sayin'. :-)

+1

This would be extremely helpful. This repo has been sitting idle for quite a long time and this part of the standard mustache toolkit is really vital.

Pretty pretty please?

+1 this is essential functionality. Without this, you can't iterate over a simple array.

Thanks for the great module!

I would also like this functionality. Not being able to iterate over a simple array is getting to me.

ETA - seems that it may already be there, but I can't seem to figure out how to use it.

{{#list}}
we have an item {{.}}
{{/list}}

The iteration is there, but {{.}} will fail. (although {{ foo.bar }} will work. It's the lone period that is not working)

I've rolled my sleeves and am diving in the code. We'll see what I can come up with.

That would be great. Thanks!

Good news, I have a branch rewrite that supports all of Mustache's specs (as of v1.1.3).

Bad news, I did major changes to the API on the way.

Everyone subscribed to this ticket: could you do a checkout of the rewrite branch and let me know if it work for you, or if I did terrible things that would make you sad if it made it to master?

It does what I want it to! Even with moose, which none of the other libraries do!

#!/usr/bin/env perl

package Decs;

use Moose;

has 'name' => (
    is      => 'rw',
    isa     => 'Str',
    default => 'Jillian'
);

has 'value' => (
    is      => 'rw',
    default => 100,
);

has 'in_ca' => (
    is        => 'rw',
    default   => 1,
    predicate => 'has_in_ca',
);

has 'taxed_value' => (
    is      => 'rw',
    default => 100,
);

has 'some_list' => (
    is      => 'rw',
    isa     => 'ArrayRef',
    default => sub { return [ 1, 2, 3 ] },
);

package Main;

use Template::Mustache;

my $self = Decs->new();

my $template = <<EOT;
Hello {{name}}
You have just won {{value}}!
{{#some_list}}
{{.}}
{{/some_list}}
{{#in_ca}}
Well, {{taxed_value}}, after taxes.
{{/in_ca}}
EOT

print Template::Mustache->render( $template, $self ), "\n";

$self->in_ca(0);

print Template::Mustache->render( $template, $self ), "\n";
Hello Jillian
You have just won 100!
1
2
3
Well, 100, after taxes.

Hello Jillian
You have just won 100!
1
2
3

\o/ And now that I have the guts of the module rewritten to be Moo-based, you could even do:

#!/usr/bin/env perl

package Decs;

use Moose;

extends 'Template::Mustache';

has 'name' => (
is => 'rw',
isa => 'Str',
default => 'Jillian'
);

has 'value' => (
is => 'rw',
default => 100,
);

has 'in_ca' => (
is => 'rw',
default => 1,
predicate => 'has_in_ca',
);

has 'taxed_value' => (
is => 'rw',
default => 100,
);

has 'some_list' => (
is => 'rw',
isa => 'ArrayRef',
default => sub { return [ 1, 2, 3 ] },
);


has '+template' => default => sub { <<'EOT';
Hello {{name}}
You have just won {{value}}!
{{#some_list}}
{{.}}
{{/some_list}}
{{#in_ca}}
Well, {{taxed_value}}, after taxes.
{{/in_ca}}
EOT
};

package Main;

my $self = Decs->new();

print $self->render, "\n";

$self->in_ca(0);

print $self->render, "\n";

Hmm... I should create a Template::Mustache::Role to make that use-case even cleaner. But yeah, even now, it totally works. :-)

I think a nice feature would be to add a Template::Mustache::Template class.

package Decs;

has 'my_template_1' => (
isa => 'Template::Musache::Template'
default => sub {
return <<'EOT';
Hello {{name}}
You have just won {{value}}!
{{#some_list}}
{{.}}
{{/some_list}}
{{#in_ca}}
Well, {{taxed_value}}, after taxes.
{{/in_ca}}
EOT
}

package Main;
my $self = Decs->new();
$self->my_template_1->render();

But I am quite happy with the current implementation. Thanks so much!

@jerowe Am I correct to think that #44 would scratch your itch? :-)

That is exactly what I want. Thanks!