Skip to content

Is it possible to name elements of a Vec<String>? #42

@quat1024

Description

@quat1024

So I'm making a personal blog site (surprise), and I wanted to add post tags. No problem, i'll add a field to my struct real quick.

#[derive(Content)]
pub struct Post {
    //...
    pub tags: Vec<String>
}

Now to make a row of links to each tag. Let's see.

{{#tags}}<a href="/post/tag/{{ ? }}">{{ ? }}</a> {{/tags}}

...I'm not sure how to name each item in the Vec. The section repeats 3 times if there are 3 elements in the Vec, but I don't know how to actually refer to the element, to make the sections different.

I was able to solve it using a tuple struct, just because I can name the field 0, like this.

#[derive(Content)]
pub struct Post {
    //...
    pub tags: Vec<Tag>
}

#[derive(Content, Clone, PartialEq, Eq, Hash, Default, Debug)] //idk im not used to tuple structs
pub struct Tag(String);
{{#tags}}<a href="/post/tag/{{0}}">{{0}}</a> {{/tags}}

I'm a little bummed that I needed a useless tuple struct for this though. Is there a nicer way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions