rubellum / Slim-Blade-View

Slim Framework 3 view helper built on Blade component
MIT License
18 stars 7 forks source link

A bit of documentation would not hurt #1

Open judgej opened 8 years ago

judgej commented 8 years ago

It really wouldn't, even if it was to ask for help or tell us this package is incomplete.

judgej commented 8 years ago

What I did to get it working:

In index.php set up the blade container:

$container = $app->getContainer();
$container['blade'] = function($container) {
    return new \Slim\Views\Blade(
        $container['settings']['renderer']['blade_template_path'],
        $container['settings']['renderer']['blade_cache_path']
    );
};

The settings include:

<?php
return [
    'settings' => [
        'displayErrorDetails' => true, // set to false in production

        // Renderer settings
        'renderer' => [
            'template_path' => __DIR__ . '/../templates/', // Default .phtml templates
            'blade_template_path' => __DIR__ . '/../blade/', // String or array of multiple paths
            'blade_cache_path' => __DIR__ . '/../cache/', // Mandatory by default, though could probably turn caching off for develpment
        ],
        ....

This puts my blade template path in /blade and the compiled templates in /cache off the Slim root directory. I'm keeping them away from other templates that may use other templating systems, though that may not be necessary given the blade template naming conventions.

The templates are created in /blade, e.g. blade/index.blade.php.

This can be rendered in the router like this:

return $this->blade->render($response, 'index', $args);

All the normal dot-notation for specifying directories will work here. Use your normal @extends(), @yield() and @section() constructs to do their thang.

It looks like the templates can be put into multiple root directories - just supply a list in an array. I'm not tried this, but I assume the use-case is to be able to pull in templates from other packages and theme overrides, without having to copy everything into one place.

judgej commented 8 years ago

I do like blade for its simplicity and easy learning curve, and is just so much nicer to look at and interpret than Twig templates. But each to their own :-)

judgej commented 8 years ago

I'm grabbing a fork just so I have a snapshot for a current project, until I know more about what future is in store for this package.

judgej commented 8 years ago

Now, the default phtml Slim 3.0 container seems to register itself under $container['renderer']. I'm not sure how that works or why extra work is needed here to explicitly register the service, so I'm sure there will be some changes to the above documentation.