# MetaStatic Tutorial ### Building a Web Site Using the SB Admin Meta Tag Library # Introduction The MetatStatic Repository includes a Tag Library that provides a partial implementation of the Open Source [SB Admin](https://github.com/StartBootstrap/startbootstrap-sb-admin) User Interface. What's included is sufficient to demonstrate: - how a technical developer can build out such a Meta Tag Library - how a Web Site maintainer can build and maintain a site using such a Tag Library In this tutorial we'll explain in detail both aspects so that by the end of the tutorial you can begin to use MetaStatic for your own specific purposes. # Getting Started MetaStatic is written in JavaScript and therefore requires either Node.js or Bun.js to run. In this tutorial I'm going to use Bun as it's a lot faster to run and much simpler to set up and maintain than Node.js, but if you're a Node.js developer it should be straightforward to translate the instructions to make them work with Node.js. This tutorial also assumes that you'll be using a Linux machine. Bun.js is also available for Windows and MacOS and once installed, all the commands should work identically apart from file path syntax differences. So let's get started! ## Create a Work Directory Create a directory for this tutorial and switch to it. For example: ```code mkdir mstutorial cd mstutorial ``` ## Install Bun.js If you're using Linux or MacOS, the recommended way to install Bun.js is as follows: ```code curl -fsSL https://bun.sh/install | bash ``` If you're using Windows, [refer to the Bun.js documentation](https://bun.sh/docs/installation). ## Clone the MetaStatic Repository ```code git clone https://github.com/robtweed/metastatic ``` ## Switch to the Repository's */examples* directory cd cd metastatic/examples ## Start the Included Bun.js Web Server We've included a basic but handy Web Server. It's written in Bun.js and uses its extremely fast *Bun.serve* HTTP server. This Web Server will allow you to quickly view the Web Site file(s) you generate in this tutorial directly from the location in which MetaStatic will create them. Note that this Web Server is **NOT** meant for production use! You'll see the Bun Web Server in the directory that you've previously switched to: ```code bunws.js ``` and you can [view its source code here](./examples/bunws.js). Note also that the file(s) generated by MetaStatic can be served up by any Web Server, so once you're happy with them, simply upload them to your Web Server. You will, of course, need to ensure that any URL paths for images etc that you might use are correctly structured for your production Web Server. To start the Web Server, just issue the following command from the */examples* directory: ```code bun bunws.js ``` This will start the Web Server and it will listen on port 3000. If you're already using that port for something else, Bun will report an error and fail to start. If so, you can specify a different port, eg: ```code bun bunws.js 8080 ``` Note: the first time you invoke this command, Bun should automatically install all of the Web Server's dependencies. If it doesn't, and if it reports an error, eg: ```code error: Cannot find package "mg-bun-router" from "/home/ubuntu/metastatic-tutorial/bun/metastatic/examples/bunws.js" ``` Then try manually installing the missing package: ```code bun install mg-bun-router ``` You should then be able to start the Web Server. ## Test the Web Server The Bun Web Server is automatically configured to fetch static files from the */examples/sites* folders. You can test it with the *demo* site which includes a pre-built *index.html* file. In another process, type: ```code curl http://localhost:3000/demo/index.html ``` or point a browser at this same URL. if you followed the instructions correctly, you should see the file's contents or see them rendered in your browser. If so, you're ready to begin exploring MetaStatic! ---- # The SB Admin Tag Library ## Background You'll find the SB Admin Tag Library in your */examples/metaTagLibraries/sbadmin* directory. You can also see the [source files here](./examples/metaTagLibraries/sbadmin). The SB Admin User Interface (UI) is designed around a set of panels: - a top bar, for titles etc - a left-hand panel for menu options - a footer panel for site information, copyright notices etc - a main content panel This UI is based on the popular Open Source responsive Bootstrap v5 CSS framework. The built-in responsive behaviour is very nice: for example, you'll find that the menu panel will automatically collapse on small devices such as phones, and content will automatically stack vertically in small devices. As such, the SB Admin UI is very powerful and versatile and can be used for many, if not most Web Sites to provide a modern UI for your content. Of course, with MetaStatic you aren't limited to using this UI: any alternative UI can be implemented as a set of MetaStatic's Meta Tags and then used to construct your Web Site. Their design is entirely up to you! The *namespace* we've used for the SB Admin Tag Library is *sbadmin*, so you'll notice that all the individual Tag definition files are prefixed with this, eg: - sbadmin-root - sbadmin-header - sbadmin-sidebar-menu Each Meta Tag is defined in its own file which has a file extension of *.mst*, eg: - sbadmin-root.mst - sbadmin-header.mst - sbadmin-sidebar-menu.mst A Web Developer/Maintainer will use these as tags, eg: - <sbadmin-root> - <sbadmin-header> - <sbadmin-sidebar-menu> ## The Root Meta Tag Every MetaStatic Tag Library will have a top-level tag within which all other Tags are nested. What it's called is up to you. In our case we've named this tag *sbadmin-root*. You'll find it in your system at */examples/metaTagLibraries/sbadmin/sbadmin-root.mst*. You can also inspect its [source code here](./examples/metaTagLibraries/sbadmin/sbadmin-root.mst). We'll come back to explain its contents and how/why it works in detail later. For now, suffice to say that this Meta Tag: - creates the correct HTML <head> tag contents for your Web Site: - loading the required Bootstrap-compatible CSS resources; - loading the Bootstrap JavaScript resources - loading the free Font Awesome icon resources - adding some specific customisable styles that override the standard *StartBootstrap* ones - defines and creates the basic UI layout in the <body> tag ## Creating Our Web Site Let's get started and create a "bare bones" SB Admin Web Site. Change to the */examples/sites/tutorial* directory that you'll find on your system. In order to create a Web Site, you create a file with a file extension of *.meta*. The file name is otherwise up to you, but by convention, a single page Web Site will normally have a name of *index*. You'll find we've already created an instance of the *index.meta* file for you to get you started. It's very simple and just contains the *sbadmin-root* tag: ```html ``` Let's try building a Web Site from it and see what happens. ## Running the MetaStatic Builder You're best to run the MetaStatic Builder from within your */examples* directory: - change to the correct directory, eg: ```code cd ~/mstutorial/metastatic/examples ``` Then run the Builder: ```code bun build.mjs tutorial ``` Specifying *tutorial* as a command line parameter will tell the builder to find your definition *.meta* file in your */sites/tutorial* directory. By default it will look for and use a file named *index.meta*. You could specify it explicitly by adding it as a second command line parameter: ```code bun build.mjs tutorial index.meta ``` The first time you run this, Bun will probably return an error, telling you that it couldn't find the *metastatic* module. You can fix this by typing: ```code bun install metastatic ``` Now you should be able to re-run the Builder: ```code bun build.mjs tutorial ``` You should see the generated HTML returned in the terminal, but that same HTML should now be in a file named *index.html* in your */examples/sites/tutorial* directory. Try fetching it in a browser (making use of the Bun Web Server we started earlier): ```code http://localhost:3000/tutorial/index.html ``` You should see an empty version of the SB Admin UI! ![Initial SB Admin UI](./images/sbadmin-1.png) Clearly this isn't very useful as yet, but it's worth taking a look at what's been generated and why. If you inspect the contents of the generated *index.html* file, you'll see that it includes everything that was needed to render this UI. [Here's a copy of what it should contain](./examples/sites/tutorial/example1.html). Notice how the <title> tag has used a default title attribute value from the <sbadmin-root> tag: ```html MetaStatic ``` If you want to specify your own <title> text, edit the *index.meta* tag as shown: ```html ``` and re-run the builder: ```code bun build.mjs tutorial ``` You'll see that the *index.html* file has been overwritten and contains the new version of the generated markup, and if you take a look at the <title> tag, it should now contain our own modified text: ```html My MetaStatic Demo ``` It might not look like it yet, but this *sbadmin-root* tag has already generated all the basic scaffolding needed to create your own customised version of the SB Admin UI, with empty versions of: - a top bar, for titles etc - a left-hand panel for menu options - a footer panel for site information, copyright notices etc - a main content panel So in a while we'll look at how you use and populate each of the four panel areas it's created for you. But first, let's take a deep dive into that *sbadmin-root* Meta Tag definition, as it demonstrates almost all of the key features of MetaStatic. ## Key Features of the *sbadmin-root* Meta Tag We're going to take an in-depth look at the *sbadmin-root* Meta Tag. You'll find it in your system in the */examples/metaTagLibraries/sbadmin directory: it's the file named *sbadmin-root.mst*. Alternatively view the [source code for it here](./examples/metaTagLibraries/sbadmin/sbadmin-root.mst). The first thing to notice is that it contains two <template> tags and a <script> tag. ### First Template Let's start with the very first line which is the first <template> tag: ```html