if condition in html template

259 views
Skip to first unread message

ivo.he...@gmail.com

unread,
Sep 11, 2017, 5:53:10 AM9/11/17
to golang-nuts
Hello golang-nuts,

My problem: in a html page, I iterate over a list (of strings). the items are names of backupfiles. Sometimes, a backup has failed, in this case, the backupfilename contains the token .FAILED. 
when i print the list of items, I would like to check if I have to print a restore-link or not. Obviously, I should not print a restore link for backups that contains the token .FAILED.

    <table width="100%" class="backups" border="0">
      {{ range $element := .Items }}
      <tr>
        <td>{{ $element }}</td>
        {{ if CONDITION }}
          <td><a href="javascript:action('restore','{{$element}}');">Restore</a></td>
        {{ end }}
      </tr>
      {{ end }}
    </table>

So i need a regex or -contains- function for CONDITION. But I only found the  eq, ne, lt, le, gt, and ge, functions. Is there a way to check a String in the condition?

Thank you very much for any hints...

Ivo

Jakob Borg

unread,
Sep 11, 2017, 7:33:19 AM9/11/17
to ivo.he...@gmail.com, golang-nuts
The clean way to do this, in my opinion, is to make your item/element a type that knows whether it's failed or not.

https://play.golang.org/p/K_t8iEZvUc

You can also inject strings.Contains or similar using https://golang.org/pkg/html/template/#Template.Funcs

//jb
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Shawn Milochik

unread,
Sep 11, 2017, 8:56:38 AM9/11/17
to golang-nuts
On Mon, Sep 11, 2017 at 7:32 AM, Jakob Borg <ja...@kastelo.net> wrote:
The clean way to do this, in my opinion, is to make your item/element a type that knows whether it's failed or not.

https://play.golang.org/p/K_t8iEZvUc

You can also inject strings.Contains or similar using https://golang.org/pkg/html/template/#Template.Funcs


Or return the strings in two separate slices -- one for the "good" and the other for the failed. It would not only solve this problem, but allow you to separate the failed ones visually. 

ivo.he...@gmail.com

unread,
Sep 12, 2017, 10:08:29 AM9/12/17
to golang-nuts
Thank you very much.

Indeed, it would be clean to use Objects instead of simple Strings to flag if a dump can be restored or not.

I've added the function strings.Contains before parsing the template-files:

    templates, err = template.New("").Funcs(template.FuncMap{
      "Contains": strings.Contains}).ParseFiles(allFiles...)

And in the template I can parse the String now:

        {{ if (Contains $element ".FAILED.") }}
          &nbsp;
        {{ else }}
          <a href="javascript:action('restore','{{$element}}', false);">Restore</a>
        {{ end }}


one day, I will change the Objects passed to the template :)

Thank you very much

Ivo

Simon Ritchie

unread,
Sep 13, 2017, 3:08:51 AM9/13/17
to golang-nuts
I suggest that you use the Model View Controller pattern, which is to say, you do all the data processing in Go and use your HTML template only to display the result. For example in your Go driver program, simply create a set of strings. to be displayed. For a successful backup, the string should be the HTML for the line containg the backup link. For a failed backup, it's the HTML for your failure message.

I wrote a tool that generates a simple web server from a JSON specification (https://github.com/goblimey/scaffolder). It makes heavy use of templates and they could have become horribly complicated. I avoided that using this technique.

Fundamentally, it's much easier to write logic in Go than in templatese, and, of course, you can write unit tests tocheck that it works.

Reply all
Reply to author
Forward
0 new messages