github.com/goravel/framework@v1.13.9/http/view.go (about)

     1  package http
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/goravel/framework/support/file"
     7  )
     8  
     9  type View struct {
    10  	shared sync.Map
    11  }
    12  
    13  func NewView() *View {
    14  	return &View{}
    15  }
    16  
    17  func (r *View) Exists(view string) bool {
    18  	return file.Exists("resources/views/" + view)
    19  }
    20  
    21  func (r *View) Share(key string, value any) {
    22  	r.shared.Store(key, value)
    23  }
    24  
    25  func (r *View) Shared(key string, def ...any) any {
    26  	value, ok := r.shared.Load(key)
    27  	if !ok {
    28  		if len(def) > 0 {
    29  			return def[0]
    30  		}
    31  
    32  		return nil
    33  	}
    34  
    35  	return value
    36  }
    37  
    38  func (r *View) GetShared() map[string]any {
    39  	shared := make(map[string]any)
    40  	r.shared.Range(func(key, value any) bool {
    41  		shared[key.(string)] = value
    42  		return true
    43  	})
    44  
    45  	return shared
    46  }