HttpResponse
The HttpResponse
has utility functions which can be used to send html, json, and file response to the client.
1. HttpResponse Methods
PortalView
This method is used to render views as plain html from resources/views/portal
directory in your plugin.
For example if you have a view in resources/views/portal/index.html
,
then you can render it with:
// handler
func (w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"title": "Dashboard",
}
api.Http().HttpResponse().PortalView(w, r, "dashboard/index.html", data)
}
It uses the file resources/views/portal/layout.html
as the layout. You must create this file in order to use the PortalView
method.
The view has access to the HttpHelpers instance. Below is an example of how to access the data and helpers in the view:
<a href='<% .Helpers.UrlForRoute "dashboard" %>'>
<% .Data.title %>
</a>
AdminView
This method is very similar to PortalView but it is used to render views from resources/views/admin
directory in your plugin. It also uses the file resources/views/admin/layout.html
as the layout. You must create this file in order to use the AdminView
method.
Below is an example of how to render a view from the admin directory:
// handler
func (w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"title": "Dashboard",
}
api.Http().HttpResponse().PortalView(w, r, "dashboard/index.html", data)
}
<a href='<% .Helpers.UrlForRoute "dashboard" %>'>
<% .Data.title %>
</a>
View
This method is similar to PortalView and AdminView but it is used to render any views from the resources/views
directory in your plugin as plain html. It does not use any layout view. It also has access to the HttpHelpers instance and data.
File
This method is used to render text and asset files from the resources
directory. Just like PortalView and AdminView methods, the template files have access to the HttpHelpers instance and data. The response header's Content-Type
will be automatically derived from the filename. Below is an example of how to render a file:
data := map[string]string{}
"title": "Dashboard",
}
api.Http().HttpResponse().File(w, r, "assets/js/app.js", data)
Json
This method is used to send json response to the client. Below is an example of how to send json response:
// handler
func (w http.ResponseWriter, r *http.Request) {
data := map[string]string{
"title": "Dashboard",
}
api.Http().HttpResponse().Json(w, data, http.StatusOK)
}
2. Template Parsing
The views are parsed using the html/template package. But instead of using {{ }}
as delimiters, we are using <% %>
as delimiters. This is to avoid conflicts with the {{ }}
delimiters used in the frontend framework.
3. Layout View
In order to use HttpResponse.PortalView and HttpResponse.AdminView methods, a layout view must be created first.
The portal layout view must be created in resources/views/portal/layout.html
and the admin layout view must be created in resources/views/admin/layout.html
inside your plugin directory.
The layout view is used to define the common structure of the view. For example, the layout view can be used to define the header, footer, and sidebar of the page. Below is an example of how to define the layout view: