转到:指定template.ParseFiles的模板文件名 [英] Go: Specifying template filenames for template.ParseFiles

查看:154
本文介绍了转到:指定template.ParseFiles的模板文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的当前目录结构如下所示:

 应用程序
- 模板
- foo。 go
- foo.tmpl
- Model
- bar.go
- 另一
- 目录
- baz.go

文件 foo.go 使用 ParseFiles code>在 init 中读取模板文件。

 导入文本/模板

var qTemplate * template.Template

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

.. 。

单元测试 foo.go 工作如预期。不过,我现在正在尝试为 bar.go baz.go 运行单元测试,它们都导入 foo.go ,并且我试图打开 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'已尝试将模板名称指定为相对目录(./foo.tmpl),完整目录(〜/ go / src / github.com / App / Template / foo.tmpl),应用程序相对目录/App/Template/foo.tmpl)等等,但是对于这两种情况似乎都没有效果。单元测试对于 bar.go baz.go (或两者)都失败。



我的模板文件放在哪里以及我应该如何调用 ParseFiles ,这样它总能找到模板文件,而不管我调用哪个目录 go test from?

解决方案

有用的提示:使用 os.Getwd() filepath.Join()来查找相对文件路径的绝对路径。



示例

  // File:showPath.go 
package main
import (
fmt
path / filepath
os

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

首先,我建议模板文件夹仅包含用于演示文稿的模板,不包含用于文件的模板。



接下来,为了使生活更轻松,只能运行根项目目录中的文件。这将有助于使文件的路径与嵌套在子目录内的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 

最后,使用基本路径和路径/ filepath

例子:

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


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?

解决方案

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")
) 

这篇关于转到:指定template.ParseFiles的模板文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