Specifying template filenames for template.ParseFiles

huangapple go评论89阅读模式
英文:

Specifying template filenames for template.ParseFiles

问题

我的当前目录结构如下:

App
  - Template
    - foo.go
    - foo.tmpl
  - Model
    - bar.go
  - Another
    - Directory
      - baz.go

文件 foo.goinit 期间使用 ParseFiles 读取模板文件。

import "text/template"

var qTemplate *template.Template

func init() {
  qTemplate = template.Must(template.New("temp").ParseFiles("foo.tmpl"))
}

...

foo.go 的单元测试按预期工作。然而,我现在尝试运行 bar.gobaz.go 的单元测试,它们都导入 foo.go,并且在尝试打开 foo.tmpl 时出现了 panic。

/App/Model$ go test    
panic: open foo.tmpl: no such file or directory

/App/Another/Directory$ go test    
panic: open foo.tmpl: no such file or directory

我尝试将模板名称指定为相对目录("./foo.tmpl"),完整目录("~/go/src/github.com/App/Template/foo.tmpl"),App 相对目录("/App/Template/foo.tmpl")等等,但无论如何都无法同时适用于两种情况。单元测试对于 bar.gobaz.go(或两者)都会失败。

我的模板文件应该放在哪里?如何调用 ParseFiles,以便无论从哪个目录调用 go test,它都能找到模板文件?

英文:

My current directory structure looks like the following:

App
  - Template
    - foo.go
    - foo.tmpl
  - Model
    - bar.go
  - Another
    - Directory
      - baz.go

The file foo.go uses ParseFiles to read in the template file during init.

import "text/template"

var qTemplate *template.Template

func init() {
  qTemplate = template.Must(template.New("temp").ParseFiles("foo.tmpl"))
}

...

Unit tests for foo.go work as expected. However, I am now trying to run unit tests for bar.go and baz.go which both import foo.go and I get a panic on trying to open foo.tmpl.

/App/Model$ go test    
panic: open foo.tmpl: no such file or directory

/App/Another/Directory$ go test    
panic: open foo.tmpl: no such file or directory

I've tried specifying the template name as a relative directory ("./foo.tmpl"), a full directory ("~/go/src/github.com/App/Template/foo.tmpl"), an App relative directory ("/App/Template/foo.tmpl"), and others but nothing seems to work for both cases. The unit tests fail for either bar.go or baz.go (or both).

Where should my template file be placed and how should I call ParseFiles so that it can always find the template file regardless of which directory I call go test from?

答案1

得分: 18

有用的提示:

使用os.Getwd()filepath.Join()来找到相对文件路径的绝对路径。

示例:

// 文件:showPath.go
package main

import (
    "fmt"
    "path/filepath"
    "os"
)

func main() {
    cwd, _ := os.Getwd()
    fmt.Println(filepath.Join(cwd, "./template/index.gtpl"))
}

首先,我建议template文件夹只包含用于展示的模板文件,而不是go文件。

接下来,为了简化生活,只从根项目目录运行文件。这将有助于在子目录中嵌套的go文件中保持文件路径的一致性。相对文件路径从当前工作目录开始,即程序被调用的位置。

示例显示当前工作目录的更改:

user@user:~/go/src/test$ go run showPath.go
/home/user/go/src/test/template/index.gtpl
user@user:~/go/src/test$ cd newFolder/
user@user:~/go/src/test/newFolder$ go run ../showPath.go 
/home/user/go/src/test/newFolder/template/index.gtpl

至于测试文件,您可以通过提供文件名来运行单个测试文件。

go test foo/foo_test.go

最后,使用基本路径和path/filepath包来形成文件路径。

示例:

var (
  basePath = "./public"
  templatePath = filepath.Join(basePath, "template")
  indexFile = filepath.Join(templatePath, "index.gtpl")
)
英文:

Helpful tip:

Use os.Getwd() and filepath.Join() to find the absolute path of a relative file path.

Example

// File: showPath.go
package main
import (
        "fmt"
        "path/filepath"
        "os"
)
func main(){
        cwd, _ := os.Getwd()
        fmt.Println( filepath.Join( cwd, "./template/index.gtpl" ) )
}

First off, I recommend that the template folder only contain templates for presentation and not go files.

Next, to make life easier, only run files from the root project directory. This will help make the path to an file consistent throughout go files nested within sub directories. Relative file paths start from where the current working directory, which is where the program was called from.

Example to show the change in current working directory

user@user:~/go/src/test$ go run showPath.go
/home/user/go/src/test/template/index.gtpl
user@user:~/go/src/test$ cd newFolder/
user@user:~/go/src/test/newFolder$ go run ../showPath.go 
/home/user/go/src/test/newFolder/template/index.gtpl

As for test files, you can run individual test files by supplying the file name.

go test foo/foo_test.go

Lastly, use a base path and the path/filepath package to form file paths.

Example:

var (
  basePath = "./public"
  templatePath = filepath.Join(basePath, "template")
  indexFile = filepath.Join(templatePath, "index.gtpl")
) 

huangapple
  • 本文由 发表于 2013年9月27日 01:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/19035187.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定