23.03.2013 Views

Build rapid and lightweight static websites with Hyde - IBM

Build rapid and lightweight static websites with Hyde - IBM

Build rapid and lightweight static websites with Hyde - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong><br />

<strong>Hyde</strong><br />

Remove the mystery from content management <strong>with</strong> a<br />

website development toolkit based on the plain file system<br />

Uche Ogbuji<br />

Principal Consultant<br />

Fourthought Inc.<br />

Skill Level: Intermediate<br />

Date: 05 Feb 2013<br />

Web publishing frameworks are great, but sometimes they're just too much<br />

<strong>and</strong> you want a simple, <strong>static</strong> site that you can rely on for good <strong>and</strong> consistent<br />

performance. Static site generators are a useful hybrid for those occasions when<br />

you want the convenience of a web publishing framework <strong>with</strong>out the overhead.<br />

<strong>Hyde</strong> is a popular site generator that provides powerful templating, based on<br />

Django, <strong>and</strong> metadata management. It's implemented in Python but does not<br />

strictly require Python knowledge. In this article, learn to use <strong>Hyde</strong> to accelerate<br />

development of <strong>static</strong> <strong>websites</strong>.<br />

Overview<br />

Early in the history of the Web, most pages were just pages on a file system on a<br />

server somewhere. As the Web became more sophisticated, <strong>with</strong> dynamic sites, ecommerce,<br />

large-scale publishing, <strong>and</strong> applications on the Web, it became more <strong>and</strong><br />

more common to switch from simple files to sophisticated web content management<br />

systems (CMS) <strong>and</strong> publishing tools. These days many thous<strong>and</strong>s of sites run on:<br />

• <strong>IBM</strong>® WebSphere®<br />

• Other Java® frameworks or .NET<br />

• Popular, alternative web publishing frameworks, such as Ruby on Rails <strong>and</strong><br />

Django<br />

• Community-oriented CMSs, such as WordPress<br />

It has become almost expected that anything but the simplest sites requires a CMS<br />

<strong>with</strong> modern facilities <strong>and</strong> workflow.<br />

But many old-timers on the Web, such as I am, remember some of the advantages<br />

of the simpler, old days. For one thing, serving <strong>static</strong> files requires less server<br />

© Copyright <strong>IBM</strong> Corporation 2013 Trademarks<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 1 of 9


developerWorks® ibm.com/developerWorks/<br />

horsepower. Another issue is backups <strong>and</strong> content portability. When you choose a<br />

CMS or web publishing framework, you rely on it to make sense of your content. With<br />

your content tied up in database tables, you can't easily reach in <strong>and</strong> grab a file to<br />

share. The move to a different infrastructure can be a daunting task. You also have to<br />

rely on specialized tools for backup <strong>and</strong> restore.<br />

The characteristic underlying these issues is transparency. Web publishing<br />

frameworks <strong>and</strong> CMSs are generally not transparent, which means a lot of special<br />

knowledge is required to underst<strong>and</strong> how they store, manage, <strong>and</strong> serve up content.<br />

To some extent, this requirement is inevitable on all but the simplest <strong>websites</strong>, but<br />

recently emerging projects offer some frameworks benefits, <strong>with</strong>out losing the core<br />

element of processing <strong>and</strong> publishing simple files on a server. One popular such<br />

framework is Jekyll. Some developers built on the core ideas of Jekyll, but using a<br />

different set of building blocks created the open-source <strong>Hyde</strong> project. <strong>Hyde</strong> is now my<br />

web publishing system of choice. It is simple yet offers a great deal of power, <strong>and</strong> it<br />

limits most of its business to processing simple files to generate a <strong>static</strong> site, resulting<br />

in a great deal of efficiency <strong>and</strong> flexibility.<br />

Getting started <strong>with</strong> <strong>Hyde</strong><br />

<strong>Hyde</strong> is a system written in Python that allows you to manage content <strong>and</strong> templates<br />

in a directory on the file system of your web development machine. You create or<br />

update the content using a regular text editor <strong>and</strong> comm<strong>and</strong>-line tools, or you can<br />

use an IDE if you like. When you're ready to see how it looks, <strong>Hyde</strong> can generate the<br />

site <strong>and</strong> serve it up locally using a test web server. When you are satisfied <strong>with</strong> how<br />

the site looks in test mode, you can push the <strong>static</strong>, generated files to your production<br />

web server for Apache, Lighttpd, Nginx, or any web server that can serve files at<br />

lightning speeds.<br />

