control-flow

Control flow

Template::HAML recognizes a small set of control-flow keywords that follow the silent operator -. Each opens an indented block whose body is conditionally or repeatedly rendered; the keyword line itself produces no output and consumes no level of output indentation.

if / elsif / else

- if $count == 0
  %p Nothing here yet.
- elsif $count == 1
  %p One item.
- else
  %p
    Many items.

The first matching branch renders; subsequent elsif/else branches are skipped. An elsif or else without a preceding if/unless raises X::HAML::OrphanElse.

unless

- unless $logged-in
  %a(href='/login') Sign in

unless is the inverse of if: it renders the body when the expression is falsey. It also accepts trailing elsif/else branches.

for

Both Raku-native and Python-flavored forms are accepted:

- for $items -> $item
  %li
    = $item.name

- for $name in $names
  %p
    = $name

The loop variable is added to the locals available to the body — and only the body. Sibling lines outside the loop will not see the loop variable.

while

- while $queue.elems
  %li
    = $queue.shift

Templates iterate at most Renderer.max-while-iters times (default 10000) as a runaway-loop safeguard.

repeat

- repeat 3
  %li hi

renders three <li>hi</li> siblings. The count is any expression that evaluates to an integer; values that are zero or negative render nothing. Use repeat when you only need a fixed number of iterations and don't care about the loop index — for the index, reach for for.

given / when / default

- given $status
  - when 'ok'
    %p.success All good
  - when 'warn'
    %p.warning Heads up
  - default
    %p.error Unknown status

given EXPR topicalizes the expression as $_ for its when and default children. Each when EXPR smartmatches against the topic ($_ ~~ EXPR); the first match wins. default runs only if no when matched. Children other than when/default directly under given are ignored.

Indentation

A control-flow line consumes one level of source indentation but produces zero levels in the output. So:

%div
  - if $show
    %p Hi

renders as:

<div>
  <p>Hi</p>
</div>

— the %p is one level deep in the output, not two, because - if is transparent.

Errors

ExceptionRaised when
X::HAML::OrphanElseelsif or else appears without a preceding if.
X::HAML::EvalThe condition or iterator expression fails to eval.

Template::HAML v0.9.5

HAML template engine

Authors

  • Greg Donald

License

Artistic-2.0

Dependencies

Provides

  • Template::HAML
  • Template::HAML::Actions
  • Template::HAML::CLI
  • Template::HAML::Cache
  • Template::HAML::Codegen
  • Template::HAML::Comment
  • Template::HAML::Config
  • Template::HAML::Context
  • Template::HAML::DirectCodegen
  • Template::HAML::DirectEmit
  • Template::HAML::Doctype
  • Template::HAML::Eval
  • Template::HAML::Filter
  • Template::HAML::Filters
  • Template::HAML::Format
  • Template::HAML::Grammar
  • Template::HAML::Helpers
  • Template::HAML::HelpersRole
  • Template::HAML::Interpolation
  • Template::HAML::Lint
  • Template::HAML::Multiline
  • Template::HAML::Node
  • Template::HAML::Plain
  • Template::HAML::Plugin
  • Template::HAML::Renderer
  • Template::HAML::Statement
  • Template::HAML::Tag
  • Template::HAML::TagTransformers
  • Template::HAML::ViewContext
  • Template::HAML::Visitor
  • Template::HAML::Watch
  • Template::HAML::X

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.