[TYPO3-dev] Re: Register namespace for ViewHelpers

Daniel Haring daniel at haring.co.at
Mon Dec 21 15:06:12 CET 2015


Hi Jens,

you should be able to achieve this by extending the Template Parser.



~/typo3conf/ext/myext/Classes/Parser/TemplateParser.php:

[php]
<?php

namespace Vendor\Myext\Parser;

class TemplateParser extends \TYPO3\CMS\Fluid\Core\Parser\TemplateParser{

    public function __construct() {
        $this->namespaces = [
            'f' => 'TYPO3\\CMS\\Fluid\\ViewHelpers',
            'myns' => 'Vendor\\Myext\\ViewHelpers'
        ];
        parent::__construct();
    }

    protected function reset() {
        $this->namespaces = [
            'f' => 'TYPO3\\CMS\\Fluid\\ViewHelpers',
            'myns' => 'Vendor\\Myext\\ViewHelpers'
        ];
    }

}
[/php]



~/typo3conf/ext/myext/ext_typoscript_setup.txt:

[code]
config.tx_extbase.objects{
    TYPO3\CMS\Fluid\Core\Parser\TemplateParser{
        className = Vendor\Myext\Parser\TemplateParser
    }
}
[/code]



After this (and clearing caches), you should be able to call your ViewHelpers via '<myns:myviewhelper />' without the need of specifying your namespace within every template.

Tested in TYPO3 CMS v 7.6.0 LTS.

Cheers,
Daniel


Quote: mjns wrote on Thu, 17 December 2015 15:26
----------------------------------------------------
> Hello,
> 
> I have an extension with many custom ViewHelpers and it bothers me that I have to write the namespace declaration each time in the templates.
> 
> After some research I have found the way how does Fluid registered his namespace.
> 
> TYPO3\CMS\Fluid\Core\Parser\TemplateParser.php
> [php]
> protected $namespaces = array(
> 	'f' => 'TYPO3\\CMS\\Fluid\\ViewHelpers'
> );
> [/php]
> 
> I have tried to extend the array with a custom identifier with namespace, but it does not work.
> Is there a way to register a identifier with namespace for custom ViewHelpers of an extension?
> 
> Cheers 
> Jens
----------------------------------------------------



More information about the TYPO3-dev mailing list