在Go应用程序中,模板文件读取自身而不是读取JS和CSS文件。

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

Template file in go app reading itself instead of js and css files

问题

我刚开始学习Go语言,并且正在使用Google应用引擎进行Web应用程序的开发。我已经完成了留言板教程,并且现在开始了自己的项目。Go部分编译没有问题,但是在模板尝试渲染时出现了一些奇怪的情况。它能够渲染所有的HTML,但是我添加了CSS和JS,而模板却在读取自身而不是相应的CSS和JS文件。这很奇怪。我不确定是否是路径问题(我本来期望会出现404错误)。

这是我在控制台中得到的错误信息:

Uncaught SyntaxError: Unexpected token <

当我查看JS文件的资源时,它与模板完全相同。我将我的JS和CSS文件放在与模板文件夹相对应的位置,所以我希望它能够找到它们:

app/
- templates/
- css/
- js/

所以在模板中,我这样调用我的脚本:

<script src="../js/main.js"></script>

我应该如何组织我的JS和CSS资源(以及稍后的图片和SASS/LESS)?

编辑

这是我在Chrome开发工具中查看源代码时看到的内容:

在Go应用程序中,模板文件读取自身而不是读取JS和CSS文件。

英文:

I've just starting learning go and I'm noodling around with web apps using the Google app engine. I've worked through the guestbook tutorial, and have now started my own project. The go part compiles just fine, but there's something strange happening when the template tries to render. It's able to render all the HTML, but I've added both CSS and JS, and the template is somehow reading in itself instead of the respective CSS and JS files. It's bizarre. I'm not sure it's a path problem (I would expect a 404).

This is the error I'm getting in the console:

Uncaught SyntaxError: Unexpected token &lt;

And when I view the resource it's picking up for the js file, it's identical to the template. I've placed my js and css relative to the template folder, so I would expect it would find it:

app/
- templates/
- css/
- js/

So my script is called thusly in the template:

&lt;script src=&quot;../js/main.js&quot;&gt;&lt;/script&gt;

How should I be structuring my JS and CSS resources (and later, images, and SASS/LESS)?

Edit

This is what I see in the source for Chrome devtools:

在Go应用程序中,模板文件读取自身而不是读取JS和CSS文件。

答案1

得分: 0

(╯°□°)╯︵ ┻━┻ 啊,问题出在 app.yaml 文件上。我原来写的是:

application: bedlington
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app
  
- url: /css
  static_dir: css
  
- url: /js
  static_dir: js

但是第一个 url 处理程序捕捉到了所有的内容。所以我改成了这样解决问题:

application: bedlington
version: 1
runtime: go
api_version: go1

handlers:
- url: /
  script: _go_app
  
- url: /css
  static_dir: css
  
- url: /js
  static_dir: js

现在我不知道如何捕捉所有的 URL(或者如何正确排除资源文件夹),但这解决了最初的问题。

英文:

(╯°□°)╯︵ ┻━┻ Ah it was the app.yaml file. I had:

application: bedlington
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app
  
- url: /css
  static_dir: css
  
- url: /js
  static_dir: js

But that first url handler catches all the things. So instead this solved the problem:

application: bedlington
version: 1
runtime: go
api_version: go1

handlers:
- url: /
  script: _go_app
  
- url: /css
  static_dir: css
  
- url: /js
  static_dir: js

Now I don't know how to catch all the urls (or how to properly exclude resource folders), but it solves the initial problem.

huangapple
  • 本文由 发表于 2015年10月27日 22:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/33370205.html
匿名

发表评论

匿名网友

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

确定