github.com/whatlly/hugo@v0.47.1/tpl/collections/init.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package collections
    15  
    16  import (
    17  	"github.com/gohugoio/hugo/deps"
    18  	"github.com/gohugoio/hugo/tpl/internal"
    19  )
    20  
    21  const name = "collections"
    22  
    23  func init() {
    24  	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
    25  		ctx := New(d)
    26  
    27  		ns := &internal.TemplateFuncsNamespace{
    28  			Name:    name,
    29  			Context: func(args ...interface{}) interface{} { return ctx },
    30  		}
    31  
    32  		ns.AddMethodMapping(ctx.After,
    33  			[]string{"after"},
    34  			[][2]string{},
    35  		)
    36  
    37  		ns.AddMethodMapping(ctx.Apply,
    38  			[]string{"apply"},
    39  			[][2]string{},
    40  		)
    41  
    42  		ns.AddMethodMapping(ctx.Delimit,
    43  			[]string{"delimit"},
    44  			[][2]string{
    45  				{`{{ delimit (slice "A" "B" "C") ", " " and " }}`, `A, B and C`},
    46  			},
    47  		)
    48  
    49  		ns.AddMethodMapping(ctx.Dictionary,
    50  			[]string{"dict"},
    51  			[][2]string{},
    52  		)
    53  
    54  		ns.AddMethodMapping(ctx.EchoParam,
    55  			[]string{"echoParam"},
    56  			[][2]string{
    57  				{`{{ echoParam .Params "langCode" }}`, `en`},
    58  			},
    59  		)
    60  
    61  		ns.AddMethodMapping(ctx.First,
    62  			[]string{"first"},
    63  			[][2]string{},
    64  		)
    65  
    66  		ns.AddMethodMapping(ctx.KeyVals,
    67  			[]string{"keyVals"},
    68  			[][2]string{
    69  				{`{{ keyVals "key" "a" "b" }}`, `key: [a b]`},
    70  			},
    71  		)
    72  
    73  		ns.AddMethodMapping(ctx.In,
    74  			[]string{"in"},
    75  			[][2]string{
    76  				{`{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}`, `Substring found!`},
    77  			},
    78  		)
    79  
    80  		ns.AddMethodMapping(ctx.Index,
    81  			[]string{"index"},
    82  			[][2]string{},
    83  		)
    84  
    85  		ns.AddMethodMapping(ctx.Intersect,
    86  			[]string{"intersect"},
    87  			[][2]string{},
    88  		)
    89  
    90  		ns.AddMethodMapping(ctx.IsSet,
    91  			[]string{"isSet", "isset"},
    92  			[][2]string{},
    93  		)
    94  
    95  		ns.AddMethodMapping(ctx.Last,
    96  			[]string{"last"},
    97  			[][2]string{},
    98  		)
    99  
   100  		ns.AddMethodMapping(ctx.Querify,
   101  			[]string{"querify"},
   102  			[][2]string{
   103  				{
   104  					`{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}`,
   105  					`bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose`},
   106  				{
   107  					`<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>`,
   108  					`<a href="https://www.google.com?page=3&amp;q=test">Search</a>`},
   109  			},
   110  		)
   111  
   112  		ns.AddMethodMapping(ctx.Shuffle,
   113  			[]string{"shuffle"},
   114  			[][2]string{},
   115  		)
   116  
   117  		ns.AddMethodMapping(ctx.Slice,
   118  			[]string{"slice"},
   119  			[][2]string{
   120  				{`{{ slice "B" "C" "A" | sort }}`, `[A B C]`},
   121  			},
   122  		)
   123  
   124  		ns.AddMethodMapping(ctx.Sort,
   125  			[]string{"sort"},
   126  			[][2]string{},
   127  		)
   128  
   129  		ns.AddMethodMapping(ctx.Union,
   130  			[]string{"union"},
   131  			[][2]string{
   132  				{`{{ union (slice 1 2 3) (slice 3 4 5) }}`, `[1 2 3 4 5]`},
   133  			},
   134  		)
   135  
   136  		ns.AddMethodMapping(ctx.Where,
   137  			[]string{"where"},
   138  			[][2]string{},
   139  		)
   140  
   141  		ns.AddMethodMapping(ctx.Seq,
   142  			[]string{"seq"},
   143  			[][2]string{
   144  				{`{{ seq 3 }}`, `[1 2 3]`},
   145  			},
   146  		)
   147  
   148  		ns.AddMethodMapping(ctx.NewScratch,
   149  			[]string{"newScratch"},
   150  			[][2]string{
   151  				{`{{ $scratch := newScratch }}{{ $scratch.Add "b" 2 }}{{ $scratch.Add "b" 2 }}{{ $scratch.Get "b" }}`, `4`},
   152  			},
   153  		)
   154  
   155  		ns.AddMethodMapping(ctx.Uniq,
   156  			[]string{"uniq"},
   157  			[][2]string{
   158  				{`{{ slice 1 2 3 2 | uniq }}`, `[1 2 3]`},
   159  			},
   160  		)
   161  
   162  		return ns
   163  
   164  	}
   165  
   166  	internal.AddTemplateFuncsNamespace(f)
   167  }