saimn / sigal

yet another simple static gallery generator
http://sigal.saimon.org/
MIT License
888 stars 168 forks source link

Add .tojson() method to Album and other classes #373

Open defeo opened 5 years ago

defeo commented 5 years ago

For writing themes based on purely frontend technologies (e.g., VueJS), it would be useful if the Gallery, Album, etc. classes could be easily serialized to JSON.

Jinja supports a tojson filter, which is based on Python's default json module, however most of the data passed to templates in the album dict is not JSON-serializable, thus leading to writing ugly code such as (never mind the early draft, plz)

https://github.com/defeo/sigal-theme/blob/68e3dae1e7a1b94578626d9d587aca3cded1a3cc/templates/album.html#L16-L49

It is not easy to hook into the way the json module serializes objects: having Jinja's tojson work with the Album class would require changes to Jinja's code. Thus I believe the simplest solution would be to add a tojson() method to these classes in Sigal.

If we can agree on a course of actions, I am willing to write a pull request.

saimn commented 5 years ago

Hi, Interesting, I need to think a bit more about it but it seems a good idea. The galleria theme already use some json serialization so we could maybe use this as well.