rawleyfowler / Humming-Bird

A fun and performant web application framework for Raku
MIT License
44 stars 6 forks source link

Stash for templates? #59

Closed swaggboi closed 1 year ago

swaggboi commented 1 year ago

Hello I'm trying to populate a template with something that the user submits in a POST body:

use Humming-Bird::Core;
use Template6;

my $templates = Template6.new;
$templates.add-path: 'templates';

my $router = Router.new(root => '/');

$router.post(-> $request, $response {
    my $output-value = $request.content.{'input-value'};

    $response.html($templates.process: 'index', :$output-value);
});

$router.get(-> $request, $response {
    $response.html($templates.process: 'index');
});

The template can just be:

<h1>Cool template</h1>
[% IF output-value %]
<p>Output value is: [% output-value %]</p>
[% END %]

The problem I'm having is that 'output-value' lives on until someone submits another. I expected it to just exist for that one request/response transaction. I see there's a 'stash' in the Middleware class; is this something I can or should use to pass this value to the template and then be 'forgotten' after the transaction?

rawleyfowler commented 1 year ago

Yup. Please see this member of the HTTPAction parent class.

TLDR: There is a .stash Hash on each Request and Response that is simply transient storage that only lives during each message, and is discarded alongside the message itself, when the message finishes.

rawleyfowler commented 1 year ago

This is definitely a Template6 issue if I'm comprehending this correctly. And using the stash won't actually change anything. T6 isn't in a great state IMO, I would strongly recommend using Mustache instead.

Just to be super clear, you make the post request, then the get request and the get request has the post value still set in the template?

swaggboi commented 1 year ago

Thanks for the response (pun absolutely intended). I actually popped in to reply to your first one to say the same: I think this is a Template6 problem and I probably should've just used Mustache as I saw in your examples. I went with Template6 because I had some familiarity with TT but I hit another bug after trying to pass my $response.stash hash to the template.

My time would've been better spent figuring out Mustache!

Just to be super clear, you make the post request, then the get request and the get request has the post value still set in the template?

That is correct. I tested from two different clients and one can see the value from the other. Thought I was losing it.

rawleyfowler commented 1 year ago

@swaggboi I'm going to close this as complete, please re-open it if you think think this was improperly closed. Thanks!