fvsch / kirby-twig

Twig templating support for Kirby CMS 2. For Kirby 3, use https://github.com/amteich/kirby-twig
MIT License
70 stars 8 forks source link

How to load twig templates with Kirby function tpl::load #23

Closed designbydc closed 7 years ago

designbydc commented 7 years ago

Hi,

is it possible to load or pass twig templates with the tpl::load() function?

array(
    'pattern' => array('(:any)/(:any)/details/(:any)'),
    'action'  => function($i18n, $section, $details) {
        $items_section = site()->visit($section, $i18n)->children()->visible();
        $items_topic = $items_section->children()->findBy('slug', $details);

        if ( isset($items_topic) && !empty($items_topic) ){
            tpl::$data = array_merge(tpl::$data, array(
                'kirby' => kirby(),
                'site' => site(),
                'pages' => pages(),
                'page' => $items_topic
            ), $items_topic->templateData());

            echo tpl::load( kirby()->roots()->templates() . DS . 'sometemplate.twig');
        }
    },
    'method' => 'GET'
)

Of course this doesn't works because the file will not be rendered! Maybe someone can help me please or can show me another solution?

fvsch commented 7 years ago

Kirby’s tpl::load will only load PHP scripts. But you should be able to use the twig() helper function instead. See the README: https://github.com/fvsch/kirby-twig#rendering-a-template-in-php-the-twig-helper

designbydc commented 7 years ago

I've tried just the twig() helper function but I get the error "Call to undefined function twig()". I also register the Twig Plugin first with Kirby\Twig\Plugin::register(); but than I get a 500 error.

fvsch commented 7 years ago

See my answer on the kirby forum: https://forum.getkirby.com/t/how-to-set-twig-files-in-template-loader/7866/7?u=fvsch