zendframework / zend-expressive-zendviewrenderer

zend-view PhpRenderer integration for Expressive
BSD 3-Clause "New" or "Revised" License
11 stars 12 forks source link

Doctype in Expressive? #49

Closed mano87 closed 6 years ago

mano87 commented 6 years ago

How can we correctly set the doctype to HMTL5 in a zend-expressive application so that all ViewHelpers work properly. In the view this does not really make sense.

<?php echo $this->doctype('HTML5') . PHP_EOL; ?>

froschdesign commented 6 years ago

@mano87 You can use an own factory for the Doctype helper.

Example:

namespace My\View;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\View\Helper\Doctype;

class DoctypeFactory implements FactoryInterface
{
    public function __invoke(
        ContainerInterface $container,
        $requestedName,
        array $options = null
    ) {
        $helper = new Doctype();
        $config = $container->get('config');
        if (array_key_exists('view_helper_config', $config)
            && array_key_exists('doctype', $config['view_helper_config'])
        ) {
            $helper->setDoctype($config['view_helper_config']['doctype']);
        }

        return $helper;
    }
}

Now you can set the doctype in your config: (e.g. templates.config.php)

return [
    'dependencies' => [
        // …
    ],

    'templates' => [
       // …
    ],

    'view_helpers' => [
        'factories' => [
            Zend\View\Helper\Doctype::class => My\View\DoctypeFactory::class,
        ],
    ],

    'view_helper_config' => [
        'doctype' => Zend\View\Helper\Doctype::HTML5,
    ],
];

(In a zend-mvc based application it is done by the Zend\Mvc\Service\ViewHelperManagerFactory::createDoctypeHelperFactory().)

mano87 commented 6 years ago

Oh thank you. Stupid that I did not think so myself ...