You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在blue-jay/blueprint中如何定义非数据库关联变量并在视图中使用

How to Use Custom Variables (Unrelated to DB Structs) in blue-jay/blueprint Templates

Got it, let's break down how to add and use custom variables (that don't tie to your database structs) in your blue-jay/blueprint app. Here's a step-by-step approach tailored to your controller/categories/category.go file:

1. Define and Pass Custom Variables in Your Controller

Blue-jay uses the render.Data type (a map[string]interface{} under the hood) to pass data to templates. You can simply add your custom key-value pairs alongside your existing struct data.

Example Controller Code:

package category

import (
    "net/http"
    "github.com/blue-jay/blueprint/lib/render" // Assuming this is your render package
    "github.com/blue-jay/blueprint/models"
)

func Show(w http.ResponseWriter, r *http.Request) {
    // Fetch your existing category struct from the DB
    category, err := models.GetCategory(1) // Example DB call
    if err != nil {
        // Handle error
        render.Error(w, r, http.StatusInternalServerError)
        return
    }

    // Add custom variables here—they can be strings, booleans, numbers, slices, etc.
    templateData := render.Data{
        "Category":   category,       // Your existing DB struct
        "PageTitle":  "Our Top Categories", // Custom string variable
        "IsPromoted": true,           // Custom boolean variable
        "TagList":    []string{"books", "electronics", "home"}, // Custom slice
    }

    // Pass the data to your template
    render.Render(w, r, "categories/show", templateData)
}

Alternatively, if you prefer type safety, create a wrapper struct that includes both your DB model and custom fields:

// Define a custom struct for template data
type CategoryTemplateData struct {
    Category   models.Category
    PageTitle  string
    IsPromoted bool
    TagList    []string
}

// In your handler:
data := CategoryTemplateData{
    Category:   category,
    PageTitle:  "Our Top Categories",
    IsPromoted: true,
    TagList:    []string{"books", "electronics", "home"},
}
render.Render(w, r, "categories/show", data)

2. Access the Custom Variables in Your HTML Template

Blue-jay uses Go's standard html/template package, so you can reference custom variables using dot notation just like your struct fields.

Example Template (views/categories/show.html):

<!DOCTYPE html>
<html>
<head>
    <title>{{.PageTitle}}</title>
</head>
<body>
    <h1>{{.PageTitle}}</h1>

    <!-- Your existing category data -->
    <div class="category-card">
        <h2>{{.Category.Name}}</h2>
        <p>{{.Category.Description}}</p>
    </div>

    <!-- Use custom boolean variable with an if statement -->
    {{if .IsPromoted}}
        <div class="promo-badge">🔥 This category is promoted!</div>
    {{end}}

    <!-- Loop through custom slice variable -->
    <div class="tags">
        Tags:
        {{range .TagList}}
            <span class="tag">{{.}}</span>
        {{end}}
    </div>
</body>
</html>

Key Notes:

  • Variable Types: Go's template engine handles all basic types (strings, bools, numbers) and complex types (slices, maps, structs) seamlessly. Match your template usage to the variable type (e.g., {{range}} for slices, {{if}} for booleans).
  • Scope: Top-level variables are available across your template. If using nested templates, pass variables explicitly or leverage blue-jay's template inheritance patterns.

内容的提问来源于stack exchange,提问作者iamtoc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:31:55