Skip to content

Instantly share code, notes, and snippets.

@librasteve
Created December 8, 2024 20:00
Show Gist options
  • Save librasteve/2b2ee2f2739d50c274032ae28a27a717 to your computer and use it in GitHub Desktop.
Save librasteve/2b2ee2f2739d50c274032ae28a27a717 to your computer and use it in GitHub Desktop.
Cromponent Nests
#!/usr/bin/env raku
use lib "lib";
use Cro::WebApp::Template;
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use Cromponent;
class Col {
has $.data is required;
method RENDER {
warn $!data.raku; $*ERR.flush;
render-template q:to/END/, {:$!data};
<td></td>
END
}
}
class Row {
has $.data is required;
method RENDER {
warn $!data.raku; $*ERR.flush;
my @cols = do for $!data -> $data { Col.new: :$data }
render-template q:to/END/, {:@cols};
<tr>
<@.cols: $col>
<&Col($col)>
</@>
</tr>
END
}
}
class Table {
has $.data is required;
method RENDER {
my @rows = do for $!data -> $data { Row.new: :$data }
render-template q:to/END/, {:@rows};
<table>
<@.rows: $row>
<&Row($row)>
</@>
</table>
END
}
}
my $routes = route {
my $table = Table.new: data => [[1,2],[3,4]];
get -> {
template-with-components Q:to/END/, { :$table };
<html>
<head>
<script src="https://unpkg.com/[email protected]" integrity="sha384-0895/pl2MU10Hqc6jd4RvrthNlDiE9U1tWmX7WRESftEDRosgxNsQG/Ze9YMRzHq" crossorigin="anonymous"></script>
</head>
<body>
<table>
<&Table($table)>
</table>
</body>
</html>
END
}
#iamerejh - sort out id
add-component Table, :load( -> UInt() $id { @todos.first: { .id == $id } }),;
add-component Row;
add-component Col;
}
my Cro::Service $http = Cro::HTTP::Server.new(
http => <1.1>,
host => "0.0.0.0",
port => 3000,
application => $routes,
);
$http.start;
say "Listening at http://0.0.0.0:3000";
react {
whenever signal(SIGINT) {
say "Shutting down...";
$http.stop;
done;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment