Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing a list of hash table #77

Open
asarch opened this issue Mar 20, 2022 · 2 comments
Open

Parsing a list of hash table #77

asarch opened this issue Mar 20, 2022 · 2 comments

Comments

@asarch
Copy link

asarch commented Mar 20, 2022

If you would:

(render #P"foods.html"
    (list :foods '((:id 10 :nombre "Pizza" :origin "Italy")
                   (:id 32 :nombre "Sushi" :origin "Japan")
                   (:id 92 :nombre "Taco" :origin "Mexico"))

How would you get the values of every hash table in the list?

{% for food in foods %}
	{% for (key . value) in food %}
		<p>{{ value }}</p>
	{% endfor %}
{% endfor %}

does not work

@mmontone
Copy link
Owner

I'm not sure you can.

You could pass preprocessed values and work with that:

(defun plist-values (plist)
	   (loop for v in (cdr plist) by #'cddr
		 collect v))

(let ((foods '((:id 10 :nombre "Pizza" :origin "Italy")
                   (:id 32 :nombre "Sushi" :origin "Japan")
                   (:id 92 :nombre "Taco" :origin "Mexico"))))
	   (list :foods foods
		 :foods-values (mapcar 'plist-values foods)))

@vindarel
Copy link

strictly speaking :foods is a list of plists right? (not hash-tables)

+1 for Mariano's preprocessing, foods-values looks like this:

 :FOODS-VALUES ((10 "Pizza" "Italy") (32 "Sushi" "Japan") (92 "Taco" "Mexico")))

or yet make it look like

'(((:id 10) (:nombre "pizza") (:origin "Italy)
   (:id …) (…)))

(more parens…)

or (?) loop over the list and check if the value is a key by checking if it starts with a colon (could be done easily with a template filter, they are easy to create),

or return a list of structs or objects, etc.

my 2c :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants