amteich / kirby-twig

Twig templating support for Kirby CMS
MIT License
48 stars 12 forks source link

Availability of model function in template #21

Closed renestalder closed 3 years ago

renestalder commented 3 years ago

So, I browsed through the documentation and quickly tried whether a function define in a Kirby model is available in the Twig templates, but It doesn't seem to be.

Is there any way to use regular Kirby page models with the Twig plugin?

seehat commented 3 years ago

I think that it should work to call page model methods. For examle a custom model function of a page.

Could you provide an example, which is not working for you?

renestalder commented 3 years ago

Turns out, my mistake was that I tried it with the default template, which of course doesn't map to the content file e.g. models/default.php will not work. After your answer, I started replicating the test with the homepage template, the php version and the Twig version, I now can tell that it was a mistake in how I applied it before.

So, this works flawlessly.

site/models/home.php:

<?php

class HomePage extends Page {
  public function test() {
    return "Test";
  }
}

site/templates/home.twig:

{{ page.test }}

Sorry for stealing your time, and thanks for confirming, that it works.

seehat commented 3 years ago

You are welcome. I'm glad that it works now for you.