File Structure Query

Hi,

I am new to Kirby and have decided to migrate a client website from Wordpress to Kirby “from scratch”.

Everything is going smoothly at this stage (more trouble mimicking the template than the actual backend) but I have run into a problem.

In the Wordpress version, there are “default files”. By which if you do not have a hero-image for your post, for example, it will use a default image that is set as part of the configuration.

Obviously these defaults should not reside with the blog posts but rather somewhere “common” and I thought assets would be ideal - however I am having a tough time getting hold of the file.

I have tried $file, $site, even $page.

Here is the little snippet I am using at the moment - any help would be appreciated.

<?php // set up page variables to use $feature_file = $page->image($page->feature()); if (!$feature_file) { echo "NOT HERE"; $feature_file = *** This is where I want to grab the default (resides in assets/defaults/blog_image.jpg *** echo $feature_file->url(); } $feature_url = $feature_file->url(); ?>

D

You could try this. The base url of the website concatenated with your asset URL.

<?php $feature_file = $site->url() . '/assets/defaults/blog_image.jpg'; ?>

Tell me if this works!

The code you suggested constructs the correct file path - I have tried this before - however it does not create a $file object with which I can use the $file->url() method.

The response from the server I am receiving is

Fatal error: Call to a member function url() on a non-object in /path-to/blogentry.php on line 7

Line 7 correlates to the echo $feature_fle->url(), but even if I delete that line the same error will occur when I try to set $feature_url to $feature_file->url();

Thank you,
D

I belived you can use the toFile() method to create a $file object out of it.

Does $feature_fle->toFile()->url() work?

url('assets/defaults/blog_image.jpg') should just work fine for image assets.

1 Like

Unfortunately toFile() only works on objects. If you note in the code (in the if statement) I am constructing the file location as a string.

D

Yes this does work, however I would have to modify the if statement to handle $feature_file AND $feature_url differently. I was hoping that after testing, I could reduce the if statement to a 1-liner.

Any other suggestions? Do not misunderstand, you have solved my problem, I was just hoping for some elegancy in my finished code.

D

Why don’t you put the file into site files, i.e. the “root” of the content folder, next to site.txt. That way, the user would be able to change the default image.

http://getkirby.com/docs/cheatsheet/site/images

1 Like

Letting the user change the files is not a bad thing (when I get to designing the panels etc) and perhaps it is the best way, however I would imagine there is an overhead when using $site->images() to grab lists of images all of the time.

I did originally think about this as the place to put it, however it just didn’t look right structurally (in a visual sense).

I have currently used distantnative’s solution since it works, however when I do the back end I will most likely migrate it. It is now a simple matter of changing one line for that to occur.

Thank you all for the help.
D

P.S. How do I credit a poster for helping with the solution? Or is that something not used on this forum?

You can also just grab a single image http://getkirby.com/docs/cheatsheet/site/image

We are currently considering installing an “accepted answer” plugin. But in the meantime, you can “heart” a post.