sipin / gorazor Goto Github PK
View Code? Open in Web Editor NEWRazor view engine for go
License: Other
Razor view engine for go
License: Other
衡量一个模板是否好用,复用是一个非常重要的指标,非 Hello World 所能反映出来的,必须在深入使用,才能体会。
我想请教几个问题,想问问 gorazor 是什么解决的。
我都是基于 golang template 来说的.
<select>
<option value="-1">all</option>
<option value="4" {{if eq .item.Value 4}} selected="selected"{{end}}>a</option>
<option value="9" {{if eq .item.Value 9}} selected="selected"{{end}}>b</option>
<option value="11" {{if eq .item.Value 11}} selected="selected"{{end}}>c</option>
<option value="3" {{if eq .item.Value 3}} selected="selected"{{end}}>d</option>
</select>
这样的代码既丑陋又低效,gorazor 能解决吗?
Hi GoRazor Team,
Can you add some license information into the project?
The GoLang project uses a 3-clause BSD license. If you don't have any preferences would you consider a BSD license or an Apache License?
Thanks!
-Erik
一般网站在发生内部错误的时候会统一跳转到一个错误页面。所以会有一个统一的入口和实现。用 go native template 实现的伪代码:
basepage.go
package base
func Error(errcode int, errmsg string) {
data.User = currentUser
data.HasNewMessage = false
data.SomeOtherBussinesData = xxx
gotemplate.Render("error.tpl", data)
}
这个思路在很多模板系统是流行的解决方法,能统一处理所有页面处理时出错 渲染 Error Page
用gorazor按这个思路做不了,比如伪代码类似
gorazor_basepage.go
package base
import "gorazor_gentpl"
func Error(errcode int, errmsg string) {
data.User = currentUser
data.HasNewMessage = false
data.SomeOtherBussinesData = xxx
gorazor_gentpl.Error(data)
}
gorazor_gentpl 这里面的代码有 import 了 base 里的不少东西。
问题的本质: go native tempate 非代码运行,虽然tpl引用了base,但 base 内再 call tpl 没问题。但gorazor是代码运行就有package 相互 import 的问题。
我能想到的解决方法:
不知道 gorazor 如何解决这个问题的
This results in an error
@*
should be ignored
*@
The gorazor will change the file name to a first-letter upper function name, currently.
But I want that the gorazor put the file name direct to a function name,so I can generate a private function.I know this will break the exist client,so I suggest that we can add a option to do this.
I will try to add a pull request to do this.
@section
seem to only work with an enclosing HTML tag. This breaks
@section side {
<!-- comment -->
plain text
}
Here is the compilation error.
[tjyang@hp1 test]$ gorazor tpl tpl.out
Processing dir: tpl tpl.out
Processing: tpl/helper/footer.gohtml --> /home/tjyang/gocode/src/test/tpl.out/helper/footer.go
Processing: tpl/helper/header.gohtml --> /home/tjyang/gocode/src/test/tpl.out/helper/header.go
Processing: tpl/helper/msg.gohtml --> /home/tjyang/gocode/src/test/tpl.out/helper/msg.go
Processing: tpl/home.gohtml --> /home/tjyang/gocode/src/test/tpl.out/home.go
Processing: tpl/layout/base.gohtml --> /home/tjyang/gocode/src/test/tpl.out/layout/base.go
[tjyang@hp1 test]$ cd tpl.out/
[tjyang@hp1 tpl.out]$ go build -x home.go
WORK=/tmp/go-build837451497
home.go:6:2: cannot find package "kp/models" in any of:
/usr/lib64/golang/src/pkg/kp/models (from $GOROOT)
/home/tjyang/gocode/src/kp/models (from $GOPATH)
home.go:7:2: cannot find package "tpl/helper" in any of:
/usr/lib64/golang/src/pkg/tpl/helper (from $GOROOT)
/home/tjyang/gocode/src/tpl/helper (from $GOPATH)
home.go:8:2: cannot find package "tpl/layout" in any of:
/usr/lib64/golang/src/pkg/tpl/layout (from $GOROOT)
/home/tjyang/gocode/src/tpl/layout (from $GOPATH)
[tjyang@hp1 tpl.out]$
gorazor seems to have issues dealing with CSS @media
and @font-face
directives
Hello!
I'm sorry for the silly question, but is it possible to declare model variables from main package? If yes, how could I call it?
The problem is, I want to send an interface with a map like Content{var, var, var, map}
but in the end get "undefined map" error
Also, might be a good idea to add examples of models to examples, I'm missing the model code from your example packages :(
Thanks.
<script type="text/javascript">console.log("\n");</script>
is producing
_buffer.WriteString("<script type=\"text/javascript\">console.log(\"\n\");</script>")
it should produce:
_buffer.WriteString("<script type=\"text/javascript\">console.log(\"\\n\");</script>")
template
value=\"<?= h(aabasdf\Admin\Document::$asdf) ?>\"/>\n
compiled go file:
package tpl
import (
"bytes"
)
func FailTest() string {
var _buffer bytes.Buffer
_buffer.WriteString("value=\\"<?= h(aabasdf\Admin\Document::$asdf) ?>\\"/>\")
return _buffer.String()
}
compiled failed info:
/xxx/tpl/failTest.go:8:32: illegal character U+003F '?'
/xxx/tpl/failTest.go:8:44: illegal character U+005C '\'
/xxx/tpl/failTest.go:8:50: illegal character U+005C '\'
/xxx/tpl/failTest.go:8:61: illegal character U+0024 '$'
/xxx/tpl/failTest.go:8:68: illegal character U+003F '?'
/xxx/tpl/failTest.go:8:70: illegal character U+005C '\'
/xxx/tpl/failTest.go:8:71: illegal character U+005C '\'
/xxx/tpl/failTest.go:8:72: string literal not terminated
/xxx/tpl/failTest.go:11:1: expected ')', found 'return'
/xxx/tpl/failTest.go:12:3: expected '}', found 'EOF'
@{
isActive := func(name string) {
if active == name {
<li class="active">
}else{
<li>
}
}
}
生成.go后
{
isActive := func(name string) {
if active == name {
_buffer.WriteString("<li class=\"active\">\n ")
} else {
_buffer.WriteString("<li>\n ")
}
}
}
导致出现isActive
未定义错误
in lexer.go
line: text = strings.Replace(lexer.Text, "\r", "\n", -1)
should be: text = strings.Replace(text, "\r", "\n", -1)
or maybe just remove that line and replace line above with: text := strings.Replace(lexer.Text, "\r", "", -1)
we use idea,thanks.
I'm able to import project/internal/app/templates/helper
and use imported classes without issue
index.gohtml
@{
import (
"project/internal/app/templates/helper"
)
}
@helper.Test("helper")
<div>content</div>
I've created a layout in templates/layout/Base.gohtml
:
<div>!!!</div>
When I change index.gohtml
's first lines to the following:
@{
import (
"project/internal/app/templates/helper"
"project/internal/app/templates/layout"
)
layout := layout.Base
}
It throws
Can't find layout: project/internal/app/templates/layout/base
I tried copying the example templates from the example
directory to my project's tpl
(fixing the packages), and get the same error.
你好 我想知道目前这个库是否也能和blazor一样 不用写js吗 纯写go和html实现操作浏览器dom吗?因为目前只有js可以操作dom,请问gorazor在浏览器端的用于操作dom
更新的js咋没看到呢 谢谢
But it generates:
<ul><li> Felix Sun 0 </li><li> Felix Sun 1 </li><li> Felix Sun 2 </li><li> Felix Sun 3 </li><li> Felix Sun 4 </li><li> Felix Sun 5 </li><li> Felix Sun 6 </li><li> Felix Sun 7 </li><li> Felix Sun 8 </li><li> Felix Sun 9 </li><li> Felix Sun 10 </li><li> Felix Sun 11 </li><li> Felix Sun 12 </li><li> Felix Sun 13 </li><li> Felix Sun 14 </li><li> Felix Sun 15 </li><li> Felix Sun 16 </li><li> Felix Sun 17 </li><li> Felix Sun 18 </li><li> Felix Sun 19 </li><li> Felix Sun 20 </li><li> Felix Sun 21 </li><li> Felix Sun 22 </li><li> Felix Sun 23 </li><li> Felix Sun 24 </li><li> Felix Sun 25 </li><li> Felix Sun 26 </li><li> Felix Sun 27 </li><li> Felix Sun 28 </li><li> Felix Sun 29 </li>
</ul>
case reference: https://gist.github.com/Wuvist/7f22c2f3e2348e7b8479
Panic out put
panic: UNMATCHED!
goroutine 16 [running]:
runtime.panic(0x121f80, 0x2082d8460)
/usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
github.com/sipin/gorazor/gorazor.(*Parser).handleMKP(0x208398320, 0x2083702ac, 0x6, 0x197e90, 0xe, 0x11, 0xe, 0x1)
/Users/wuvist/Dropbox/sipin/zfw_code/src/github.com/sipin/gorazor/gorazor/parser.go:354 +0x800
github.com/sipin/gorazor/gorazor.(*Parser).Run(0x208398320, 0x0, 0x0)
/Users/wuvist/Dropbox/sipin/zfw_code/src/github.com/sipin/gorazor/gorazor/parser.go:541 +0x272
github.com/sipin/gorazor/gorazor.run(0x7fff5fbffb08, 0x3a, 0x2082e4c30, 0x0, 0x0, 0x0)
/Users/wuvist/Dropbox/sipin/zfw_code/src/github.com/sipin/gorazor/gorazor/gogen.go:330 +0x620
github.com/sipin/gorazor/gorazor.Generate(0x7fff5fbffb08, 0x3a, 0x2082e4c30, 0x0, 0x0, 0x0, 0x0)
/Users/wuvist/Dropbox/sipin/zfw_code/src/github.com/sipin/gorazor/gorazor/gogen.go:356 +0x64
github.com/sipin/gorazor/gorazor.GenFile(0x7fff5fbffb08, 0x3a, 0x7fff5fbffb43, 0x3a, 0x2082e4c30, 0x0, 0x0)
/Users/wuvist/Dropbox/sipin/zfw_code/src/github.com/sipin/gorazor/gorazor/gogen.go:383 +0x502
main.main()
/Users/wuvist/Dropbox/sipin/zfw_code/src/github.com/sipin/gorazor/gorazor.go:44 +0x637
goroutine 17 [runnable]:
runtime.MHeap_Scavenger()
/usr/local/go/src/pkg/runtime/mheap.c:507
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1430
goroutine 18 [runnable]:
bgsweep()
/usr/local/go/src/pkg/runtime/mgc0.c:1960
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1430
goroutine 19 [runnable]:
runfinq()
/usr/local/go/src/pkg/runtime/mgc0.c:2586
runtime.goexit()
/usr/local/go/src/pkg/runtime/proc.c:1430
@section js {
<script src="/js/jquery.rwdImageMaps.min.js"></script>
@if custom["js"] != "" {
<script src="@custom["js"]"></script>
}
<script type="text/javascript">
$(document).ready(function(e) {
$('img[usemap]').rwdImageMaps();
});
</script>
}
gives
js := func() string {
var _buffer bytes.Buffer
_buffer.WriteString("<script src=\"/js/jquery.rwdImageMaps.min.js\"></script>")
if custom["js"] != "" {
_buffer.WriteString("<script src=\"")
_buffer.WriteString(gorazor.HTMLEscape(custom["js"]))
_buffer.WriteString("\"></script>")
return _buffer.String()
}
_buffer.WriteString("<script type=\"text/javascript\">\n $(document).ready(function(e) {\n $('img[usemap]').rwdImageMaps();\n });\n </script>")
}
should be
js := func() string {
var _buffer bytes.Buffer
_buffer.WriteString("<script src=\"/js/jquery.rwdImageMaps.min.js\"></script>")
if custom["js"] != "" {
_buffer.WriteString("<script src=\"")
_buffer.WriteString(gorazor.HTMLEscape(custom["js"]))
_buffer.WriteString("\"></script>")
}
_buffer.WriteString("<script type=\"text/javascript\">\n $(document).ready(function(e) {\n $('img[usemap]').rwdImageMaps();\n });\n </script>")
return _buffer.String()
}
@{
import (
"zfw/models"
. "zfw/tplhelper"
"dm"
)
var obj *models.Widget
}
@{
data, dmType := dm.GetData(obj.PlaceHolder)
if dmType == "simple" {
obj.StringList = data.([]string)
<div>@raw(SelectPk(obj))</div>
} else {
node := data.(*dm.DMTree)
<div class="form-group @GetErrorClass(obj)">
<label for="@obj.Name" class="col-sm-2 control-label">@obj.Label</label>
<div class="col-sm-10">
<select class="form-control" name="@obj.Name" @BoolStr(obj.Disabled, "disabled")>
@for _, option := range node.Keys{
if option == obj.Value{
<option selected>@option</option>
}else{
<option>@option</option>
}
}
</select>
@if obj.ErrorMsg != "" {
<span class="label label-danger">@obj.ErrorMsg</span>
}
</div>
</div>
}
}
example my gohtml file name is : footer_ID_zh-CN.gohtml , it's cannot work.
衡量一个模板是否好用,非 Hello World 所能反映出来的,必须在深入使用,才能体会。
我在实际使用go template 时遇到几个问题,想问问 gorazor 是什么解决的。
我都是基于 golang template 来说的.
{{range .Students}}
{{template "child" .}}
{{end}}
<img src="{{$.CDN}}{{.Logo}}">
child.tpl:
<p>{{.Name}}</p>
<img src="{{$.CDN}}{{.Pic}}">
xx.go
if request.URL == "" {
m.CDN = "http://cdn.xxx.com"
} else {
m.CDN = ""
}
m.Logo = "/pics/logo.png"
m.Students = []Students{{Name:"smith","Pic":"/pics/smith.jpg"},{Name:"fox","Pic":"/pics/fox.jpg"}}
render("parent.tpl", m)
具体问题: child.tpl 的 {{$.CDN}} 会报错。
本质问题: 子模板被引用后,内部的最高作用域 root 是parent传入的变量,但实际应用还需要 glob 这样的作用域
基于go template我能想到的解决方法都有问题:
Have no idea to find where quickly.
sunfmin@FelixMBPR ~/gopkg/src/github.com/theplant/qortex> gorazor ./templates/ ./templates/; go run main.go
Processing dir: ./templates/ ./templates/
Processing: templates/users/UserHeader.gohtml --> /Users/sunfmin/gopkg/src/github.com/theplant/qortex/templates/users/UserHeader.go
panic: UNMATCHED
goroutine 1 [running]:
runtime.panic(0xf96c0, 0x2103bef70)
/Users/sunfmin/go/src/pkg/runtime/panic.c:266 +0xb6
github.com/sipin/gorazor/gorazor.(*Parser).advanceUntil(0x21047e4b0, 0x210481044, 0x1, 0x1652f0, 0xd, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:273 +0x24f
github.com/sipin/gorazor/gorazor.(*Parser).handleBLK(0x21047e4b0, 0x210481044, 0x1, 0x1652f0, 0xd, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:407 +0x775
github.com/sipin/gorazor/gorazor.(*Parser).Run(0x21047e4b0, 0x0, 0x0)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:543 +0x29b
github.com/sipin/gorazor/gorazor.(*Parser).subParse(0x21047e460, 0x21048100b, 0x1, 0x166a50, 0xa, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:290 +0x2ac
github.com/sipin/gorazor/gorazor.(*Parser).handleBLK(0x21047e460, 0x21048100b, 0x1, 0x166a50, 0xa, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:432 +0x495
github.com/sipin/gorazor/gorazor.(*Parser).Run(0x21047e460, 0x0, 0x0)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:543 +0x29b
github.com/sipin/gorazor/gorazor.(*Parser).subParse(0x21047e410, 0x210481001, 0x1, 0x164890, 0xa, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:290 +0x2ac
github.com/sipin/gorazor/gorazor.(*Parser).handleBLK(0x21047e410, 0x210481001, 0x1, 0x164890, 0xa, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:432 +0x495
github.com/sipin/gorazor/gorazor.(*Parser).Run(0x21047e410, 0x0, 0x0)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/parser.go:543 +0x29b
github.com/sipin/gorazor/gorazor.run(0x2104069f0, 0x21, 0x2104382a0, 0x210403e10, 0x0, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/gogen.go:322 +0x5bc
github.com/sipin/gorazor/gorazor.Generate(0x2104069f0, 0x21, 0x2104382a0, 0x43, 0xf96c0, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/gogen.go:348 +0x40
github.com/sipin/gorazor/gorazor.GenFile(0x2104069f0, 0x21, 0x21047d180, 0x51, 0x2104382a0, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/gogen.go:375 +0x43b
github.com/sipin/gorazor/gorazor.func·001(0x2104069f0, 0x21, 0x371930, 0x210403d20, 0x0, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/gogen.go:423 +0x21a
path/filepath.walk(0x2104069f0, 0x21, 0x371930, 0x210403d20, 0x2210511e00, ...)
/Users/sunfmin/go/src/pkg/path/filepath/path.go:341 +0x72
path/filepath.walk(0x2103be570, 0xf, 0x371930, 0x210439dc0, 0x2210511e00, ...)
/Users/sunfmin/go/src/pkg/path/filepath/path.go:359 +0x338
path/filepath.walk(0x7fff5fbffa58, 0xc, 0x371930, 0x2104393c0, 0x2210511e00, ...)
/Users/sunfmin/go/src/pkg/path/filepath/path.go:359 +0x338
path/filepath.Walk(0x7fff5fbffa58, 0xc, 0x2210511e00, 0x3d, 0x0)
/Users/sunfmin/go/src/pkg/path/filepath/path.go:380 +0xb7
github.com/sipin/gorazor/gorazor.GenFolder(0x7fff5fbffa58, 0xc, 0x7fff5fbffa65, 0xc, 0x2104382a0, ...)
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor/gogen.go:431 +0x248
main.main()
/Users/sunfmin/gopkg/src/github.com/sipin/gorazor/gorazor.go:38 +0x376
handlers/users/user_handlers.go:21:2: cannot find package "github.com/theplant/templates/users" in any of:
/Users/sunfmin/go/src/pkg/github.com/theplant/templates/users (from $GOROOT)
/Users/sunfmin/gopkg/src/github.com/theplant/templates/users (from $GOPATH)
The problem template file.
@{
import(
"Tpl"
)
}
<div class="container">
@(raw(Tpl.TplBread([]string{"选择邮寄方式"})))
</div>
Great project! This makes it easy to move from ASP.NET MVC to GO.
One request I have is to add Go's undocumented "line directives" to the generated code so Go’s error reporting can reference your original source:
//line index.ego:1
func RenderIndex(w io.Writer, widgets []*Widget) error {
//line index.ego:2
_, _ = fmt.Fprintf(w, "\n\n<html>\n<body>\n <h1>Widgets for Sale!</h1>\n\n <ul>\n ")
//line index.ego:8
for _, widget := range widgets {
//line index.ego:9
_, _ = fmt.Fprintf(w, "\n <li>")
//line index.ego:9
_, _ = fmt.Fprintf(w, "%v", widget.Name)
//line index.ego:9
_, _ = fmt.Fprintf(w, " for $")
//line index.ego:9
_, _ = fmt.Fprintf(w, "%v", widget.Price)
//line index.ego:9
_, _ = fmt.Fprintf(w, "</li>\n ")
//line index.ego:10
}
//line index.ego:11
_, _ = fmt.Fprintf(w, "\n </ul>\n</body>\n</html>\n")
return nil
}
The above code is from an article on the EGO templating engine.
Email: <div>[email protected]</div>
It will treat @gmail.com
as variable, anyway to escape that?
Is there a reason the return value on template functions is string
instead of just bytes.Buffer
? I understand it's more eye readable but it requires two transformations to write to ResponseWriter
.
return _buffer.String()
func index(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(views.Index())
}
Wouldn't this be more efficient
views.Index().writeTo(w)
无法高亮
修改GoSublime 的gslint和gscomplete,也无法“补全”
请求更新,感谢!
code:
@{
var l *Locale
}
<span>@l.T(`for`)</span>
Will generate:
package test
import (
"github.com/sipin/gorazor/gorazor"
"bytes"
)
func S(l *Locale) string {
var _buffer bytes.Buffer
_buffer.WriteString("\n<span>")
_buffer.WriteString(gorazor.HTMLEscape(l.T(`for`
)))
_buffer.WriteString("</span>")
return _buffer.String()
}
which can't compile because:
FelixMBPR:Desktop sunfmin$ gorazor test/ test/
Processing dir: test/ test/
Processing: test/s.gohtml --> /Users/sunfmin/Desktop/test/s.go
/Users/sunfmin/Desktop/test/s.go:10:49: missing ',' before newline in argument list
gofmt: exit status 2
Say, I have this code:
@{
for _, v := range p.Posts {
<h1>@raw(v.Title)</h1>
<p>@raw(v.Content)</h1>
}
}
v.Title and v.Content vars do not produce raw results (gorazor and HTMLEscape is still used after recompile).
@{
import (
"github.com/sunfmin/gorazortests/models"
)
}
<body>
@Hello("Felix Sun", "h1", 30, &models.Author{"Van", 20},10)
</body>
generated:
package templates
import (
"github.com/sunfmin/gorazortests/models"
"github.com/sipin/gorazor/gorazor"
"bytes"
)
func Invokehello() string {
var _buffer bytes.Buffer
_buffer.WriteString("\n\n<body>\n")
_buffer.WriteString(gorazor.HTMLEscape(Hello("Felix Sun", "h1", 30, &models.Author{"Van", 20}
,10)))
_buffer.WriteString("\n</body>\n\n")
return _buffer.String()
}
compiles error:
Processing: templates/invokehello.gohtml --> /Users/sunfmin/gopkg/src/github.com/sunfmin/gorazortests/templates/invokehello.go
/Users/sunfmin/gopkg/src/github.com/sunfmin/gorazortests/templates/invokehello.go:11:94: missing ',' before newline in argument list
只修改gohtml文件的大小写,然后在mac里面生成模板,传到git里面.再到linux里面拉取,然后会生成2个大小写不一样的go文件导致无法编译.
Change the file case of the gohtml file,build the go file in mac,push it to a git ,pull that git in linux,and build the go file again,you will get two golang files with file case different.And the package will not compile in linux.
现在主流的ide是goland;
还有就是这个gorazor在输出go代码时失败没有错误提示 就只是提示失败了 又没法在ide里面调试 写起来很难受 有何解决办法呢请问 谢谢
@variable
will automatically escape the variable, but I found that the escaping doesn't work in layout.
If I have a layout:
@{
var body string
var js string
}
hello @js
And in another template do not use
@section js {
world
}
I get a not enough arguments in call to layout.App`` because the compiled template is calling
return layout.App(_buffer.String())```
The docs state that the layout sections are optional, so is this a bug or am I reading the docs incorrectly? Thanks for an awesome library!
您好 我简单改写的index.gohtml如下
@{
import (
"time"
)
}
@{
t := time.Now()
StrTime := t.Format("2006-01-02 15:04:05")
<p>Time now is: @StrTime </p>
currentCount := 0;
func IncrementCount(){
currentCount++;
}
}
<p>This is Index</p>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
不能编译出go文件 请帮忙看下 谢谢
Today gorazor command sources files from 'tpl' and create a new folder 'gen' with files packaged as 'tpl'.
Is this correct? Shouldn't the generated code be packaged as the same output folder?
Thanks
go写api非常棒,但是做web开发,由于官方模板的问题,大大限制了go的发挥。
很多人前端用Vue(或react)+后端go服务器端。这个办法实现前后端分离,但是对seo不友好,学习成本也挺高。接着又有了基于vue上的nuxt。在服务器端渲染,虽然解决seo的问题,但是还是不够简单高效。
既然是服务器端渲染,何必还要靠js呢?不如直接go实现?gorazor本质就是干这个事的啊!
相比nuxt,go做服务器端渲染至少有这些优势:1.更简单,无需学习vue一套东西,学习成本低。2.更高效,go的运行效率肯定比js高。3.更清爽,用go写代码,无疑爽多了。4.更好调试。
所以是否可以考虑参考nuxt.js,做go版本的服务器端渲染框架?彻底解决go模板渲染的问题,为go补齐一个短板。
I have following php template, I am trying to translate to gorazor template.
<img src="/public/image/login-image-<?php echo rand(1,5);?>.jpg" alt="Login Full Background" class="full-bg animation-pulseSlow">
I have tried this:
@{
import (
"github.com/bronze1man/kmg/kmgRand"
)
}
<img src="/public/image/[email protected](0,5).jpg" alt="Login Full Background" class="full-bg animation-pulseSlow">
It will product:
package Tpl
import (
"bytes"
"github.com/bronze1man/kmg/kmgRand"
"github.com/sipin/gorazor/gorazor"
)
func AdminLoginPage() string {
var _buffer bytes.Buffer
_buffer.WriteString("\n<img src=\"/public/image/login-image-")
_buffer.WriteString(gorazor.HTMLEscape(kmgRand.IntBetween(0, 5).jpg))
_buffer.WriteString("\" alt=\"Login Full Background\" class=\"full-bg animation-pulseSlow\">")
return _buffer.String()
}
@if x==0 {class="aaa"}
期望的输出是:
if x == 0 {
_buffer.WriteString("class="aaa"")
}
目前的输出是:
if x == 0 {
class = "aaa"
}
少了 _buffer.WriteString()
这个说法不一定叫继承, 具体的意思请看 gold 模板的实现
Fixing this right now: If you use gopkg, simply fix your imports to point to the fsnotify/fsnotify.v1 repository.
gopkg.in/fsnotify.v1 → gopkg.in/fsnotify/fsnotify.v1
I think use gopkg.in/fsnotify/fsnotify.v1
will fix this problem.
code
@{
import (
"github.com/sunfmin/gorazortests/models"
)
}
@{
au := &models.Author{"Van", 20}
}
<body>
@raw(Hello("Felix Sun", "h1", 30, au ,10))
</body>
generate:
package templates
import (
"bytes"
"github.com/sunfmin/gorazortests/models"
)
func Invokehello() string {
var _buffer bytes.Buffer
_buffer.WriteString("\n\n")
{
au := &models.Author{"Van", 20}
}
_buffer.WriteString("\n\n\n<body>\n")
_buffer.WriteString((Hello("Felix Sun", "h1", 30, au, 10)))
_buffer.WriteString("\n</body>\n\n")
return _buffer.String()
}
compiles error:
sunfmin@FelixMBPR ~/gopkg/src/github.com/sunfmin/gorazortests> gorazor ./templates/ ./templates/ ; go run main.go
Processing dir: ./templates/ ./templates/
Processing: templates/hello.gohtml --> /Users/sunfmin/gopkg/src/github.com/sunfmin/gorazortests/templates/hello.go
Processing: templates/invokehello.gohtml --> /Users/sunfmin/gopkg/src/github.com/sunfmin/gorazortests/templates/invokehello.go
# github.com/sunfmin/gorazortests/templates
templates/invokehello.go:15: undefined: au
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.