Installation <strong>and</strong> setup<br />

If you do web development on Mac or Linux systems, you already have the main<br />

thing you need, Python. Python is also easy to install on Microsoft® Windows®<br />

<strong>and</strong> most other platforms. To install <strong>Hyde</strong> in your Python library, invoke sudo<br />

easy_install hyde or pip install hyde, depending on your setup. When you do<br />

so, the hyde comm<strong>and</strong> should be available. To start, set up <strong>Hyde</strong> in a directory <strong>with</strong> a<br />

skeleton set of files for a typical website <strong>with</strong> this comm<strong>and</strong>:<br />

hyde -s $PATH_TO_DIRECTORY create<br />

Replace $PATH_TO_DIRECTORY <strong>with</strong> the directory where you want to work on content<br />

<strong>and</strong> template files.<br />

The above comm<strong>and</strong> uses a default layout for a site, but <strong>Hyde</strong> allows you to pick<br />

other site layouts. This option is useful if you want to use some well-known toolkit or<br />

setup for your site, for example, if you use the Twitter Bootstrap framework, or if you<br />

know the target site will be a weblog.<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 2 of 9


ibm.com/developerWorks/ developerWorks®<br />

If you switch to the newly created site directory using the default layout, you should<br />

see the following subdirectories:<br />

README.markdown content info.yaml layout site.yaml<br />

Each of these entries offers a different side of <strong>Hyde</strong>.<br />

Markdown<br />

Not many people enjoy writing HTML, <strong>and</strong> <strong>with</strong> <strong>Hyde</strong> you can minimize how much<br />

HTML you deal <strong>with</strong>. Most content management systems offer ways to avoid h<strong>and</strong>authoring<br />

HTML, usually using special what-you-see-is-what-you-get (WYSIWYG)<br />

widgets that give you a word-processor feel while generating HTML. Another<br />

approach is well-known from wikis. You edit plain text using special characters to<br />

indicate the various HTML markup constructs. These are called <strong>lightweight</strong> markup<br />

languages, <strong>and</strong> one of the more popular specimens is Markdown.<br />

<strong>Hyde</strong> builds in a processor that allows you to author web pages in Markdown rather<br />

than HTML. I shall not undertake a full introduction to Markdown in this article, but<br />

Listing 1 is a small example of rich content in Markdown format.<br />

Listing 1. Example of rich content in Markdown format<br />

For immediate release. **Boulder, Colorado, 1 January 2012** The new year began<br />

<strong>with</strong> a bang.... At city hall a local first nation elder was invited to offer<br />

residents a greeting of *Heebee*, or "Hello" in Arapaho, <strong>and</strong> recorded for the<br />

