Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3244

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T12:56:08+00:00 2024-11-26T12:56:08+00:00

How to use Templ with Goravel

  • 61k

Install the framework and tools

First let's install Goravel. It's a batteries included Go Web Framework for developers familiar with the Laravel Framework.

Steps from Getting started documentation:

// Download framework git clone https://github.com/goravel/goravel.git rm -rf goravel/.git*  // Install dependencies cd goravel go mod tidy  // Create .env environment configuration file cp .env.example .env  // Generate application key go run . artisan key:generate 
Enter fullscreen mode Exit fullscreen mode

Now install templ (for html templates) and air (for hot reloading)

go install github.com/a-h/templ/cmd/templ@latest go install github.com/cosmtrek/air@latest 
Enter fullscreen mode Exit fullscreen mode

Configure a simple frontend template structure

(work in progress – I will update with global variables and script push)

• Add this line in your /.gitignore file: *_templ.go
• Delete resources/views/welcome.tmpl
• Create 2 folders in resources/views, home and parts
• In the folder resources/views/parts create 3 files:
   1. resources/views/parts/header.templ

package parts  templ Header() {     <h1>Header</h1> } 
Enter fullscreen mode Exit fullscreen mode

   2. resources/views/parts/footer.templ

package parts  templ Footer() {     <footer>         <p>&copy; 2024 MyProject</p>     </footer> } 
Enter fullscreen mode Exit fullscreen mode

   3. resources/views/parts/template.templ

package parts  templ Template() {     <!DOCTYPE html>     <html>         <head>             <title>My Page</title>             <meta charset="utf-8"/>             <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>             <meta name="viewport" content="width=device-width, initial-scale=1"/>             <!-- Your styles go here -->         </head>         <body>             @Header()             { children... }             @Footer()             <!-- Your scripts go here -->             <script src="//unpkg.com/alpinejs" defer></script>         </body>     </html> } 
Enter fullscreen mode Exit fullscreen mode

• Create your homepage component: resources/views/home/index.templ

package home  import "goravel/resources/views/parts"  templ Index() {     @parts.Template() {         <h1>Homepage</h1>         <div>Templ is awesome</div>     } } 
Enter fullscreen mode Exit fullscreen mode

Their documentation for children components is here. They have an example for a layout structure but I find this method better.

Use this homepage component in your controller

I made this new file app/http/controllers/controller.go where I can store some helpers available to any controller.

package controllers  import (     "github.com/a-h/templ"     "github.com/goravel/framework/contracts/http" )  func RenderTempl(c http.Context, comp templ.Component) http.Response {     c.Response().Status(200)     c.Response().Header("Content-Type", "text/html")     comp.Render(c, c.Response().Writer())     return nil } 
Enter fullscreen mode Exit fullscreen mode

This helper renders the provided templ component in the response buffer along with a 200 status header. My first try at this I was using c.Response().Writer().Header().Set() but is does not work ! The only way to set response headers is c.Response().Header(key, val).
Let's use it in app/http/controllers/home_controller.go

package controllers  import (     "goravel/resources/views/home"     "github.com/goravel/framework/contracts/http" )  type HomeController struct {     //Dependent services }  func NewHomeController() *HomeController {     return &HomeController{         //Inject services     } }  func (r *HomeController) Index(ctx http.Context) http.Response {     // doing awesome stuff here     return RenderTempl(ctx, home.Index()) } 
Enter fullscreen mode Exit fullscreen mode

Now set this new route:

package routes  import (     "goravel/app/http/controllers"     "github.com/goravel/framework/facades" )  func Web() {     homeController := controllers.NewHomeController()     facades.Route().Get("/", homeController.Index) } 
Enter fullscreen mode Exit fullscreen mode

Configure hot reloading with Air

Goravel already comes with the configuration file (.air.toml) you need for this, we only need to add the templ generate command in the cmd parameter, like this:

[build]   bin = "./storage/temp/main" -  cmd = "go build -o ./storage/temp/main ." +  cmd = "templ generate && go build -o ./storage/temp/main ." 
Enter fullscreen mode Exit fullscreen mode

If you are using Windows add .exe to main in both bin and cmd parameters:

[build]   bin = "./storage/temp/main.exe"   cmd = "templ generate && go build -o ./storage/temp/main.exe ." 
Enter fullscreen mode Exit fullscreen mode

Done ! Happy coding !

frameworkgotemplwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.