HTMLKit icon indicating copy to clipboard operation
HTMLKit copied to clipboard

Interpolating Template Values

Open danhalliday opened this issue 3 years ago • 9 comments

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")
        }
    }
}

danhalliday avatar Jun 21 '22 18:06 danhalliday

Heya, sorry for the inconvenience.

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

mattesmohr avatar Jun 21 '22 18:06 mattesmohr

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 avatar Jun 21 '22 20:06 danhalliday

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

danhalliday avatar Jun 21 '22 21:06 danhalliday

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 avatar Jun 23 '22 19:06 mattesmohr

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

mattesmohr avatar Jun 27 '22 20:06 mattesmohr

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

it says image

onepayclick avatar Aug 03 '22 23:08 onepayclick

Hi, any good news on this?

onepayclick avatar Sep 20 '22 22:09 onepayclick

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 avatar Sep 21 '22 17:09 mattesmohr

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 https://github.com/vapor-community/HTMLKit/discussions/118. You can already use the 3.0.0-alpha.1 release, if you like. But keep in mind it's still alpha.

mattesmohr avatar Jan 07 '23 21:01 mattesmohr