github.com/gogf/gf/v2@v2.7.4/os/gview/gview_z_unit_feature_encode_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gview_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/v2/frame/g"
    14  	"github.com/gogf/gf/v2/os/gfile"
    15  	"github.com/gogf/gf/v2/os/gview"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  )
    18  
    19  func Test_Encode_Parse(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		v := gview.New()
    22  		v.SetPath(gtest.DataPath("tpl"))
    23  		v.SetAutoEncode(true)
    24  		result, err := v.Parse(context.TODO(), "encode.tpl", g.Map{
    25  			"title": "<b>my title</b>",
    26  		})
    27  		t.AssertNil(err)
    28  		t.Assert(result, "<div>&lt;b&gt;my title&lt;/b&gt;</div>")
    29  	})
    30  }
    31  
    32  func Test_Encode_ParseContent(t *testing.T) {
    33  	gtest.C(t, func(t *gtest.T) {
    34  		v := gview.New()
    35  		tplContent := gfile.GetContents(gtest.DataPath("tpl", "encode.tpl"))
    36  		v.SetAutoEncode(true)
    37  		result, err := v.ParseContent(context.TODO(), tplContent, g.Map{
    38  			"title": "<b>my title</b>",
    39  		})
    40  		t.AssertNil(err)
    41  		t.Assert(result, "<div>&lt;b&gt;my title&lt;/b&gt;</div>")
    42  	})
    43  }