Skip to content

Instantly share code, notes, and snippets.

@bastianallgeier
Last active May 7, 2016 17:44
Show Gist options
  • Select an option

  • Save bastianallgeier/edf33e468e3c022e4772 to your computer and use it in GitHub Desktop.

Select an option

Save bastianallgeier/edf33e468e3c022e4772 to your computer and use it in GitHub Desktop.
<?php
kirby::$handlers['template'] = function($kirby, $page, $data = array()) {
require_once(__DIR__ . DS . 'vendor' . DS . 'autoload.php');
$loader = new Twig_Loader_Filesystem($kirby->roots()->templates());
$twig = new Twig_Environment($loader, array(
'cache' => false
));
$data = array_merge(tpl::$data, array(
'kirby' => $kirby,
'site' => $kirby->site(),
'pages' => $kirby->site()->children(),
'page' => $page
), $page->templateData(), $data, $kirby->controller($page, $data));
return $twig->render(basename($page->templateFile()), $data);
};
@shiftsave

Copy link
Copy Markdown

What are your thoughts on developing this plugin further? This would be the last step in the automated process I created with Gulp to work amazing :)

I hit a roadblock since I am using Jade for templating and Jade-PHP isn't the greatest, a little hacky.

@bastianallgeier

Copy link
Copy Markdown
Author

@shiftsave It sucks so badly that I didn't get any notifcation about your comment. I'm about to add the handlers api to the next version of Kirby, which makes this kind of plugins super easy. I'm not actually planning to be the main developer of a twit plugin, but I'd love to through a first version out there for a real twig expert to extend it. The same could happen for Jade.

@lehni

lehni commented Jul 24, 2015

Copy link
Copy Markdown

This looks good, but why not leave the creation of the $data object to the caller? This will probably always be the same anyway, and is already defined here:

https://github.com/getkirby/kirby/blob/master/kirby.php#L666

Also, why is tpl::$data overridden on each call to $kirby->template()?

@fvsch

fvsch commented May 7, 2016

Copy link
Copy Markdown

@shiftsave You might not need it anymore after close to one year, but for the record I made a Twig template component for Kirby 2.3: https://github.com/fvsch/kirby-twig
(And for other PHP templating engines, I guess it’s an okay enough example to help get people started.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment