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

Ability to build an object or hashmap? #898

Open
absolutejam opened this issue Feb 8, 2024 · 8 comments
Open

Ability to build an object or hashmap? #898

absolutejam opened this issue Feb 8, 2024 · 8 comments

Comments

@absolutejam
Copy link

absolutejam commented Feb 8, 2024

Hey!

I'm trying to build some grouping functionality for my Zola blog and I wondered if it is currently possible (or planned) to build and mutate a JSON object or hashmap data structure?

For example, I'm currently iterating over an array of objects that each have their own tags.
I'd like to be able to group these objects into by their tags, ending up with a Hashmap<String, Vec<MyObject>> in rough Rust terms.

It looks like this would probably want to live in https://github.com/Keats/tera/blob/master/src/builtins/filters/object.rs as this is already concerned with handling serde_json::Value::Objects?

I'm happy to take a stab at this if you're okay with it 👍

@schungx
Copy link

schungx commented Apr 20, 2024

This will be tremendously interesting to me. It will drastically reduce the number of parameters I have in macros and simplify a lot of code if optional fields can be grouped inside a single config object instead of each being a variable.

A way to create a hash literal is needed as well as the ability to set a field of an object.

@schungx
Copy link

schungx commented Apr 20, 2024

@absolutejam your suggestion gives me an idea.

We can very simply write such a function, say make_obj, that returns a hash object from its parameters.

Until the native syntax arrives, this can be a huge boom in productivity as I can then write macros that take objects as parameters.

EDIT: I confirm that the idea works. I have obj function and mixin/polyfill filters and all are looking good!

@Keats
Copy link
Owner

Keats commented Apr 20, 2024

What's not defined yet is what would be the syntax to update a hashmap

@schungx
Copy link

schungx commented Apr 22, 2024

I solved it via a mixin filter:

{{ map | mixin(foo=42, bar=true) }}

will return a new map with the two properties replaced.

Setting properties then become:

set map = map | mixin(foo=42, bar=true)

@Keats
Copy link
Owner

Keats commented Apr 23, 2024

Yeah the filter is the obvious one, it's just a bit verbose to set new things but maybe it's the simpler one

@schungx
Copy link

schungx commented May 1, 2024

If you're looking for syntax suggestions, I suggest:

{% set_prop map.foo = 42 %}
{% set_prop map["foo"] = 42 %}

{% remove_prop map.foo %}
{% remove_prop map["foo"] %}

@Keats
Copy link
Owner

Keats commented May 3, 2024

Or maybe just {% set map.foo = 42 %}? I don't know if we will need to remove things from a hashmap. I have never needed that in any template engine (so far)

@schungx
Copy link

schungx commented May 4, 2024

Or maybe just {% set map.foo = 42 %}? I don't know if we will need to remove things from a hashmap. I have never needed that in any template engine (so far)

That would work too...

In that case we can do delete map.prop or delete_prop map.prop like JavaScript.

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