gofiber / template

🧬 Template engine middleware for Fiber
https://docs.gofiber.io/guide/templates
MIT License
262 stars 52 forks source link

🤗 [Question]: #347

Closed Jhnvlglmlbrt closed 7 months ago

Jhnvlglmlbrt commented 7 months ago

Question Description

Im having trouble embedding a function using engine.addFunc in my HTML. I have already set Auto Escape to false, but the style just doesn't seem to be applying to the HTML. Any ideas what could be going wrong? I want to add that I use tailwindcss. Perhaps the style is simply not generated this way. But then what to do.

Code Snippet (optional)

Function - 

func CreateEngine() *django.Engine {
    engine := django.New("./views", ".html")
    engine.SetAutoEscape(false)
    engine.Reload(true)

    engine.AddFunc("badgeForStatus", func(status string) (res string) {
        switch status {
        case data.StatusOffline:
            return fmt.Sprintf(`<div class="badge badge-error">%s</div>`, status)
        case data.StatusHealthy:
            return fmt.Sprintf(`<div class="badge badge-success">%s</div>`, status)
        case data.StatusExpires:
            return fmt.Sprintf(`<div class="badge badge-warning">%s</div>`, status)
        case data.StatusExpired:
            return fmt.Sprintf(`<div class="badge badge-error">%s</div>`, status)
        case data.StatusInvalid:
            return fmt.Sprintf(`<div class="badge badge-error">%s</div>`, status)
        }
        return ""
    })
}

Html file - 

                    <tr>
                        <th>Status</th>
                    </tr>
                </thead>
                <tbody>
                    {% for tracking in trackings %}
                    <tr>
                        <td>{{(badgeForStatus(tracking.Status))}}</td>
                    </tr>
                    {% endfor %}
                </tbody>

Checklist:

ReneWerner87 commented 7 months ago

@Jhnvlglmlbrt what is the HTML output after it is rendered ?

ReneWerner87 commented 7 months ago

have you tried https://docs.djangoproject.com/en/dev/ref/templates/builtins/#autoescape

or the safe pipeline see https://github.com/gofiber/template/pull/326#issuecomment-1884948200 -> https://github.com/gofiber/template/tree/master/django#setting-autoescape-using-django-built-in-template-tags

Jhnvlglmlbrt commented 7 months ago

have you tried https://docs.djangoproject.com/en/dev/ref/templates/builtins/#autoescape

or the safe pipeline see #326 (comment) -> https://github.com/gofiber/template/tree/master/django#setting-autoescape-using-django-built-in-template-tags

I see that -

<td><div class="badge badge-success">healthy</div></td>

I used engine.SetAutoEscape(false). There is no need for autoescape. I guess the problem in tailwindcss. Its just cant compile that html into css.

ReneWerner87 commented 7 months ago

okay, but then it has nothing to do with our template engine and everything works as expected

please contact the software experts of the other systems