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 get file URL #28

Closed jimhaakman closed 4 years ago

jimhaakman commented 7 years ago

When using page.field.toFile.url it returns (unknown) while page.field.toFile returns the filename and dump(page.field.toFile) returns an object. How do I get the URL?

fvsch commented 7 years ago

Hi. Not sure this is related to Twig. If you try with a PHP template, can you get the URL? For example with:

<?php echo $page->myfield()->toFile()->url(); ?>

In Twig you would use the same Kirby API, the only difference is the slightly shorter syntax.

If you can't get a correct result in PHP, a better place to ask would be the Kirby forum or, if you believe it's a bug in Kirby, https://github.com/getkirby/kirby/issues

jimhaakman commented 7 years ago

I get a result in php, the url of the file http://localhost:3000/content/test/test.mp4 instead of (unknown).

fvsch commented 7 years ago

Apparently we should call the url method explicitly:

{{ page.myfield.toFile.url }} -> ""
{{ page.myfield.toFile.url() }} -> "/content/foo/bar.jpg"

That's usually not needed in Twig in my experience (and according to their doc) but it seems this object class is implemented in a way that is fooling Twig into getting a null value.