mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	templates: Add arguments to .Include
This commit is contained in:
		
							parent
							
								
									5a6b765673
								
							
						
					
					
						commit
						22a266a259
					
				@ -104,7 +104,7 @@ func nextFunc(shouldGzip bool) httpserver.Handler {
 | 
				
			|||||||
				return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w)
 | 
									return 0, fmt.Errorf("ResponseWriter should be gzipResponseWriter, found %T", w)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if strings.Contains(w.Header().Get("Content-Type"), "application/x-gzip") {
 | 
								if strings.Contains(w.Header().Get("Content-Type"), "application/x-gzip") {
 | 
				
			||||||
				return 0, fmt.Errorf("Content type should not be gzip.")
 | 
									return 0, fmt.Errorf("Content-Type should not be gzip")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			return 0, nil
 | 
								return 0, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -12,8 +12,9 @@ import (
 | 
				
			|||||||
	"text/template"
 | 
						"text/template"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/russross/blackfriday"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/russross/blackfriday"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// This file contains the context and functions available for
 | 
					// This file contains the context and functions available for
 | 
				
			||||||
@ -24,10 +25,12 @@ type Context struct {
 | 
				
			|||||||
	Root http.FileSystem
 | 
						Root http.FileSystem
 | 
				
			||||||
	Req  *http.Request
 | 
						Req  *http.Request
 | 
				
			||||||
	URL  *url.URL
 | 
						URL  *url.URL
 | 
				
			||||||
 | 
						Args []interface{} // defined by arguments to .Include
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Include returns the contents of filename relative to the site root.
 | 
					// Include returns the contents of filename relative to the site root.
 | 
				
			||||||
func (c Context) Include(filename string) (string, error) {
 | 
					func (c Context) Include(filename string, args ...interface{}) (string, error) {
 | 
				
			||||||
 | 
						c.Args = args
 | 
				
			||||||
	return ContextInclude(filename, c, c.Root)
 | 
						return ContextInclude(filename, c, c.Root)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -30,6 +30,7 @@ func TestInclude(t *testing.T) {
 | 
				
			|||||||
	}()
 | 
						}()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
 | 
							args                 []interface{}
 | 
				
			||||||
		fileContent          string
 | 
							fileContent          string
 | 
				
			||||||
		expectedContent      string
 | 
							expectedContent      string
 | 
				
			||||||
		shouldErr            bool
 | 
							shouldErr            bool
 | 
				
			||||||
@ -42,7 +43,15 @@ func TestInclude(t *testing.T) {
 | 
				
			|||||||
			shouldErr:            false,
 | 
								shouldErr:            false,
 | 
				
			||||||
			expectedErrorContent: "",
 | 
								expectedErrorContent: "",
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
		// Test 1 - failure on template.Parse
 | 
							// Test 1 - all good, with args
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								args:                 []interface{}{"hello", 5},
 | 
				
			||||||
 | 
								fileContent:          `str1 {{ .Root }} str2 {{index .Args 0}} {{index .Args 1}}`,
 | 
				
			||||||
 | 
								expectedContent:      fmt.Sprintf("str1 %s str2 %s %d", context.Root, "hello", 5),
 | 
				
			||||||
 | 
								shouldErr:            false,
 | 
				
			||||||
 | 
								expectedErrorContent: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Test 2 - failure on template.Parse
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			fileContent:          `str1 {{ .Root } str2`,
 | 
								fileContent:          `str1 {{ .Root } str2`,
 | 
				
			||||||
			expectedContent:      "",
 | 
								expectedContent:      "",
 | 
				
			||||||
@ -73,7 +82,7 @@ func TestInclude(t *testing.T) {
 | 
				
			|||||||
			t.Fatal(testPrefix+"Failed to create test file. Error was: %v", err)
 | 
								t.Fatal(testPrefix+"Failed to create test file. Error was: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		content, err := context.Include(inputFilename)
 | 
							content, err := context.Include(inputFilename, test.args...)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			if !test.shouldErr {
 | 
								if !test.shouldErr {
 | 
				
			||||||
				t.Errorf(testPrefix+"Expected no error, found [%s]", test.expectedErrorContent, err.Error())
 | 
									t.Errorf(testPrefix+"Expected no error, found [%s]", test.expectedErrorContent, err.Error())
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user