Papercraft Update: New Version, New Website

I’ve been working quite a bit on Papercraft these last few weeks. Yesterday I released Papercraft version 2.16, and here are some of the notable changes introduced since the last update:

• Emit DOCTYPE for html tag by default. Before this change, you needed to use the html5 tag to include the DOCTYPE at the top of the generated markup. Now you can just use html. This is important since this way you avoidquirks mode.

• Do not content of style and script tags. This makes it easier to write inline CSS and Javascript.

• Add Papercraft.markdown_doc convenience method which returns a Kramdown::Document instance for further processing of Markdown content.

• Add support for rendering of namespaced components, so you can now do stuff like Foo::Bar('baz') right in your templates.

New Papercraft Website

I’ve also been working on a website for Papercraft and it’s finally online. Check it out:

papercraft.noteflakes.com

Like the noteflakes.com website, which you’re currently reading, the Papercraft website is made usingSyntropy. All of the documentation pages are written using Markdown. Let’s look at some examples of how Papercraft is used on its own website:

The Default Layout

Here is the content of the default layout (source code):

It’s all pretty standard except for that export at the top, which means that this file is loaded by Syntropy as a Syntropy module (more on that later). There’s also the auto_refresh_watch! directive, which is a Syntropy extension that permits refreshing the page automatically whenever the source code changes in development mode.

The Docs Layout

The documentation page layout (source code) is a bit more involved, bit basically it is derived from the default layout using apply:

I’ve omitted the header and the side bar for the sake of brevity, so let’s look at the article element which contains the actual content of the page. There’s the title, there’s the pre-rendered HTML rendered from the Markdown content (but you can just as well use the markdown method to render it in-place), and then the nav element holds links to the previous and next pages. You can see how the logic flows naturally along the HTML content expressed with plain Ruby.

Here’s the code that renders the documentation pages (source code):

The page entry is retrieved from the Pages collection, and then passed to the layout template, along with some other metadata.

The Landing Page

The landing page (a.k.a. the index page) also uses the apply method to fill the default layout with content. Here’s a part of it (source code):

As you can see, we can mix HTML and Markdown content freely. Another thing that may stick out is the fact that I (almost) don’t use any CSS classes. I prefer using semantic tag names, which not only makes the templates much more readable, but also makes the generated HTML much smaller in size, which helps in creating a snappy user experience.

Refactoring Opportunities

For the Papercraft site, since there are basically just two kinds of layouts, with little in common (except for the outer HTML envelope), I didn’t really feel there was a need to create components. But this possibility always exists. For example, let’s look at another snippet from the landing page:

There are a total of six “featurettes” like that on the landing page, so supposing we wanted to create a featurette component, it might look like this:

And then the landing page markup would look as follows:

I think from the point of view of effort vs gain it’s not so interesting to do this, but this is certainly a possibility, and just goes to show how easy it is to compose and reuse templates in Papercraft.

Integration with Other APIs

Another thing that occurred to me while working on the Papercraft website is that in fact a lot of the difficulties or issues surrounding the integration of a templating library with existing frameworks or tools just disappear with Papercraft. There’s no boilerplate code, no ceremony around setting up state or context objects. Your templates become pure functions that take some parameters as input, and give you back HTML code, ready to serve. And, did I mention it’sreally fast?

Please feel free to test-drive Papercraft in your projects. Head on over to thePapercraft website, and enjoy!

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论