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

Kirby Uniform how to get it to work with twig #22

Closed Sananes closed 7 years ago

Sananes commented 7 years ago

Hey guys,

I'm struggling trying to get Kirby uniform to work with Twig, any recommendations as to what I could do? I'm referring to: https://github.com/mzur/kirby-uniform

Thanks in advance! :D

fvsch commented 7 years ago

Dunno, what are you doing and what is failing?

Some recommendations:

Compared to PHP templates, Twig is a more restricted environment where you can do fewer programmy things. PHP templates have access to all variables, classes and functions defined by Kirby and plugins, and to the full power of PHP. In Twig, you can do fewer things and you only have access to what is exposed explicitly (the kirby, site, pages and page objects, most Kirby helper functions, what is returned by controllers and finally anything you enable in configuration).

One possible limit when using a plugin like Uniform is that the few helper functions it uses (honeypot_field, csrf_field and uniform_captcha) will not be available as Twig functions, unless you explicitly declare them:

<?php

c::set([
  'twig.function.csrf_field' => 'csrf_field',
  'twig.function.honeypot_field' => 'honeypot_field',
  'twig.function.uniform_captcha' => 'uniform_captcha'
]);

Another option, if those functions return a HTML string, would be to just use them in your controller and return their value, which will then be available in the template:

<?php
use Uniform\Form;

return function ($site, $pages, $page) {
   $form = new Form(…);
   $csrf = csrf_field();
   $honeypot = honeypot_field();
   return compact('form', 'csrf', 'honeypot');
};
…
{{ honeypot | raw }}
…
Sananes commented 7 years ago

Hi @fvsch thanks for this wonderful reply, the only issue I'm having is with the controller. It doesn't pick up the Uniform\Form part. I just get an error with Variable form does not exist.

Again, thanks a bunch for your reply! I really appreciate it 👍

fvsch commented 7 years ago

You could show a bit of your code here, but I suspect this is not a bug in kirby-twig.