notrab / dumbo

A lightweight, friendly PHP framework for HTTP.
MIT License
158 stars 16 forks source link

feat: Render View #48

Closed rayblair06 closed 1 month ago

rayblair06 commented 2 months ago

In this PR, you'll find examples of how Dumbo could render views using template engines like Blade, Latte, Twig, or really any template engine, all within the Context service. Let me know your thoughts!

notrab commented 1 month ago

@rayblair06 do you think it's worth adding something like this?

public function shareWithViews(string $key, $value): void
{
  $this->sharedViewData[$key] = $value;
}

public function view(...$params): ResponseInterface
   {
       // Merge shared data with view-specific data

       $viewData = array_merge($this->sharedViewData, $params[1] ?? []);
       $params[1] = $viewData;

       // ... 
   }
rayblair06 commented 1 month ago

@notrab would this be to share global variables across views?

$app->get("/", function ($context) {
    $context->shareWithView('user', [
        'username' => 'Ray'
    ]);
    ...
});

$app->get("/foo", function ($context) {
    // Would have access to user['username'] without having to pass it?
    $context->view('view', []);
});
notrab commented 1 month ago

@notrab would this be to share global variables across views?


$app->get("/", function ($context) {

    $context->shareWithView('user', [

        'username' => 'Ray'

    ]);

    ...

});

$app->get("/foo", function ($context) {

    // Would have access to user['username'] without having to pass it?

    $context->view('view', []);

});

I thought anyway @rayblair06 but perhaps it's not the best idea. Let