<commit_before>## ----------------------------------------------------------------------------

package CGI::Conduit::Template;

use Moose::Role;

use Template;
use Template::Constants qw( :debug );

## ----------------------------------------------------------------------------

has 'stash' => ( is => 'rw' );

## ----------------------------------------------------------------------------

sub tt {
    my ($self) = @_;

    return $self->{tt} if $self->{tt};

    $self->{tt} = Template->new({
        INCLUDE_PATH => $self->cfg_value('tt_dir'),
        ($self->cfg_value('tt_pre_process') ? (PRE_PROCESS => $self->cfg_value('tt_pre_process')) : ()),
    });
    return $self->{tt};
}

sub tt_stash_set {
    my ($self, $key, $value) = @_;
    $self->{stash}{$key} = $value;
}

sub tt_stash_add {
    my ($self, $key, $value) = @_;
    $self->{stash}{$key} ||= [];
    push @{$self->{stash}}, $value;
}

sub tt_stash_params {
    my ($self, @params) = @_;
    foreach my $name ( @params ) {
        my $value = $self->req_param($name);
        next unless defined $value;
        $self->tt_stash_set( $name, $value );
    }
}

sub tt_stash_del {
    my ($self, $key) = @_;
    delete $self->{stash}{$key};<commit_msg>Allow multi-level data structures in tt_stash_set()<commit_after>## ----------------------------------------------------------------------------

package CGI::Conduit::Template;

use Moose::Role;

use Template;
use Template::Constants qw( :debug );

## ----------------------------------------------------------------------------

has 'stash' => ( is => 'rw' );

## ----------------------------------------------------------------------------

sub tt {
    my ($self) = @_;

    return $self->{tt} if $self->{tt};

    $self->{tt} = Template->new({
        INCLUDE_PATH => $self->cfg_value('tt_dir'),
        ($self->cfg_value('tt_pre_process') ? (PRE_PROCESS => $self->cfg_value('tt_pre_process')) : ()),
    });
    return $self->{tt};
}

sub tt_stash_set {
    my ($self, $key, $value) = @_;

    my $location = $self->{stash};

    # turn the key into something we can eval
    $key =~ s{([a-z_-]+)}{\{$1\}}gxms;
    $key =~ s{(\d+)}{\[$1\]}gxms;
    $key =~ s{\.}{}gxms;

    eval "\$location->$key = \$value";
    if ( $@ ) {
        die $@;
    }
}

sub tt_stash_add {
    my ($self, $key, $value) = @_;
    $self->{stash}{$key} ||= [];
    push @{$self->{stash}}, $value;
}

sub tt_stash_params {
    my ($self, @params) = @_;
    foreach my $name ( @params ) {
        my $value = $self->req_param($name);
        next unless defined $value;
        $self->tt_stash_set( $name, $value );
    }
}

sub tt_stash_del {
    my ($self, $key) = @_;
    delete $self->{stash}{$key};
{'fraction_non_alphanumeric': 0.35772357723577236, 'fraction_numerical': 0.0007390983000739098, 'mean_word_length': 1.6053897978825793, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '635722', 'n_tokens_mistral': 910, 'n_tokens_neox': 894, 'n_words': 176}