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

Reuse HML components #232

Open
beastgrim opened this issue Jun 16, 2024 · 1 comment
Open

Reuse HML components #232

beastgrim opened this issue Jun 16, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@beastgrim
Copy link

So I have reusable HTML component, ex textfield. For example its look like this:
textfield.leaf

<div class=#(class)>
    <p>
        <label class="label">#(title)</label>
        <br>
        <span data-name="#(name)">
        <input size="40" id="#(id)" placeholder="#(placeholder)" value="#(value)" type="#(type)" name="#(name)" accept="#(accept)">
        </span>
    </p>
</div>

Also I have data for this HTML component:

struct TextFiled: Encodable {
    var `class`: String?
    var title: String?
    var name: String?
    var placeholder: String?
    var value: String?
    var type: String?
    var accept: String?
}

struct Form: Encodable {
    var email: TextFiled
    var phone: TextFiled
    var address: TextFiled
}

I send data to render like this:

func routes(_ app: Application) throws {
    app.get { req async throws in
        return try await req.view.render("index", Form(
            email: TextFiled(...),
            phone: TextFiled(...),
            address: TextFiled(...)
       ))
    }
}

And I can't realise how to pass data to reusable textfields?
Intuitively it should be something like this, but it doesn't work.

<div class="form">
#extend("textfield") { #(email) }
#extend("textfield") { #(phone) }
#extend("textfield") { #(address) }
</div>

Thanks for any help how to do it.

@beastgrim beastgrim added the enhancement New feature or request label Jun 16, 2024
@0xTim
Copy link
Member

0xTim commented Aug 20, 2024

You can print out the context in the render function when debugging to see how it's passed in, but it should be something like email.name - Leaf uses Codable so it's based off keys rather than types

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

No branches or pull requests

2 participants