Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active January 7, 2021 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/23d83c2ddcfc69aae26f33c572caa87d to your computer and use it in GitHub Desktop.
Save Xliff/23d83c2ddcfc69aae26f33c572caa87d to your computer and use it in GitHub Desktop.
This Role will allow you to use string literals as templates with Cro::WebApp
use v6;

use Cro::WebApp::Template::ASTBuilder;
use Cro::WebApp::Template::Parser;
use Cro::WebApp::Template::Repository;

role Cro::WebApp::TemplateString {
  has $!template-repo;

  submethod TWEAK {
    $!template-repo = Cro::WebApp::Template::Repository.new
  }

  method render ($template, *%params) {
    my $*TEMPLATE-REPOSITORY = $!template-repo;
    my $ast = Cro::WebApp::Template::Parser.parse(
      $template,
      actions => Cro::WebApp::Template::ASTBuilder
    ).ast;

    Cro::WebApp::Template::Compiled.new(
      |$ast.compile,
      repository => $!template-repo
    ).render(%params);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment