Build Status Coverage Status

NAME

Text::Xslate::Bridge::TypeDeclaration - A Mouse-based Type Validator in Xslate.

SYNOPSIS

my $xslate = Text::Xslate->new(
    module => [ 'Text::Xslate::Bridge::TypeDeclaration' ],
);

# @@ template.tx
# <:- declare(name => 'Str', engine => 'Text::Xslate') -:>
# <: $name :> version is <: $engine.VERSION :>.

# Success!
$xslate->render('template.tx', {
    name   => 'Text::Xslate',
    engine => $xslate
});
# Text::Xslate version is 3.4.0.


# A string 'TT' is not isa 'Text::Xslate'
$xslate->render('template.tx', {
    name   => 'Text::Xslate',
    engine => $xslate
});
# <pre class="type-declaration-mismatch">
# Declaration mismatch for `engine`
#   declaration: 'Text::Xslate'
#         value: 'TT'
# </pre>
# Template::Toolkit version is .

DESCRIPTION

Text::Xslate::Bridge::TypeDeclaration is a type validator module in Text::Xslate templates.

The type validation of this module is base on Mouse::Util::TypeConstraints.

declare interface was implemented with reference to Smart::Args.

DECLARATIONS

Mouse Defaults

Object

Hashref

Arrayref

OPTIONS

Text::Xslate->new(
    module => [
        'Text::Xslate::Bridge::TypeDeclaration' => [
            # defaults
            method      => 'declare', # method name to export
            validate    => 1,         # enable validation when truthy
            print       => 'html',    # error output format ('html', 'text' or 'none')
            on_mismatch => 'die',     # error handler ('die', 'warn' or 'none')
        ]
    ]
);

APPENDIX

Disable Validation on Production

Perhaps you want to disable validation in production to prevent spoiling performance.

Text::Xslate->new(
    module => [
        'Text::Xslate::Bridge::TypeDeclaration' => [
            validate => $ENV{PLACK_ENV} ne 'production',
        ],
    ],
);

Use type-declaration-mismatch class name

Highlight by css

.type-declaration-mismatch { color: crimson; }

Lint with Test::WWW::Mechanize

# in subclass of Test::WWW::Mechanize
sub _lint_content_ok {
    my ($self, $desc) = @_;

    if (my $mismatch = $self->scrape_text_by_attr('class', 'type-declaration-mismatch')) {
        $Test::Builder::Test->ok(0, $mismatch);
    };

    return $self->SUPER::_lint_content_ok($desc);
}

SEE ALSO

LICENSE

Copyright (C) pokutuna.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

pokutuna popopopopokutuna@gmail.com