made templates contextual

This commit is contained in:
thewhitetulip 2015-11-21 18:50:51 +05:30
parent a5f50060aa
commit bb5e280a69
8 changed files with 39 additions and 23 deletions

View File

@ -26,18 +26,20 @@ func Close() {
//GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed
func GetTasks(status string) []types.Task {
func GetTasks(status string) types.Context {
var task []types.Task
var context types.Context
var TaskID int
var TaskTitle string
var TaskContent string
var TaskCreated time.Time
var getTasksql string
if status == "pending" {
getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc"
} else if status == "trashed" {
} else if status == "deleted" {
getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc"
} else if status == "complete" {
} else if status == "completed" {
getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc"
}
@ -56,8 +58,8 @@ func GetTasks(status string) []types.Task {
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]}
task = append(task, a)
}
return task
context = types.Context{Tasks: task, Navigation: status}
return context
}
//GetTaskByID function gets the tasks from the ID passed to the function

View File

@ -4,7 +4,8 @@
<head>
<title>Tasks</title>
<title>{{if eq .Navigation "pending"}} Tasks {{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted{{end}}</title>
<!-- Mobile viewport optimized -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
@ -40,8 +41,8 @@
<nav class="navbar navbar-default navbar-fixed-top mainHeader">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="/"> Tasks</a>
<a class="navbar-brand" href='{{ if eq .Navigation "pending"}} / {{else}} /{{.Navigation}} {{end}}'> {{if eq .Navigation "pending"}} Tasks {{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted{{end}}</a>
<span id="icons">
<form action="/search/" method="POST">
<input type="text" name="query" placeholder="Search" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
@ -64,14 +65,14 @@
<li class="sidebar-group"><span>Tasks</span>
<ul class="sidebar-group-menu">
<li class="sidebar-item">
<a href="/" ><span class="glyphicon glyphicon-tasks"></span> <span class="nav-item">Pending</span></a>
<!-- class="active" -->
<a href="/" {{ if eq .Navigation "pending"}} class="active" {{end}} ><span class="glyphicon glyphicon-tasks"></span> <span class="nav-item">Pending</span></a>
</li>
<li class="sidebar-item">
<a href="/completed/"><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a>
<a href="/completed/" {{ if eq .Navigation "completed"}} class="active" {{end}}><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a>
</li>
<li class="sidebar-item">
<a href="/deleted/"><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a>
<a href="/deleted/" {{ if eq .Navigation "deleted"}} class="active" {{end}}><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a>
</li>
</ul>
</li>

View File

@ -1,7 +1,7 @@
{{template "_head.html"}}
{{template "_head.html" .}}
<div class="timeline">
{{ if .}} {{range .}}
{{ if .}} {{range .Tasks}}
<div class="note">
<p class="noteHeading">{{.Title}}</p>
<hr>

View File

@ -1,4 +1,4 @@
{{template "_head.html"}}
{{template "_head.html" .}}
<!--end mainHeader -->
{{if .}}
<a href="/delete/all">
@ -7,7 +7,7 @@
{{end}}
<div class="timeline">
{{ if .}} {{range .}}
{{ if .}} {{range .Tasks}}
<div class="note">
<p class="noteHeading">{{.Title}}</p>
<hr>

View File

@ -1,4 +1,4 @@
{{template "_head.html"}}
{{template "_head.html" .}}
<!--end mainHeader -->
<button class=" btn-danger btn glyphicon glyphicon-plus floating-action-icon floating-action-icon-add"></button>
@ -32,7 +32,7 @@
</div>
<div class="timeline">
{{ if .}} {{range .}}
{{ if .}} {{range .Tasks}}
<div class="note">
<p class="noteHeading">{{.Title}}</p>
<hr>

View File

@ -1,4 +1,4 @@
{{template "_head.html"}}
{{template "_head.html" .}}
<div class="timeline">
{{ if .}} {{range .}}
<div class="note">

View File

@ -1,8 +1,15 @@
package types
//Task is the struct used to identify tasks
type Task struct {
Id int
Title string
Content string
Created string
}
//Context is the struct passed to templates
type Context struct {
Tasks []Task
Navigation string
}

View File

@ -26,6 +26,9 @@ func PopulateTemplates() {
var allFiles []string
templatesDir := "./public/templates/"
files, err := ioutil.ReadDir(templatesDir)
if err != nil {
fmt.Println("Error reading template dir")
}
for _, file := range files {
filename := file.Name()
if strings.HasSuffix(filename, ".html") {
@ -37,6 +40,9 @@ func PopulateTemplates() {
fmt.Println(err)
}
templates, err = template.ParseFiles(allFiles...)
if err != nil {
fmt.Println(err)
}
homeTemplate = templates.Lookup("home.html")
deletedTemplate = templates.Lookup("deleted.html")
@ -58,7 +64,7 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("trashed") //false when you want deleted notes
context := db.GetTasks("deleted") //false when you want deleted notes
deletedTemplate.Execute(w, context)
}
}
@ -96,7 +102,7 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("complete") //false when you want finished notes
context := db.GetTasks("completed") //false when you want finished notes
completedTemplate.Execute(w, context)
} else {
http.Redirect(w, r, "/", http.StatusFound)
@ -112,7 +118,7 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
} else {
task := db.GetTaskByID(id)
editTemplate.Execute(w, task)
}
}
} else {
http.Redirect(w, r, "/", http.StatusFound)
}
@ -190,7 +196,7 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
//UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST"{
if r.Method == "POST" {
r.ParseForm()
id, err := strconv.Atoi(r.Form.Get("id"))
if err != nil {