Raw HTML is very verbose and, at times, repetitive. We will utilize a third-party template engine, called Ace, which will let us write cleaner, more succinct markup.
- [Narrator] Hi. And welcome to Section 3: Become the Librarian. During Section 2, we added Book Search and Selection to our Library app. In Section 3, we'll use that foundation to start displaying and managing our book collection, while also taking care of our codebase. We'll start this section by replacing the default template rendering engine with a third-party library. Next, we'll update the UI to display our book collection. Then, we'll add UI elements to delete books we no longer own. Finally, we'll integrate the popular gorilla/mux library into our application.
In this video, we'll take advantage of third-party libraries to make writing HTML easier. Creating an HTML file can start to feel very repetitive with raw HTML. If we replace the standard library's templating engine, we can use a markup language which is less verbose. To start, we'll choose a new template rendering engine. Then we'll replace the default HTTP template engine with our new third-party engine. Finally, we'll update our original template using our new syntax. Let's get started. If you look at the first route we wrote, you'll remember that we've been using the standard library's HTML template package to render a template.
If we look at 'index.html', it's obvious that using a default template engine essentially means writing straight HTML code. Keeping track of all these angle brackets and ending nodes is cumbersome and will make it harder to upkeep our templates in the future. One of the more popular alternatives to the default template engine is Ace. Ace is inspired by some templates for Ruby and jTemplates for Node.js. This example on the homepage sums up the syntax for writing Ace templates. Ace is space-sensitive and there are no angle brackets or closing tags to deal with.
Ace makes writing HTML easier and de-clutters our template files. Since this a third-party library, we'll use 'go get github.com/yosssi/ace' to download and install the package. This may take a moment based on your computer hardware and network connection. Next, we'll need to update the main .go file to use Ace. Add 'github.com/yosssi/ace' to the import list at the top of this file. And, while we're here, let's remove 'html/template'.
Ace works a little differently from HTML template. We no longer need this call to parse files. Instead, we'll use 'ace.load' to load our template on demand in the route, given the file name, subtemplate, and a list of options. In this case, we'll use 'nil' to use the default option. Each call to load stores the parsed template in a cache for later use, which means that the template will only be parsed the first time the handler is called. We'll check for an error and return an internal server error if necessary.
Instead of using this template Execute method, we'll need to call Execute on the template object we just created. Execute takes in the response writer and our Page object as parameters. This last part may be a little tricky. We'll walk through our HTML file and convert the syntax to Ace. Let's start by making a copy of the 'index.html' file and calling the new file 'index.ace'. At the top of the file, let's add '= doctype html' to tell the web browser that this will be an HTML5 file.
Now we'll remove all the closing tags. There aren't very many, but you're free to use a Find and Replace with a regular expression to do this if you want to avoid missing any by hand. Okay, good. Now we can lean on our Text Editor to find all left angle brackets and replace them with nothing. And we can do the same thing with all the right angle brackets. For the CSS we've embedded in the head tag, we'll replace the style node with '= css', which is shorthand to let the Ace parser know to expect raw CSS.
Fortunately, our current codebase is small so our transition was relatively painless. In this video, we found a new third-party library to make writing HTML better, called Ace, and integrated it within our application. In the next video, we'll create a new UI to start displaying our book collection.
Go is on the rise. This open-source programming language (created by Google!) comes with a strong and accessible standard library, and provides a growing ecosystem of tools, libraries, and frameworks that can help you build web applications fast. Go for Web Development gets you started with the language, opening with the classic "Hello world" exercise and culminating in a web application with a strongly designed database back end, useful middleware, UI with an intelligent search function, multi-user authentication, and more.
First, learn how to build a web server with Go's extensive standard library. Explore the concepts behind a single-page web application and learn how to create a dynamic user interface, manipulate a database, and use powerful encryption algorithms to implement an authentication system. Next, you'll incorporate more functionality by calling out to external libraries from the database. Finally, it all comes together into an optimized package you can deploy into the wild with Heroku.
- Creating a route in Go
- Using templates
- Building database connections
- Collecting data
- Using web middleware
- Using the Ace template engine
- Integrating HTTP routers like gorilla/mux
- Authenticating users
- Optimizing a Go codebase