iamtoc
iamtoc

Reputation: 1709

Go blue-jay/blueprint, how to assign variable in controller and use it in html/template view?

blue-jay/blueprint is an MVC (https://blue-jay.github.io/views/) fast start golang webapp, with CRUD against mysql and other out of the box features.

Database fields from the 'struct' are not in question, those are loading and rendering fine. How to declare a variable that has nothing to do with the database fields or 'struct'.

controller/categories/category.go:

// Package category provides a simple CRUD in a web page.
package category

import (
   "net/http"

   "github.com/blue-jay/blueprint/lib/flight"
   "github.com/blue-jay/blueprint/middleware/acl"
   "github.com/blue-jay/blueprint/model/category"
   "github.com/blue-jay/core/form"
   "github.com/blue-jay/core/pagination"
   "github.com/blue-jay/core/router"
)

var (
  uri = "/categories"
)

// Load routes.
func Load() {
   c := router.Chain(acl.DisallowAnon)
   router.Get(uri, Index, c...)
   router.Get(uri+"/create", Create, c...)
   router.Post(uri+"/create", Store, c...)    
}

// Create displays html page.    
func Create(w http.ResponseWriter, r *http.Request) {
   c := flight.Context(w, r)
   v := c.View.New("categories/create")  
   v.Vars["test"] = "Testing"  <--- VARIABLE IN QUESTION

   form.Repopulate(
     r.Form,
     v.Vars,
     "parent_id",
     "title",
     "icon",
     "description",
     "url",
     "sort",
     "status_id",
     "guid_id",
     "role_id",
     "user_id")
     v.Render(w, r)
}

view/categories/create.tmpl

{{define "title"}}New Category{{end}}
{{define "head"}}{{end}}
{{define "content"}}
   <div class="page-header"><h1>{{template "title" .}}</h1></div>   
   {{ .test }}  <--- VARIABLE IN QUESTION
   {{template "footer" .}}
{{end}}
{{define "foot"}}{{end}}

I have eliminated most of the form and the model (model/category/category.go), for the sake of brevity.

All we are looking for here is how to create and fill a variable "test" from within the controller, and get it to display its value "Testing" in the template. Currently, it outputs nothing.

The thing that has me baffled is that i have declared a variable 'guid' and called it this way (seemingly) in another controller ("controller/register/register.go" in func Index and output in /view/register/index.tmpl as found at the github link below.) which seems to work fine. I can't locate what i am doing different here. Something i'm not understanding apparently.

Rest of the code: https://github.com/iamtoc/blueprint-ledger

Only a few days into golang so still very new. Any idea as to how this is possible? Thanks!

Upvotes: 1

Views: 477

Answers (1)

vitr
vitr

Reputation: 7134

I've checked out your github repository and found lots of broken imports like

"github.com/blue-jay/blueprint/model/category"

after replacing blue-jay/blueprint with your repo path iamtoc/blueprint-ledger I was able to run your original code

v.Vars["test"] = "Testing"  <--- VARIABLE IN QUESTION

   form.Repopulate(
     r.Form,
     v.Vars,
     "parent_id",
...

without any issues and could see the test template variable printed out. I suspect, you just copied the original blue-jay/blueprint project incorrectly, as, I also tested this case with the original repo and again it worked as expected.

Upvotes: 1

Related Questions