mmontone / djula

Common Lisp port of the Django templating language
http://mmontone.github.io/djula/djula
MIT License
152 stars 21 forks source link

Accessing struct values inside the template #83

Closed challmand closed 9 months ago

challmand commented 2 years ago

Is it possible to reference struct accessors from the template?

I am moving from treating my lists as

(ID 5 NAME "fake")

using the template like the examples {{ person.id }} {{ person.name }}

to person structures

#S(PERSION :ID 5 :NAME "fake")

but I cannot access the values when they are structures instead of key value pair lists.

I was just checking to see if this is possible before trying a different options.

Thanks for any clarifications.

mmontone commented 2 years ago

Djula uses access library for accessing objects.

But access does not seem to work for structs?

I've tested with (access:access (make-person :name "lala") 'name) . And I get NIL. That's strange. I should revise why that is.

mmontone commented 2 years ago

You could try with CLOS instances instead of structs, but we should make it possible to access structs too.

mmontone commented 2 years ago

I've spotted the problem in the access library. Doesn't work for structs.

Problem is here: https://github.com/AccelerationNet/access/blob/1f3440b03823e01bc6f1384daf18626234f86447/access.lisp#L181 and here: https://github.com/AccelerationNet/access/blob/1f3440b03823e01bc6f1384daf18626234f86447/access.lisp#L323

(structure-objects are not considered).

I'll make a pull request soon.

mmontone commented 2 years ago

https://github.com/AccelerationNet/access/pull/20

challmand commented 2 years ago

@mmontone thank you for the quick replies and quick solution. I could explore CLOS instead but my use case is using datafly which uses a defstruct model.

appreciate the help and explanations.

mmontone commented 2 years ago

You have one more option: use the struct generated accessors. person-id and person-name.

So: {{person.person-id}} in your template.

I've tested it and it works.

If you still get NIL, make sure that *djula-execute-package* points to the package that defines your structures.

mmontone commented 2 years ago

If you find Djula too troublesome you could consider TEN, my another attempt at a template system for Lisp, that is more flexible than Djula. (you would still have this accessing problem though, as it is a problem with access library)

mmontone commented 9 months ago

Structure accessing has been integrated into the access library.