[city Web page](http://www.bouldercolorado.gov/). One resident said his New Year<br />

resolutions were to:<br />

* Lose weight<br />

* Go camping for at least one week in the summer<br />

* Pay off one credit card<br />

A Markdown processor such as the one bundled <strong>with</strong> <strong>Hyde</strong> can convert this into<br />

HTML such as in Listing 2.<br />

Listing 2. HTML output from Markdown content in Listing 1<br />

For immediate release.<br />

Boulder, Colorado, 1 January 2012<br />

The new year began <strong>with</strong> a bang....<br />

At city hall a local first nation elder was invited to offer residents<br />

a greeting of Heebee, or "Hello" in Arapaho, <strong>and</strong> recorded<br />

for the city Web page.<br />

One resident said his New Year resolutions were to:<br />

<br />

Lose weight<br />

Go camping for at least one week in the summer<br />

Pay off one credit card<br />

<br />

Site configuration <strong>and</strong> YAML<br />

YAML Ain't Markup Language (YAML) is a language that some programmers like to<br />

use for configuration files. A site built <strong>with</strong> <strong>Hyde</strong> does require some configuration, for<br />

example, to control how templates are applied. The default site.yaml file generated<br />

by <strong>Hyde</strong> specifies a few plug-ins, indicates how some local file directories are<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 3 of 9


developerWorks® ibm.com/developerWorks/<br />

translated to paths for the web server, <strong>and</strong> provides basic metadata for some key<br />

pages.<br />

Templates <strong>and</strong> Jinja2<br />

Most <strong>websites</strong> these days can benefit from some degree of templating. A template<br />

system can help you separate common elements of your site's structure <strong>and</strong> pages<br />

from each page's individual content. For example, if you look at a weblog, sometimes<br />

more than half of the HTML of each page is boilerplate replicated across all entries.<br />

A template allows you to express that 50 percent of content only once <strong>and</strong> have it<br />

automatically applied to all the pages. A template system can also support smart<br />

features such as automatically generating a calendar of a weblog's archives.<br />

<strong>Hyde</strong> uses a similar template language to that of the popular Django framework,<br />

implemented through the Jinja2 project. Each template looks like HTML <strong>with</strong> some<br />

additional, special syntax for the template comm<strong>and</strong>s, as explored further below.<br />

The templates go in the layout directory, <strong>and</strong> you add <strong>and</strong> update templates there to<br />

manipulate the overall content outline of your site.<br />

Your content<br />

Most important of all is your own site's content, which you maintain in the directory<br />

of that name. You would generally have one file for each unique web page. Each<br />

content file starts <strong>with</strong> the page metadata <strong>and</strong> then has the HTML <strong>and</strong> template code<br />

for the page. This directory is also where you find <strong>static</strong> files to support the site, such<br />

as images, stylesheets, <strong>and</strong> script files.<br />

A small example<br />

A quick way to start <strong>with</strong> a nice-looking website is to download one of the many<br />

free web designs that include HTML, CSS, <strong>and</strong> even scripts as needed for a site's<br />

presentation. You can then replace the design's boilerplate <strong>with</strong> your own content <strong>and</strong><br />

tweak the look <strong>and</strong> feel. This is a straightforward process <strong>with</strong> <strong>Hyde</strong>, <strong>and</strong> you needn't<br />

restrict yourself to a web design for that specific software. In this section, I chose a<br />

regular, free HTML5/CSS3 template <strong>and</strong> adapt it for <strong>Hyde</strong>. It's called "Minimalism,"<br />

by Marija Zaric (see Resources), <strong>and</strong> is available under the Creative Commons<br />

Attribution (by) 3.0, so you can use it freely, even in commercial projects, as long<br />

as you credit the author for use of the template. Your <strong>websites</strong> using the template<br />

become derivative works of the template.<br />

I downloaded the template package <strong>and</strong> unzipped it. It contains an index.html file.<br />

The <strong>Hyde</strong> folder's content directory also contains a file by the same name, but it's<br />

not actually HTML but a Jinja2 template based on base.j2. What I did was turn the<br />

Minimalism file into a new template, by copying it as minimalism.j2. At the same time<br />

I copied the other, support files for the template to the appropriate locations:<br />

cp minimalism/index.html<br />

$PATH_TO_DIRECTORY/layout/minimalism.j2 cp minimalism/css/styles.css<br />

$PATH_TO_DIRECTORY/content/media/css/ cp minimalism/images/*<br />

$PATH_TO_DIRECTORY/content/media/images/<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 4 of 9


ibm.com/developerWorks/ developerWorks®<br />

Turning a design template into a Jinja2 template<br />

In a website, you'll often have the same basic layout <strong>and</strong> design for many of the<br />

pages, <strong>with</strong> differences in content <strong>and</strong> metadata. To turn the Minimalism design<br />

template into a Jinja2 template that you can use for multiple actual pages, figure out<br />

what is general to all the pages <strong>and</strong> what is specific to each one. Look at the head<br />

element from Minimalism:<br />

<br />

<br />

Minimalism - Home<br />

<br />

<br />

<br />

<br />

<br />

In this case, pretty much everything but the title is shared across pages, So I'll turn<br />

just the title content into a template field from metadata:<br />

{{ resource.meta.title }}<br />

The head element also links to icons, styles, <strong>and</strong> other resources. <strong>Hyde</strong> manages the<br />

correspondence between the folders for such resources <strong>and</strong> their URLs in resulting<br />

pages, so you have to update these links to use a special <strong>Hyde</strong> function:<br />

<br />

...<br />

<br />

You can place the favicon.gif file inline during development. When you launch a site<br />

into production, it's best to locate the icon image at the root of the domain, <strong>and</strong> the<br />

explicit links become superfluous.<br />

You also need to update any images or links to JavaScript files or other resources,<br />

using the same pattern as in previous examples. For example, one feature image<br />

used to show off the template looks like this after update:<br />

<br />

<br />

At this point, <strong>Hyde</strong> can display the template just as it looks on the demo site from<br />

which you downloaded it. But your work is not entirely done. Now you have to set up<br />

to replace the "Lorem Ipsum" dummy text <strong>with</strong> useful content.<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 5 of 9


developerWorks® ibm.com/developerWorks/<br />

Filling in content<br />

You can easily to mark places in the template where you want to fill in pagespecific<br />

markup. Just add Jinja block markers. Listing 3 illustrates in a section from<br />

minimalism.j2 .<br />

Listing 3. Section of the "Minimalism" template illustrating Jinja block markers<br />

<br />

{% block intro %}<br />

<br />

<br />

"Simplicity is the ultimate sophistication"<br />

- Leonardo da Vinci<br />

<br />

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.<br />

Donec molestie. Sed aliquam sem ut arcu. Phasellus sollicitudin.<br />

Vestibulum condimentum facilisis nulla. In hac habitasse platea<br />

dictumst. Nulla nonummy. Cras quis libero.<br />

<br />

<br />

{% endblock intro %}<br />

I added {% block intro %} <strong>and</strong> {% endblock intro %} to the template HTML. As<br />

you can see, I left the dummy text in place. This is fine because I will override it<br />

page by page. Listing 4 is sample page, saved as index.html, which is based on the<br />

minimalism.j2 template but overrides two template blocks.<br />

Listing 4. Example page based on Listing 3, but overriding two Jinja template<br />

blocks, using HTML<br />

--- extends: minimalism.j2<br />

title: Strange Case of Dr Jekyll <strong>and</strong> Mr <strong>Hyde</strong><br />

description: Tribute to a Victorian classic --<br />

{% block intro %}<br />

<br />

<br />

Strange Case of Dr Jekyll <strong>and</strong> Mr <strong>Hyde</strong><br />

—R.L. Stevenson<br />

"I knew myself, at the first breath of this new life,<br />

to be more wicked, tenfold more wicked, sold a slave to my original evil;<br />

<strong>and</strong> the thought, in that moment, braced <strong>and</strong> delighted me like wine."<br />

<br />

<br />

{% endblock intro %}<br />

{% block group1 %}<br />

<br />

The author<br />

Robert Louis Balfour Stevenson (13 November 1850 – 3 December 1894)<br />

was a Scottish novelist, poet, essayist <strong>and</strong> travel writer. His best known<br />

books include Treasure Isl<strong>and</strong>, Kidnapped, <strong>and</strong><br />

Strange Case of Dr Jekyll <strong>and</strong> Mr <strong>Hyde</strong>.<br />

<br />

<br />

Read more<br />

<br />

{% endblock group1 %}<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 6 of 9


ibm.com/developerWorks/ developerWorks®<br />

Listing 5 illustrates the use of Markdown to express part of a page's content,<br />

using the {% filter markdown -%} instruction to invoke <strong>Hyde</strong>'s built-in markdown<br />

converter.<br />

Listing 5. Example page based on Listing 3, but overriding two Jinja template<br />

blocks, using Markdown<br />

--- extends: minimalism.j2 title: Strange Case of Dr Jekyll <strong>and</strong> Mr <strong>Hyde</strong><br />

description: Tribute to a Victorian classic ---<br />

{% block intro %}<br />

<br />

<br />

{% filter markdown -%}<br />

# *Strange Case of Dr Jekyll <strong>and</strong> Mr <strong>Hyde</strong>*<br />

## "I knew myself, at the first breath of this new life, to be more wicked..."<br />

{%- endfilter %}<br />

<br />

<br />

{% endblock intro %}<br />

{% block group1 %}<br />

<br />

{% filter markdown -%}<br />

### The author<br />

**Robert Louis Balfour Stevenson** (13 November 1850 – 3 December 1894) was a<br />

Scottish novelist, poet, essayist <strong>and</strong> travel writer. His best-known books include<br />

*Treasure Isl<strong>and</strong>*, *Kidnapped*, <strong>and</strong> *Strange Case of Dr Jekyll <strong>and</strong> Mr <strong>Hyde</strong>*<br />

{%- endfilter %}<br />

<br />

<br />

<br />

Read more<br />

<br />

{% endblock group1 %}<br />

Only the content <strong>with</strong>in the filter instructions are interpreted as markdown. Notice how<br />

those bits are flush left, unlike the HTML bits, which are indented according to block<br />

elements. This is because indentation is significant in Markdown <strong>and</strong> not in HTML.<br />

Summary<br />

I only scratched the surface of the features available in Jinja templates <strong>and</strong> other<br />

components of <strong>Hyde</strong>. That works fine because <strong>Hyde</strong> is designed so that you can<br />

begin <strong>with</strong> the simpler functions <strong>and</strong> work up to more sophisticated features as<br />

you go along, whenever you need to enhance automation <strong>and</strong> workflow. You can<br />

develop many simple web pages <strong>with</strong> simple blocks (which you can nest). If you need<br />

automatically compiled weblogs <strong>and</strong> the like, <strong>Hyde</strong>'s default layout provides a good<br />

example.<br />

Overall, <strong>Hyde</strong> gives you most of the features of content management systems <strong>and</strong><br />

low-level web publishing frameworks. With the file system as the backing store, very<br />

little of your system is obscured or mysterious. I think this is a great benefit, <strong>and</strong> you<br />

now have enough of the basics to get started <strong>and</strong> create a simple web page <strong>with</strong><br />

<strong>Hyde</strong>, <strong>and</strong> a web design template of your choosing.<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 7 of 9


developerWorks® ibm.com/developerWorks/<br />

Resources<br />

Learn<br />

• Learn about Markdown, the popular, text-based, <strong>lightweight</strong> markdown<br />

language.<br />

• Learn about YAML Ain't Markup Language (YAML), which is increasingly<br />

popular for configuration files, an used in <strong>Hyde</strong>.<br />

• More articles by this author (Uche Ogbuji, developerWorks, November 2000current):<br />

Read articles about web development, Python, XML, XSLT, <strong>and</strong> other<br />

technologies.<br />

• New to Web development: Learn about the many different technologies used for<br />

web app development that serves content in HTML, CSS, or JavaScript files to<br />

relational databases <strong>and</strong> server-side programming.<br />

• developerWorks Web development zone: Find articles covering various webbased<br />

solutions. See the Web development technical library for a wide range of<br />

technical articles <strong>and</strong> tips, tutorials, st<strong>and</strong>ards, <strong>and</strong> <strong>IBM</strong> Redbooks.<br />

• Find more XML resources on the developerWorks XML zone, including Uche<br />

Ogbuji's Thinking XML column.<br />

• Stay current <strong>with</strong> developerWorks technical events <strong>and</strong> webcasts focused on a<br />

variety of <strong>IBM</strong> products <strong>and</strong> IT industry topics.<br />

• Attend a free developerWorks Live! briefing to get up-to-speed quickly on <strong>IBM</strong><br />

products <strong>and</strong> tools as well as IT industry trends.<br />

• Follow developerWorks on Twitter.<br />

• Watch developerWorks on-dem<strong>and</strong> demos ranging from product installation<br />

<strong>and</strong> setup demos for beginners, to advanced functionality for experienced<br />

developers.<br />

Get products <strong>and</strong> technologies<br />

• Get started <strong>with</strong> <strong>Hyde</strong>, a <strong>static</strong> website generator that models many common<br />

CMS features.<br />

• Check out the Jinja2 template language <strong>and</strong> toolkit, which is similar to that in<br />

Django.<br />

• Grab Marija Zaric's "Minimalism" template, used for the example in this article.<br />

• Evaluate <strong>IBM</strong> products in the way that suits you best: Download a product trial,<br />

try a product online, use a product in a cloud environment, or spend a few hours<br />

in the SOA S<strong>and</strong>box learning how to implement Service Oriented Architecture<br />

efficiently.<br />

Discuss<br />

• Get involved in the My developerWorks community. Connect <strong>with</strong> other<br />

developerWorks users while exploring the developer-driven blogs, forums,<br />

groups, <strong>and</strong> wikis.<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 8 of 9


ibm.com/developerWorks/ developerWorks®<br />

About the author<br />

Uche Ogbuji<br />

Uche Ogbuji is Partner at Zepheira where he oversees creation of<br />

sophisticated web catalogues <strong>and</strong> other richly contextual databases.<br />

He has a long history of pioneering in advanced web technologies such<br />

as XML, Semantic Web <strong>and</strong> Web services, open source projects such<br />

as Akara, an open source platform for web data applications. He is a<br />

computer engineer <strong>and</strong> writer born in Nigeria, living <strong>and</strong> working near<br />

Boulder, Colorado, USA. You can find more about Mr. Ogbuji at his<br />

weblog, Copia.<br />

© Copyright <strong>IBM</strong> Corporation 2013<br />

(www.ibm.com/legal/copytrade.shtml)<br />

Trademarks<br />

(www.ibm.com/developerworks/ibm/trademarks/)<br />

<strong>Build</strong> <strong>rapid</strong> <strong>and</strong> <strong>lightweight</strong> <strong>static</strong> <strong>websites</strong> <strong>with</strong> <strong>Hyde</strong> Page 9 of 9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!