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

Is it possible use response to render html string? #1538

Open
diuming opened this issue Nov 7, 2020 · 3 comments
Open

Is it possible use response to render html string? #1538

diuming opened this issue Nov 7, 2020 · 3 comments

Comments

@diuming
Copy link

diuming commented Nov 7, 2020

Context and Description

Is it possible use response to render html string?

Environment Details

macOS 10.15.6
Xcode 11.6
Swift 5.2.4

Expected

app.router.post("/some/path", handler: post)

func post(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
    .
    .
    let html = try response.renderHTML("My.stencil", context: context)
    try response.send(json: ["status": true, "html": html]).status(.OK).end()
}

renderHTML(context: options:) in RouterResponse.swift

public func renderHTML(_ resource: String, 
                       context: [String: Any],
                       options: RenderingOptions = NullRenderingOptions()) throws -> String {
    guard let router = getRouterThatCanRender(resource: resource) else {
        throw TemplatingError.noTemplateEngineForExtension(extension: "")
    }
    return try router.render(template: resource, context: context, options: options)
}
@dannys42
Copy link
Contributor

dannys42 commented Nov 8, 2020

I think it should be fine. Just make sure you set your Content-Type to text-html.

https://nocturnalsolutions.gitbooks.io/kitura-book/content/7-templating.html

@diuming
Copy link
Author

diuming commented Nov 9, 2020

@dannys42 thanks.

could you adding this function in the future?

@mbarnach
Copy link
Member

@diuming Are you able to make a PR for this feature? It would be great!

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