Groups
Groups
Sign in
Groups
Groups
Golang-China
Conversations
About
Send feedback
Help
golang template中的if条件判断
705 views
Skip to first unread message
lulu Lee
unread,
Aug 5, 2012, 10:43:28 AM
8/5/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang-china
各位好:
牛逼得要死的golang的默认的template,我想请问下 {{ if .Age > 18 }} xxxx {{end}} 这样的判断怎么写?
--
@QLeelulu
|
FaWave, Net4 Team |
qlee...@gmail.com
|
学海无涯,回头是岸
lihui
unread,
Aug 5, 2012, 9:04:50 PM
8/5/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
业务代码中都不提倡用魔术数字,你还好意思用在模板中?
我觉得凡是有志于在模板中引入强大语法构造的人,都应该先读一读这篇论文:
http://www.cs.usfca.edu/~parrt/papers/mvc.templates.pdf
2012/8/5 lulu Lee
<
qlee...@gmail.com
>
--
官网:
http://golang-china.org/
IRC:
irc.freenode.net
#golang-china
@golangchina
lulu Lee
unread,
Aug 5, 2012, 10:30:29 PM
8/5/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
难道你们都认为这么基本的判断语句是强大的语法构造?这么基本的判断是很复杂的业务逻辑?
我为了实现这一基本的判断我还得很SB的去写一个funcMap,然后再在模板里面调用?
或者把这“很复杂”的业务逻辑写到go的代码里面,下次我要改一下的时候我不能直接改模板而是要改go的代码还要重新编译?
python的Django自带的模板也是Logic Less。
golang这模板的实现是学院派搞的吧 ?
lihui
unread,
Aug 5, 2012, 10:52:21 PM
8/5/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
funcMap是用来格式化输出的,不应该用在此处。这种判断应该作为模型数据存在,比如 isAdult属性。
2012/8/6 lulu Lee
<
qlee...@gmail.com
>
lulu Lee
unread,
Aug 5, 2012, 11:11:49 PM
8/5/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
好吧,上面的举例确实是可以是个属性。
那么下面的呢?
{{if .comments > 200 }}
置顶200条评论 - <a>显示全部{{.comments}}条评论</a>
{{else if .comments > 0}}
一共{{.comments}}条评论
{{else}}
还没有评论
{{end}}
lihui
unread,
Aug 5, 2012, 11:47:13 PM
8/5/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
假设comments一个评论slice,那么:
{{ if .hasmore}}
置顶{{len .comments}}条评论 - <a>显示全部{{.allLen}}条评论</a>
{{else}}
{{if comments}}
一共{{.comments}}条评论
{{else}}
还没有评论
{{end}}
{{end}}
总可以将条件表达式化为一个模型属性,有时可能稍微麻烦一点,但可读性更好,而且能鼓励消除魔术数字。
促使将200这个东西参数化,让用户可以管理配置这个神奇的200。
2012/8/6 lulu Lee
<
qlee...@gmail.com
>
lulu Lee
unread,
Aug 6, 2012, 12:12:50 AM
8/6/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
所以,为了这只需要在模板中用到的
简单
逻辑,我需要为Model加两个无甚用处的属性,或者把这逻辑添加到Controller中,给template多传几个值,然后template中该有的判断一个都不能少,只是将 {{if .commentLen > 200}} 改成 {{if .hasMore}},我靠!!!!
我也支持Logic Less的模板,
但是,golang的template还是非常操蛋。
另:谢谢前面推荐的链接。
暴雨不在明天
unread,
Aug 7, 2012, 5:52:37 AM
8/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
非常不错的设计~模板必需简单
cougar cougar
unread,
Aug 7, 2012, 8:23:39 AM
8/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
django的模版也是慢慢完善的,一开始也没有直接的比较符
KevinPan
unread,
Aug 7, 2012, 10:21:40 PM
8/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
php曾经也有个模板引擎smarty红极一时,后来也渐渐没落了:
1. 模板语法是没有必要的。不要指望UI能懂模板语法和业务逻辑,模板还得php程序员来写。
2. php本身就是模板,最终还是回归自身语法了。
cougar cougar
unread,
Aug 7, 2012, 11:24:42 PM
8/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
简单的if 和for还是可以有的,我觉得django那个就挺合适的,够用就行
qihang zhang
unread,
Aug 7, 2012, 11:32:27 PM
8/7/12
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golang...@googlegroups.com
这个问题也是众口难调~ django的模板系统也经常被人诟病~
Reply all
Reply to author
Forward
0 new messages