Tenjin::Context(3pm)

πŸ“¦
version 1.000001 Β· 3 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– Tenjin::Context β€” the manual, but make it slay πŸ’–

okay bestie, this is Tenjin::Context from the Perl user-contributed docs, and it's literally the module that's in charge of managing variables passed to Tenjin templates. That's the whole vibe. Let's get into it πŸŽ€


✨ NAME ✨

Tenjin::Context β€” in charge of managing variables passed to Tenjin templates. no cap, that's the job description πŸ’…


🌸 SYNOPSIS 🌸

# this module is used internally, but if you insist, it is
# in charge of the context object:

# in your templates (unnecessary, for illustration purposes):
<title>[== $_context->{title} =]</title>
# instead use:
<title>[== $title =]</title>

so like. yes you could reach into $_context directly but bestie why would you when $title is right there 😭 that's the whole point of the context object, it feeds you the good stuff directly.


πŸ¦‹ DESCRIPTION πŸ¦‹

This module is in charge of managing Perl variables that get passed to templates when they render, so templates can just... use them. Directly. Iconic behavior honestly πŸ’–

The context object is simply a hash-ref of key-value pairs, and every key in that hash-ref becomes available to templates as a "standalone variable" named after the key. So the key title β†’ the variable $title. The way that works is genuinely so clean.

This module is also the one in charge of the actual rendering of the templates β€” or, more correctly, of evaluating the Perl code that was created from the templates: first it integrates the context variables into them, then it returns the rendered output. main character energy, honestly πŸ‘‘

And finally, this module makes the Tenjin utility methods from Tenjin::Util available natively inside templates. See Tenjin::Util for more info. (We'll list them all below too, don't worry, I would never gatekeep 🫢)


πŸ”§ INTERNAL METHODS πŸ”§

new( [\%vars] )

Constructs a brand-new context object, which is basically a hash-ref of key-value pairs that get passed to templates as variables. If you pass a $vars hash-ref into the constructor, it gets augmented into the created object. cute πŸ’•

To illustrate the context object, suppose it looks like so:

{
        scalar          => 'I am a scalar',
        arrayref        => [qw/I am an array/],
        hashref =>      { i => 'am', a => 'hash-ref' },
}

Then the variables $scalar, $arrayref and $hashref will be available for direct usage inside your templates, and you can dereference the variables normally β€” i.e. @$arrayref and %$hashref. slay πŸŽ€

evaluate( $script, $template_name )

This method receives a compiled template and actually performs the evaluation that renders it, then returns the rendered output. If Tenjin is configured to use strict, the script will be evaled under use strict. so bestie, strict mode is on, behave 😀

to_func( $script, [$filename] )

This method receives the script created when reading a template, wraps it in a subroutine, evals it, and returns the rendered output. This method is called when compiling the template. lowkey the engine room of the whole operation πŸ”₯

_build_decl()

This method is in charge of making all the key-value pairs of the context object available to templates directly, by their key names. It's simply done by traversing the key-value pairs of the context object and adding an assignment line between a scalar variable named as the key and its appropriate value. no magic, just a lot of little assignments doing their thing ✨


πŸ’… UTILITY METHODS πŸ’…

These methods are defined in Tenjin::Util and are used here so they're made available natively inside templates. See Tenjin::Util for more information. iykyk 🧚

  • _p( $expr )
  • _P( $expr )
  • escape( $expr )
  • escape_xml( $expr )
  • unescape_xml( $expr )
  • encode_url( $url )
  • decode_url( $url )
  • checked( $expr )
  • selected( $expr )
  • disabled( $expr )
  • nl2br( $text )
  • text2html( $text )
  • tagattr( $name, $expr, [$value] )
  • tagattrs( %attrs )
  • new_cycle( @items )

πŸ‘€ SEE ALSO πŸ‘€

Tenjin, Tenjin::Util, Tenjin::Template.


See Tenjin.


go read Tenjin::Util next bestie, that's where all those utility methods actually live πŸ’œ

and that's the whole page!! πŸ’– written up for perl v5.38.2, dated 2024-03-08 β€” mwah, go build something cute with your templates πŸŒˆβ­πŸŽ€

πŸ“¦ where this page comes from