Help language development. Donate to The Perl Foundation
Using Cro templates in a Hash
Introduction
Usage
Dependencies
Differences from CWT documentation
sub templates-from-hash( %hash-of-templates )
sub render-template( $template, $topic, :%parts, :build($)! )
sub modify-template-hash( %templates )
sub wait-for-hash-template-completion
This module uses the Cro template language Cro::WebApp::Template (called CWT below) where the templates are in a Hash.
CWT is aimed at templates that exist as files in some directory / folder, whether on a server or in an online resource. Consequently, each template is contained in a single file. A collection of templates exists under a root directory. The directory may be on-line or local. This paradigm implies the request-response of a server hosted website, with the template used being dependent on the request.
In a Build context that is designed to be distributed via a CDN the structural HTML of a website is static. Consequently, all the templates needed for the structure are known before any incoming request. User interaction with the website is confined to micro-service
interfaces, each of which have their own server request/responses.
The structural templates can therefore be collected before creating the HTML and can be placed in a Hash. This also allows for templates to use other templates in the Hash via the <:use>
tag.
use v6.d; use Cro::WebApp::Template::Repository::Hash; # gather template strings my %temps = %( 'format-b' => '<strong><.contents></strong>', 'heading' => q:to/HEADING/ , <:sub heading($level=1,:$close=False)> <?$close>/</?>h<$level></:> <<&heading(.level)> id="<.target>"> <a href="#<.top>" class="u" title="go to top of document"> <.text> </a> <<&heading(.level,:close)>> HEADING ); # the following is required to create a Build repository, otherwise the # default Filesystem repository will be used. templates-from-hash %temps; # later my $parameters = %( :1level, :target<https://docs.raku.org>, :top<__TOP>, :text("This is a title") ); say render-template('heading', $parameters, :build);
Uses Cro::WebApp::Template
The sub must be called with each value of %hash-of-templates
containing a valid crotmp template before using render-template
.
render-template
is used in the same way as documented CWT.
This sub can only be used after templates-from-hash
, which creates a Build repository. It is NOP otherwise.
The values in %templates over-ride the value in the repository if it was set by templates-from-hash
or adds to the repository if the key did not previously exist.
waits for all the compilation promises to stop returning Planned on a status call.
Rendered from README at 2022-06-13T18:26:21Z