Beego Framework Custom Template functions

Source: Internet
Author: User
Tags dateformat

Beego Support Template functions are not many, sometimes the front-end display data, to format the data, so use the custom template function

For example, my front-end template has time and template size of these 2 data, the original data are int timestamp and byte unit data, we want to show the data more friendly, we need to format the data

The effect is like this, you can see the time and size these 2 columns are already friendly to show the way

The code in the front-end template is like this


<td>
{{substr $v. ID 0 12}}
</td>
<td>
{{$v. Repotags}}
</td>

<td>
{{$v. Created | convertt}}
</td>

<td>
{{$v. virtualsize | convertm}} M
</td>


Data conversion through the 2 functions of Convertt and Convertm respectively, note: Must be in Beego. Before the Run () call


Package Main

Import (
_ "Dockeradmin/models"
_ "Dockeradmin/routers"
"FMT"
"Github.com/astaxie/beego"
"Time"
)

Func Convertm (in Int64) (out Int64) {
out = in/1000000
Return
}
Func Convertt (in Int64) (out string) {
TM: = time. Unix (in, 0)
Out = TM. Format ("2006-01-02 03:04:05 PM")
Return
}
Func Main () {

Beego. Addfuncmap ("Convertm", Convertm)
Beego. Addfuncmap ("Convertt", Convertt)
Beego. Run ()
}
You might wonder why format strings are used in the 2006-01-02 format. In the go language, these numbers are all special, not randomly assigned numbers, see the following list:

Month 1,01,jan,january

Day 2,02,_2

When 3,03,15,pm,pm,am,am

Minute 4,04

Seconds 5,05

Years 06,2006

Weeks Mon,monday

Time zone difference indicates -07,-0700,z0700,z07:00,-07:00,mst

Time zone letter abbreviation MST

Of course Beego still have some built-in template functions, I didn't succeed with DateFormat, so I used a custom function

DateFormat implements the format of the time, returns a string, and uses the method {{DateFormat. Time ' 2006-01-02t15:04:05z07:00 '}.
Date implements a PHP-like date function that can easily be returned based on the string, using the method {date. T "Y-m-d H:i:s"}}.
Compare implements a comparison of two objects, if the same returns true, or False, using method {{Compare. A. B}.
SUBSTR implements the interception of strings, supporting the perfect interception of Chinese interception, using the method {{substr. STR 0 30}}.
HTML2STR to convert HTML into a string, remove some script, CSS and other elements, return plain text information, using the method {{html2str. Htmlinfo}}.
Str2html implements to output the corresponding string as HTML, without escaping, using the method {{str2html. strHTML}}.
Htmlquote implements the basic HTML character escape, using the method {htmlquote. Quote}}.
Htmlunquote implements the basic reverse transfer character, using the method {Htmlunquote. unquote}}.
RenderForm generate the corresponding form directly according to Structtag, using method {{&struct | renderform}}.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.