imichall
1/7/2019 - 10:22 AM

Nette vlastní filter , helper, latte

Nette vlastní filter , helper, latte

services:
    nette.latteFactory:
        setup:
            - addFilter(abs, @petrjirasek\Latte\AbsFilter)

    - petrjirasek\Latte\AbsFilter
services:
    - petrjirasek\Latte\AbsFilter
# https://www.janpecha.cz/blog/poznamky/nette-registrace-latte-filtru/

services:
        nette.latteFactory:
                setup:
                        - addFilter('nazevFiltru', ['Trida', 'statickaMetoda'])
                        - addFilter('nazevFiltru2', @Sluzba::metoda)
<?php

 protected function beforeRender()
    {
        $this->template->addFilter('abs', function ($number) {
            return abs($number);
        });
    }
<?php 

// jako cela trida, bez zavislosti

namespace petrjirasek\Latte;

use Nette\Object;


class AbsFilter extends Object
{

    /**
     * @param int $number
     * @return int
     */
    public function __invoke($number)
    {
        return abs($number);
    }
}




// PRESENTER

use petrjirasek\Latte\AbsFilter;

protected function beforeRender()
{
    $this->template->addFilter('abs', new AbsFilter);
}
<?php
// se zavislostmi

use petrjirasek\Latte\AbsFilter;

/** @var AbsFilter @inject */
public $absFilter;

protected function beforeRender()
{
    $this->template->addFilter('abs', $this->absFilter);
}