69. Let a field use the expressiveness of html

When we are building rich web pages, in addition to pre-defined pages that can be implemented using template files, there is another situation that we often encounter. That is, some part of the content, I want to use html code to show it. For example, there are several paragraphs of news content in a mixed layout of pictures and texts, and even several words in it need to be highlighted with different colors and sizes.
In this case, for the web background, it is generally the content edited by the rich text editor.
Of course, this content is often exclusive to a field in a record.
If the content of this field is displayed on the client side, you need to define the field type as template.HTML
Just like this.

type News struct {
    NTitle  string      //Title
    NAuthor string  //author
    NPublish    string  //Release time
    NContent    template.HTML   //content
    NAttachment string  //Enclosure
    NKeyword    string  //Keyword
    NTab        string  //Label
    NClass      string  //classification
}
func Joeltemplate11(writer http.ResponseWriter, request *http.Request) {
    var RNTitle = request.FormValue("NTitle")
    var RNAuthor = request.FormValue("NAuthor")
    var RNPublish = request.FormValue("NPublish")
    var RNContent = request.FormValue("NContent")
    var RNAttachment = request.FormValue("NAttachment")
    var RNKeyword = request.FormValue("NKeyword")
    var RNTab = request.FormValue("NTab")
    var RNClass = request.FormValue("NClass")

    myNews := News{}
    myNews.NTitle = RNTitle
    myNews.NAuthor = RNAuthor
    myNews.NPublish = RNPublish
    myNews.NContent = template.HTML(RNContent)
    myNews.NAttachment = RNAttachment
    myNews.NKeyword = RNKeyword
    myNews.NTab = RNTab
    myNews.NClass = RNClass

    t, _ := template.ParseFiles("./JoelTemplate/sayHelloNews.html")
    t.ExecuteTemplate(writer, "news", myNews)
}
{{define "news"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>News</title>
</head>
<body>
News
<hr>
Title:{{.NTitle}}<br>
Author:{{.NAuthor}}<br>
Published:{{.NPublish}}<br>
Content:{{.NContent }}<br>
Enclosure:{{.NAttachment}}<br>
Keyword:{{.NKeyword}}<br>
Label:{{.NTab}}<br>
Classification:{{.NClass}}<br>
<hr>
<form action="" method="post">
    <table>

        <tr>
            <td>Title:</td>
            <td><input id="NTitle" name="NTitle" value=""></td>
        </tr>
        <tr>
            <td>Author:</td>
            <td><input id="NAuthor" name="NAuthor" value=""></td>
        </tr>
        <tr>
            <td>Published:</td>
            <td><input id="NPublish" name="NPublish" value=""></td>
        </tr>
        <tr>
            <td>Content:</td>
            <td><textarea id="NContent" name="NContent" rows="5" cols="80" >{{.NContent }}</textarea></td>
        </tr>
        <tr>
            <td>Enclosure:</td>
            <td><input id="NAttachment" name="NAttachment" value=""></td>
        </tr>
        <tr>
            <td>Keyword:</td>
            <td><input id="NKeyword" name="NKeyword" value=""></td>
        </tr>
        <tr>
            <td>Label:</td>
            <td><input id="NTab" name="NTab" value=""></td>
        </tr>
        <tr>
            <td>Classification:</td>
            <td><input id="NClass" name="NClass" value=""></td>
        </tr>
        <tr>
            <td></td>
            <td><input id="NSubmit" type="submit" value="Submission"/></td>
        </tr>

    </table>
</form>
</body>
</html>
{{end}}

This is a news structure, in which "content" should show a variety of rich forms (pictures, text, audio and video files, links, etc.), so its type is defined as template.HTML
The performance of the front end can be like this


The content is directly html code, and is directly applied on the page

Well, with this method, you can build pages at will.

Posted by kenyabob on Wed, 01 Jan 2020 12:35:44 -0800