Quantcast
Channel: HTML – Code Chewing
Viewing all articles
Browse latest Browse all 21

How to create & reuse a HTML template with Bash

$
0
0

Situation:

You’re a web developer and the pages within the project you’re working on require the same initial HTML or PHP template for every file, and you’re sick and tired of opening up an existing file and clicking the Save As option from there. Maybe you have to delete some content as well before you can even get to your starting point? Pain.

Instead, let’s be smarter and utilise a neat Bash script to do the hard work for us. All it requires is a straight forward Bash script along with some linux commands from within the terminal. Then all you’ve have to do in the future is type this into the terminal and you’ve have a new custom file with the template contents inside of it:

./html_template > new-file-with-template-.html

But before you can do the above, here’s the instructions:

1) First, open your IDE of choice – for me, it’s Sublime Text, and we’re going to create the HTML template in Bash. It’s not scary, honest! Give it a try.

2) Here’s a very basic HTML template for this example, which we’ll type into the IDE (You can go nuts from here, but it’s just a basic example):

#!/bin/bash

# Simple automated HTML template

cat << _EOF_
<!doctype html>
<html>
<head>
    <title>HTML Template example</title>
</head>

<body>

</body>

</html>
_EOF_

Here’s the screenshot inside of my Sublime Text IDE:

html template using bash shell

We’ve just wrote a Bash script! Some bits explained:

#!/bin/bash – This is not a comment, this tells the shell where Bash lives so it can successfully interpret the file.
- As you may have expected, everything inside of cat << _EOF_ and _EOF_ is outputted. It acts as an echo. You can actually name the EOF anything you want, but EOF (End Of File) is the convention.

3) Save this file inside your root website directory and name it html_template.sh

4) Now it’s time for some cool linux commands inside the terminal! Open up terminal and cd into the same website directory as your new Bash script. For me, it’s here:

cd Documents/website

cd with bash

5) Now we’re inside, check your file is there, using the list files with -l command to provide a detailed view:

ls -l

ls -l with bash

Here you can see our Bash script – html_template.sh. The problem is, the file permissions do not allow for it to be executed; -rw-r--r--. We need -rwx-r-x-r-x for it to executable.

6) Let’s change this using the chmod command, where we pass 755 to get the permissions we require:

chmod 755 html_template.sh

chmod 755 with bash

7) Check your file permissions have been updated by running the ls -l command for a detailed view again:

ls -l

check file permissions with bash

As you can see – our file permissions have successfully been updated! Now the script can run! Excellent.

8) It’s time to run the script. We need to trigger the script and pass it a file name that it creates, or overwrites (with the HTML template) upon execution. It’s simple – my new file will be called example.php. So here goes:

./html_template.sh > example.php

create new file with existing html template

Now let’s check example.php has been created:

ls

check html bash template

And there it is! Example.php just got created! And if I open it up in Sublime text:

bash html template output

The new file has adopted our HTML template! Awesome! And that’s how you easily create HTML templates and trigger them with Bash! And if your template ever needs updating, you can just go into the bash script and update it there. Then any new files created via this method will include the updated template.

Lots of credit goes here for this article.


Viewing all articles
Browse latest Browse all 21

Trending Articles