logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Text::Xslate::Manual::Debugging - Debugging techniques for Xslate templates

Description

       This document describes techniques for debugging templates.

   Setting"verbose=>2"
       Try "verbose => 2" in the first step. This option enables full warnings, especially warnings related to
       "undef".

   Filenamesandlinenumbers
       Xslate messages include file names, line numbers, and, if possible, source code lines which seems
       problems.

       You can also access the file name and the line number in templates by "__FILE__" and "__LINE__" tokens
       just like as Perl.

       If you want reports files and lines from your registered functions, "Text::Xslate->current_file" and
       "Text::Xslate->current_line" in callbacks are the same as "__FILE__" and "__LINE__" in templates
       respectively.

           sub my_sqrt {
               my($n) = @_;

               if($n < 1) {
                   # return a message instead of warnings
                   return sprintf "!!! Can't take sqrt of $n at %s line %d !!!",
                       Text::Xslate->current_file, Text::Xslate->current_line;
               }
               return sqrt($n);
           }

           my $tx = Text::Xslate->new(
               function => { sqrt => \&my_sqrt },
           );

   Todumpvalues
       You can use any dumping modules via the "function" option, but Xslate has a builtin "dump" filter to dump
       template values.

           <: $value | dump # Dump $value with Data::Dumper :>

   Detectionofmissingvariables(ortyposorvariablenames)
       Xslate itself has warning system for use of uninitialized values, but sometimes it is not enough.

       If you want fill in some string, e.g. FILLME, for missing variables, you can use the hash_with_default()
       utility. For example:

           use Text::Xslate::Util qw(hash_with_default);
           $tx->render($name, hash_with_default(\%vars, sub { "FILL ME '@_' " }) );

       Note that this is really slow because it is a tied-hash wrapper.

   Customizationoferrormessages
       You can customize error handlers by "warn_handler" and "die_handler".  In these handlers, you can call
       "Text::Xslate->print()" method in order to add your custom messages to the output buffer, which makes
       debugging easier.

           #!perl -w
           use strict;
           use Text::Xslate;
           my %vpath = (
               hello => 'Hello, <: $lang :> world!' . "\n",
           );
           my $tx = Text::Xslate->new(
               path         => \%vpath,
               verbose      => 2,
               warn_handler => sub { Text::Xslate->print('[[', @_, ']]') },
           );

           print $tx->render('hello', { });
           # => Hello, [[use nil to print at ...]] world!

Name

       Text::Xslate::Manual::Debugging - Debugging techniques for Xslate templates

See Also

       Text::Xslate

       Text::Xslate::Manual

perl v5.40.0                                       2024-10-20               Text::Xslate::Manual::Debugging(3pm)

See Also