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

Possible to access others Kirby variables? #21

Closed andregoldstein closed 7 years ago

andregoldstein commented 7 years ago

Hi there, is it possible to access other Kirby variables using Twig for example $user or $users?

Thanks

fvsch commented 7 years ago

Hi. I don’t think Kirby is exposing $user and $users variables to templates. This is an example from the Kirby documentation:

<?php
$user = site()->user();

if($user->can('myplugin.permission', ['arg1', 'arg2'])) {
  // user has permission
} else {
  // user has no permission
}

Similarly, in Twig:

{% set user = site.user %}
{% if user.can('myplugin.permission', ['arg1', 'arg2']) %}
  {# user has permission #}
{% else %}
  {# user has no permission #}
{% endif %}

Note that if you want to write a lot of logic, you should probably do it in PHP in a controller, and only output your prepared data in the template. Twig is (in my opinion) simpler than PHP for outputting content, but it’s more limited and sometimes a bit verbose for logic and preparing content.

andregoldstein commented 7 years ago

Brilliant, many thanks for pointing that out!