Skip to content

Commit

Permalink
#217 added ability to include template from anywhere under view tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Oct 25, 2018
1 parent 5cb3b31 commit 0de925c
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 20 deletions.
21 changes: 13 additions & 8 deletions view/funcs.go
Expand Up @@ -20,33 +20,38 @@ func (e *GoViewEngine) tmplSafeHTML(str string) template.HTML {
// tmplInclude method renders given template with View Args and imports into
// current template.
func (e *GoViewEngine) tmplInclude(name string, viewArgs map[string]interface{}) template.HTML {
if !strings.HasPrefix(name, "common") {
if len(name) == 0 {
log.Error("goviewengine: empty template filename suppiled to func 'include'")
return e.tmplSafeHTML("")
}
if name[0] == '/' {
if strings.HasPrefix(name, "/views") {
name = strings.TrimPrefix(name, "/views")
log.Warnf("goviewengine: use without prefix of '/views' with func 'include' [/views%s => %s]", name, name)
}
} else if !strings.HasPrefix(name, "common") {
// TODO existing behaviour will be removed in the future release
name = "common/" + name
}

name = filepath.ToSlash(name)
var err error
var tmpl *template.Template
if e.hotReload {
if e.hotReload || name[0] == '/' {
if tmpl, err = e.ParseFile(name); err != nil {
log.Errorf("goviewengine: %s", err)
return e.tmplSafeHTML("")
}
} else {
tmpl = commonTemplates.Lookup(name)
tmpl = commonTemplates.Lookup(filepath.ToSlash(name))
}

if tmpl == nil {
log.Warnf("goviewengine: common template not found: %s", name)
return e.tmplSafeHTML("")
}

buf := acquireBuffer()
defer releaseBuffer(buf)
if err = tmpl.Execute(buf, viewArgs); err != nil {
log.Errorf("goviewengine: %s", err)
return e.tmplSafeHTML("")
}

return e.tmplSafeHTML(buf.String())
}
6 changes: 3 additions & 3 deletions view/testdata/views-no-common-dir/layouts/master.html
Expand Up @@ -5,11 +5,11 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ template "title" . }}</title>
{{ import "head_tags.html" . -}}
{{ include "head_tags.html" . -}}
</head>
<body>
{{ template "body" . }}
{{ import "footer_scripts.html" . -}}
{{ import "not_found.html" . }}
{{ include "footer_scripts.html" . -}}
{{ include "not_found.html" . }}
</body>
</html>
6 changes: 3 additions & 3 deletions view/testdata/views-no-errros-dir/layouts/master.html
Expand Up @@ -5,11 +5,11 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ template "title" . }}</title>
{{ import "head_tags.html" . -}}
{{ include "head_tags.html" . -}}
</head>
<body>
{{ template "body" . }}
{{ import "footer_scripts.html" . -}}
{{ import "not_found.html" . }}
{{ include "footer_scripts.html" . -}}
{{ include "not_found.html" . }}
</body>
</html>
6 changes: 3 additions & 3 deletions view/testdata/views-no-pages-dir/layouts/master.html
Expand Up @@ -5,11 +5,11 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ template "title" . }}</title>
{{ import "head_tags.html" . -}}
{{ include "head_tags.html" . -}}
</head>
<body>
{{ template "body" . }}
{{ import "footer_scripts.html" . -}}
{{ import "not_found.html" . }}
{{ include "footer_scripts.html" . -}}
{{ include "not_found.html" . }}
</body>
</html>
1 change: 1 addition & 0 deletions view/testdata/views/common/level1/footer_scripts.html
@@ -0,0 +1 @@
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
2 changes: 2 additions & 0 deletions view/testdata/views/common/level1/head_tags.html
@@ -0,0 +1,2 @@
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>
9 changes: 6 additions & 3 deletions view/testdata/views/layouts/master.html
Expand Up @@ -5,11 +5,14 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ template "title" . }}</title>
{{ import "head_tags.html" . -}}
{{ include "head_tags.html" . -}}
{{ include "level1/head_tags.html" . -}}
</head>
<body>
{{ template "body" . }}
{{ import "footer_scripts.html" . -}}
{{ import "not_found.html" . }}
{{ include "footer_scripts.html" . -}}
{{ include "common/level1/footer_scripts.html" . -}}
{{ include "not_found.html" . }}
{{ include "" . }}
</body>
</html>
1 change: 1 addition & 0 deletions view/testdata/views/pages/app/index.html
Expand Up @@ -2,4 +2,5 @@

{{ define "body" -}}
<h1>Welcome to {{ .GreetName }} {{ .PageName }}.</h1>
{{ include "/users/testhtml-form.html" . }}
{{- end }}
1 change: 1 addition & 0 deletions view/testdata/views/pages/user/index.html
Expand Up @@ -5,4 +5,5 @@ <h1>{{ .GreetName }} {{ .PageName }}.</h1>
<form class="" action="index.html" method="post">

</form>
{{ include "/views/users/testhtml-form.html" . }}
{{- end }}
28 changes: 28 additions & 0 deletions view/testdata/views/users/testhtml-form.html
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>pageTitle</title>
<script type='text/javascript'>
if (foo) {
bar(1 + 5)
}
</script>
</head>
<body>
<h1>node template engine</h1>
<form action="{{ rurl . "form_auth_login_submit__aah" }}" class="" method="post">
<div id="container" class="col">
{{ if .Welcome }}
<p>You are amazing</p>
{{ else }}
<p>Get on it!</p>
{{ end }}
<p>
one is <a terse="terse">terse</a> and simple
templating language with a
<strong>focus</strong> on performance
and powerful features.
</div>
</form>
</body>
</html>

0 comments on commit 0de925c

Please sign in to comment.