LoginSignup
11
0

More than 5 years have passed since last update.

まだ焦る時じゃないので

Posted at

PhalconについてくるVoltについて俺的めも

Voltテンプレート

PHPの関数とかをテンプレートで使いたいことがよくあるけど
http://phalcon-docs-ja.readthedocs.io/ja/latest/reference/volt.html#id4
Voltではここで定義されてるのしか利用できない。

以下のようにaddFunctionしてやると使えるようになる。

    $di->set('view', function () use($di) {
        $view = new View();
        $view->setViewsDir(__DIR__ . '/views/');

        $view->registerEngines(array(
            '.volt' => function ($view, $di) {

                $volt = new View\Engine\Volt($view, $di);

                $volt->setOptions(array(
                    'compiledPath' => __DIR__ . '/cache/',
                    'compiledSeparator' => '_',
                    'compileAlways' => true
                ));

                $compiler = $volt->getCompiler();
                $compiler->addFunction(
                    'mb_substring',
                    function ($resolvedArgs, $exprArgs) use ($di) {
                        return 'mb_substr(' . $resolvedArgs . ')';
                    }
                );

                return $volt;
            },
            '.phtml' => 'Phalcon\Mvc\View\Engine\Php'
        ));
        return $view;
    });

voltのほうで

{{ mb_substring('あいうえお', 0, 2) }}

みたいな感じで使えるようになる。

こんなに簡単に関数はできるというのに。。。

11
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
11
0