How to check whether an image exists before inserting it in the template? #3135
Unanswered
HeuristicPerson
asked this question in
Q&A
Replies: 1 comment 6 replies
-
I would like to see some examples of how you handle these things (how you define them and how template adds them) to give concrete suggestions. For example in (1), I don't get why you would have non-existent files or the need to check the existence of them. Typical way to handle these is to define a consistent metadata within your content and use that in the templates. i.e. you can provide a |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a small blog using pelican. For simplicity, I've added a cover image to each of the entries that is called like the main document but with.jpg at the end. Then, from the template, it's easy to show that image.
Now, I have two related problems:
I would like to be able to check for the existence of the image, and if it's not there, not insert anything into the final HTML. What would be the best way to achieve that? Modifying the template? Creating a filter? I've tried checking for the existence of the file in the template using os module with something similar to
{% if (os.path.isfile(MY_FILE) %}
but I receive an error that os is not recognised.Similarly, I would like to check for different files and show them in different ways. e.g. having two files called like
entry_title_cover.jpg
andentry_title_miniature.jpg
; what would be the best solution for that?My understanding is custom filters (defined or called in
pelicanconf.py
) can only access the data contained within the entries, not in external files like my image paths, so I don't really know how to attack the problem.Many thanks in advance for your help, and also to the authors of Pelican. I've been using it just for one month or so but I'm really impressed by its simplicity and how weel it works.
Beta Was this translation helpful? Give feedback.
All reactions