LoginSignup
1
0

More than 5 years have passed since last update.

twig で 独自フィルターを追加

Posted at

自分用メモ

TwigCustomFilter.php
<?php

class TwigCustomFilter {
    private $customFilterList = [
        'ord' => 'twig_ord'
    ];
    public function getCustomFilterList() {
        return $this->customFilterList;
    }

    public function twig_ord($input) {
        if (!empty($input)) {
            return ord($input);
        } else {
            return 0;
        }
    }
}
        Twig_Autoloader::register();
        $loader = new Twig_Loader_Filesystem($templateDir);

        $twig = new Twig_Environment($loader, []);

        $twigCustomFilter = new TwigCustomFilter();
        $customFilterList = $twigCustomFilter->getCustomFilterList();
        foreach ($customFilterList as $filterName => $functionName) {
            $filter = new Twig_SimpleFilter($filterName, [$twigCustomFilter, $functionName]);
            $twig->addFilter($filter);
        }
        $template = $twig->loadTemplate($templateFileName);
        $template->display($model->getValues());
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0