module Can

Defined in:

can.cr
can/ast.cr
can/cli/render.cr
can/codegen.cr
can/css_scope.cr
can/parser.cr

Constant Summary

VERSION = "0.4.1"

Class Method Summary

Macro Summary

Class Method Detail

def self.raw(s : String) : SafeString #

Wraps content as trusted/pre-escaped. Composes with <.raw> — both end up writing the string verbatim.


[View source]
def self.write_escaped(io : IO, value) : Nil #

Runtime escape used by codegen for every non-raw {expr}. Skips HTML-escaping when the value is already a SafeString.


[View source]

Macro Detail

macro template(path) #

Same as template_inline, but reads the template from a file path resolved relative to the compile-time working directory (typically the project root).

io = IO::Memory.new
user = current_user
Can.template "pages/home.can"

When called inside a method body, top-level <.def> blocks are lowered to local Procs (since Crystal forbids nested defs). Slot-bearing components must therefore be defined at class/module scope.


[View source]
macro template_inline(source) #

Compiles a template literal at macro expansion time and splices the generated Crystal source in-place. The generated code writes HTML to a local io (which the caller is responsible for having in scope).

io = IO::Memory.new
name = "Thomas"
Can.template_inline "<p>Hello, {name}!</p>"
io.to_s # => "<p>Hello, Thomas!</p>"

[View source]
macro use(path) #

Loads a .can file as a component library in the surrounding class/module. Top-level <.def> blocks become methods; top-level render content is rejected so component-only files stay explicit.

module Components
  Can.use "components/card.can"
end

[View source]
macro view(path) #

Loads a .can file as the render surface for the surrounding class. Top-level <.def> blocks become methods, and top-level render content is emitted as render(io : IO).

class HomePage
  Can.view "pages/home.can"
end

[View source]