Class: Laze::Layout
Overview
A layout is a special kind of file that can be used to remove duplication of HTML boilerplate code. Store a header and footer of a page in a layout and simply wrap that around a given piece of content to create a page.
Inserting content into a layout
A layout can contain anything you like. The only rule is you should insert an insertion point for the content of a page. Here’s a simple example:
<html>
<head>
<title>My awesome website</title>
</head>
<body>
{{ yield }}
</body>
</html>
When your page’s content is “Welcome” your resulting page output would be:
<html>
<head>
<title>My awesome website</title>
</head>
<body>
Welcome
</body>
</html>
Laze does its best to preserve whitespace in your content.
Nested templates
You can nest multiple layouts, i.e. a layout can itself have a layout. This way you can create a master template for your site, with subtemplates for various page types.
Simply give your layout file a layout like so:
layout: my_layout
---
<html>
<head>
<title>My awesome website</title>
</head>
<body>
{{ yield }}
</body>
</html>
Template data in layout files
You can use any variables defined in your pages in your layouts, and the other way around. You could, for example, include the page title in your <title>
element:
<html>
<head>
<title>{{ page.title }} - My awesome website</title>
</head>
<body>
{{ yield }}
</body>
</html>
Constant Summary collapse
- YIELD =
Regex matching where to insert a layout’s content
/\{\{\s*yield\s*\}\}/
- SINGLE_LINE_YIELD =
Special regex matching an insertion point on a single blank line. In this case we can preserve whitespace indents.
/^(\s+)#{YIELD}\s*$/
Instance Attribute Summary
Attributes inherited from Item
#content, #parent, #properties
Class Method Summary collapse
-
.find(layout_name) ⇒ Object
Use the currently active data store to look for a layout by a given name.
Instance Method Summary collapse
-
#layout ⇒ Object
Return this layout’s own layout, to enable nested layouts.
-
#wrap(string) ⇒ Object
Apply this layout to a piece of text, returning this layout’s content with the
YIELD
replacd with the given string.
Methods inherited from Item
#ancestors, #filename, #has?, #initialize, #inspect, #number_of_subitems, #to_s
Constructor Details
This class inherits a constructor from Laze::Item
Class Method Details
.find(layout_name) ⇒ Object
Use the currently active data store to look for a layout by a given name. Returns nil
when given nil
, and a new layout instance otherwise.
100 101 102 103 |
# File 'lib/laze/layout.rb', line 100 def self.find(layout_name) return unless layout_name Secretary.current.store.find_layout(layout_name) end |
Instance Method Details
#layout ⇒ Object
Return this layout’s own layout, to enable nested layouts. Simple shortcut function.
93 94 95 |
# File 'lib/laze/layout.rb', line 93 def layout properties[:layout] end |
#wrap(string) ⇒ Object
Apply this layout to a piece of text, returning this layout’s content with the YIELD
replacd with the given string.
If the YIELD
is on a single line that is matched by SINGLE_LINE_YIELD
it will prefix every line in string
with the whitespace indent of the YIELD
.
82 83 84 85 86 87 88 89 |
# File 'lib/laze/layout.rb', line 82 def wrap(string) if content =~ SINGLE_LINE_YIELD whitespace = $1 content.sub(SINGLE_LINE_YIELD, string.split(/\n/).map { |l| whitespace + l }.join("\n")) else content.sub(YIELD, string) end end |