如何截断Golang模板中的字符串

In golang, is there a way to truncate text in an html template?

For example, I have the following in my template:

{{ range .SomeContent }}
 ....
    {{ .Content }}
 ....

{{ end }

{{ .Content }} produces: Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam tempus sem ipsum, vel accumsan felis vulputate id. Donec ultricies sem purus, non aliquam orci dignissim et. Integer vitae mi arcu. Pellentesque a ipsum quis velit venenatis vulputate vulputate ut enim.

I would like to reduce that to 25 characters.

You can define a function. Take a look http://golang.org/pkg/text/template/#example_Template_func.

Edit: See it at the playground: http://play.golang.org/p/OP2x5vDCtn

Update: Now the code below is unicode compliant for those who are working with international programs.

One thing to note is that bytes.Runes("string") below is an O(N) operation, as is the converstion from runes to a string, so this code loops over the string twice. It is likely to be more efficient to do the code below for PreviewContent()

func (c ContentHolder) PreviewContent() string {
    var numRunes = 0
    for index, _ := range c.Content {
         numRunes++
         if numRunes > 25 {
              return c.Content[:index]
         }
    }
    return c.Content
}

You have a couple options for where this function can go. Assuming that you have some type of content holder, the below can be used:

type ContentHolder struct {
    Content string
    //other fields here
}

func (c ContentHolder) PreviewContent() string {
    // This cast is O(N)
    runes := bytes.Runes([]byte(c.Content))
    if len(runes) > 25 {
         return string(runes[:25])
    }
    return string(runes)
}

Then your template will look like this:

{{ range .SomeContent }}
....
{{ .PreviewContent }}
....
{{ end }}

The other option is to create a function that will take then first 25 characters of a string. The code for that looks like this (revision of code by @Martin DrLík, link to code)

package main
import (
    "html/template"
    "log"
    "os"
)

func main() {

    funcMap := template.FuncMap{

        // Now unicode compliant
        "truncate": func(s string) string {
             var numRunes = 0
             for index, _ := range s {
                 numRunes++
                 if numRunes > 25 {
                      return s[:index]
                 }
            }
            return s
       },
    }

    const templateText = `
    Start of text
    {{ range .}}
    Entry: {{.}}
    Truncated entry: {{truncate .}}
    {{end}}
    End of Text
    `
    infoForTemplate := []string{
        "Stackoverflow is incredibly awesome",
        "Lorem ipsum dolor imet",
        "Some more example text to prove a point about truncation",
        "ПриветМирПриветМирПриветМирПриветМирПриветМирПриветМир",
    }

    tmpl, err := template.New("").Funcs(funcMap).Parse(templateText)
    if err != nil {
        log.Fatalf("parsing: %s", err)
    }

    err = tmpl.Execute(os.Stdout, infoForTemplate)
    if err != nil {
        log.Fatalf("execution: %s", err)
    }

}

This outputs:

Start of text

Entry: Stackoverflow is incredibly awesome
Truncated entry: Stackoverflow is incredib

Entry: Lorem ipsum dolor imet
Truncated entry: Lorem ipsum dolor imet

Entry: Some more example text to prove a point about truncation
Truncated entry: Some more example text to

Entry: ПриветМирПриветМирПриветМирПриветМирПриветМирПриветМир
Truncated entry: ПриветМирПриветМирПриветМ

End of Text

Needs more magic for Unicode strings

This is not correct, see below

import "unicode/utf8"

func Short( s string, i int) string {
    if len( s ) < i {
        return s
    }
    if utf8.ValidString( s[:i] ) {
        return s[:i]
    }
    // The omission.
    // In reality, a rune can have 1-4 bytes width (not 1 or 2)
    return s[:i+1] // or i-1
}

But i above is not the number of chars. It's the number of bytes. Link to this code on play.golang.org

I hope this helps.


Edit

Updated: check string length. See @geoff comment below

See that answer, and play here. It's another solution.

package main

import "fmt"

func Short( s string, i int ) string {
    runes := []rune( s )
    if len( runes ) > i {
        return string( runes[:i] )
    }
    return s
}

func main() {
    fmt.Println( Short( "Hello World", 5 ) )
    fmt.Println( Short( "Привет Мир", 5 ) )
}

But if you are interested in the length in bytes:

func truncateStrings(s string, n int) string {
    if len(s) <= n {
        return s
    }
    for !utf8.ValidString(s[:n]) {
        n--
    }
    return s[:n]
}

play.golang.org. This function never panics (if n >= 0), but you can obtain an empty string play.golang.org


Also, keep in mind this experimental package golang.org/x/exp/utf8string

Package utf8string provides an efficient way to index strings by rune rather than by byte.

You can use printf in templates, which acts as fmt.Sprintf. In your case truncating a string would be as easy as:

"{{ printf \"%.25s\" .Content }}"

There are a lot of good answers, but sometimes it's more user-friendly to truncate without cutting words. Hugo offers template function for it. But it's difficult to use outside of Hugo, so I've implemented it:

func TruncateByWords(s string, maxWords int) string {
    processedWords := 0
    wordStarted := false
    for i := 0; i < len(s); {
        r, width := utf8.DecodeRuneInString(s[i:])
        if !isSeparator(r) {
            i += width
            wordStarted = true
            continue
        }

        if !wordStarted {
            i += width
            continue
        }

        wordStarted = false
        processedWords++
        if processedWords == maxWords {
            const ending = "..."
            if (i + len(ending)) >= len(s) {
                // Source string ending is shorter than "..."
                return s
            }

            return s[:i] + ending
        }

        i += width
    }

    // Source string contains less words count than maxWords.
    return s
}

And here is a test for this function:

func TestTruncateByWords(t *testing.T) {
    cases := []struct {
        in, out string
        n       int
    }{
        {"a bcde", "a...", 1},
        {"a b", "a b", 2},
        {"a b", "a b", 3},

        {"a b c", "a b c", 2},
        {"a b cd", "a b cd", 2},
        {"a b cde", "a b...", 2},

        {"  a   b    ", "  a   b...", 2},

        {"AB09C_D EFGH", "AB09C_D...", 1},
        {"Привет Гоферам", "Привет...", 1},
        {"Here are unicode spaces", "Here are...", 2},
    }

    for i, c := range cases {
        got := TruncateByWords(c.in, c.n)
        if got != c.out {
            t.Fatalf("#%d: %q != %q", i, got, c.out)
        }
    }
}