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

Interpolating Template Values #85

Open
danhalliday opened this issue Jun 21, 2022 · 9 comments
Open

Interpolating Template Values #85

danhalliday opened this issue Jun 21, 2022 · 9 comments
Labels
bug Something isn't working v2

Comments

@danhalliday
Copy link

I’m really struggling to get my head around the design of the templating system. In the following example, how do I access values from the context and use them in the template to build text content and HTML attributes?

import HTMLKitVaporProvider

struct WidgetsIndexView: View {
    typealias Context = ListWidgetsQuery.Data

    var body: AnyContent {
        BasicPage {
            Div {
                Div {
                    Heading1 {
                        Span { "Widgets" }
                            .class("text-3xl")

                        // 1. How to pull a number out of the context?
                        // - Interpolation doesn’t seem to work
                        // - String(context.widgets.count) also doesn’t work
                        Span { "\(context.widgets.count)" }
                            .class("font-mono text-slate-500 ml-auto text-3xl")
                    }
                    .class("flex")
                }

                ForEach(in: context.widgets) { widget in
                    Anchor {
                        Unwrap(widget.name) { name in
                            Span { name }
                        }

                        Span { widget.id }
                            .class("text-lg text-slate-500 ml-auto font-mono")
                    }
                    // 2. How to construct a link dynamically?
                    // - Interpolation doesn’t seem to work
                    // - Adding an extention on `TemplateValue where Value == Widget` also fails
                    .reference("/widgets\(widget.id)/")
                    .class("text-xl flex")
                }
            }
            .class("flex flex-col flex-1 gap-8 p-4 md:p-8")
        }
    }
}
@mattesmohr
Copy link
Member

Heya, sorry for the inconvenience.

Is there a reason why you declare a typealias? Instead of @TemplateValue(ListWidgetQuery.Data.self)?

@danhalliday
Copy link
Author

Is there a reason why you declare a typealias? Instead of @TemplateValue(ListWidgetQuery.Data.self)?

No, no good reason at all — I was having trouble figuring out which APIs to use to render my views from an async controller method and going back and forth between APIs in HTMLKitVaporProvider and HTMLKit and while trying to get it all working, I tried a bunch of different incantations for defining the context as I wasn’t sure I had it right. I think I have the basic setup in place now though, and I’m on to specific issues within the views.

@danhalliday
Copy link
Author

I noticed this line in the sample project, but couldn’t figure out how it could work.

.reference("context.route.baseUrl/show/context.item.id")

https://github.com/mattesmohr/Website/blob/f2031e42155fde6558d19a69c45e8b2b2c75069d/Sources/App/Views/ArticlePageTemplate.swift#L93

@mattesmohr
Copy link
Member

I think I quoted it out for a reason, as it is a String. Let me come back to you at the weekend, okay?

@mattesmohr
Copy link
Member

I was looking into the code, and I found something. Let me see, if I can fix it the next couple days.

@mattesmohr mattesmohr added the bug Something isn't working label Jun 27, 2022
@onepayclick
Copy link

I'm having the same issue :(
Can't unwrap the value inside a ForEach
image

it says
image

@onepayclick
Copy link

Hi, any good news on this?

@mattesmohr
Copy link
Member

Hi, any good news on this?

Honestly, no.. not really. Had a lot on my plate.

In your case, you should be able to use my hack month.1.rawValue, as long it is a string value. But don't lean to much on it. I want to rework it as well.

@mattesmohr
Copy link
Member

I want to let you know, that I remove the pre-rendering. Therefore you will be able to use string interpolation. You can read more about in #118. You can already use the 3.0.0-alpha.1 release, if you like. But keep in mind it's still alpha.

@mattesmohr mattesmohr added the v2 label Jun 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v2
Development

No branches or pull requests

3 participants