Skip to content

Commit e1ae9b4

Browse files
author
Pedro Maciel
committed
allows for liquid interpolation and relative partial include
1 parent 02f53eb commit e1ae9b4

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ permalink: page/
3131
---
3232
.container
3333
%h3= "{% title %}"
34-
34+
3535
:javascript
3636
$(document).ready(function(){});
3737
```
@@ -47,10 +47,10 @@ For clean content blocks, I find it helps to use Haml's `:markdown` filter if I
4747
4848
Once upon a time, in a village by the sea...
4949
```
50-
50+
5151
### Partials
5252

53-
The gem adds the liquid filter `haml` so you can render `_includes` that are written in Haml as well.
53+
The gem adds the liquid filter `haml` so you can render `_includes` that are written in Haml as well.
5454

5555
```liquid
5656
{% haml comments.haml %}
@@ -61,7 +61,19 @@ The gem adds the liquid filter `haml` so you can render `_includes` that are wri
6161
%meta{property: 'og:type', content: 'website'}
6262
%meta{name: 'viewport', content: 'width=device-width'}
6363
```
64-
64+
65+
If you wanna render includes outside the `_includes` folder, that's fine:
66+
67+
```liquid
68+
{% haml my_neat_folder/my_include.haml relative:true %}
69+
```
70+
71+
Also, don't shy away from using liquid interpolation:
72+
73+
```
74+
{% haml "{{ page.author }}".haml %}
75+
```
76+
6577
## About
6678

6779
I originally searched around the internet for a quick way to integrate HAML into my jekyll workflow and found a few around the internet to convert haml in different cases (layouts, partials, and posts). This gem is really just a collection of those techniques so they're easy to find and you can get back to creating your site using the slickest markup. It's made to drop in to your `Gemfile` just as easily as [jekyll-sass](https://github.com/noct/jekyll-sass).

lib/jekyll-haml/tags/haml_partial.rb

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,68 @@
33
module Jekyll
44

55
class HamlPartialTag < Liquid::Tag
6-
def initialize(tag_name, file, tokens)
6+
def initialize(tag_name, user_string, tokens)
77
super
8-
@file = file.strip
8+
@user_string = user_string
99
end
1010

1111
def render(context)
12+
@user_string = Liquid::Template.parse(@user_string)
13+
.render(context)
14+
.gsub(%r{\"|\'},'')
15+
.strip
16+
17+
file, arg = @user_string.split(' ')
18+
relative = arg.nil? ? false : !!(arg =~ /true/)
19+
1220
includes_dir = File.join(context.registers[:site].source, '_includes')
1321

1422
if File.symlink?(includes_dir)
1523
return "Includes directory '#{includes_dir}' cannot be a symlink"
1624
end
1725

18-
if @file !~ /^[a-zA-Z0-9_\/\.-]+$/ || @file =~ /\.\// || @file =~ /\/\./
19-
return "Include file '#{@file}' contains invalid characters or sequences"
26+
if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./
27+
return "Include file '#{file}' contains invalid characters or sequences"
2028
end
2129

22-
return "File must have \".haml\" extension" if @file !~ /\.haml$/
23-
24-
Dir.chdir(includes_dir) do
25-
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
26-
if choices.include?(@file)
27-
source = File.read(@file)
28-
conversion = ::Haml::Engine.new(source).render.delete("\n")
29-
partial = Liquid::Template.parse(conversion)
30-
begin
31-
return partial.render!(context)
32-
rescue => e
33-
print "Liquid Exception: #{e.message}"
34-
print "in #{self.data["layout"]}"
35-
e.backtrace.each do |backtrace|
36-
puts backtrace
37-
end
38-
abort("Build Failed")
39-
end
30+
return "File must have \".haml\" extension" if file !~ /\.haml$/
4031

41-
context.stack do
42-
return partial.render(context)
32+
if relative
33+
include_file(file, context)
34+
else
35+
Dir.chdir(includes_dir) do
36+
choices = Dir['**/*'].reject { |x| File.symlink?(x) }
37+
if choices.include?(file)
38+
include_file(file, context)
39+
else
40+
"Included file '#{file}' not found in _includes directory"
4341
end
44-
else
45-
"Included file '#{@file}' not found in _includes directory"
4642
end
4743
end
4844
end
49-
end
5045

46+
private
47+
48+
def include_file(file, context)
49+
source = File.read(file)
50+
conversion = ::Haml::Engine.new(source).render.delete("\n")
51+
partial = Liquid::Template.parse(conversion)
52+
begin
53+
return partial.render!(context)
54+
rescue => e
55+
print "Liquid Exception: #{e.message}"
56+
print "in #{self.data["layout"]}"
57+
e.backtrace.each do |backtrace|
58+
puts backtrace
59+
end
60+
abort("Build Failed")
61+
end
62+
63+
context.stack do
64+
return partial.render(context)
65+
end
66+
end
67+
end
5168
end
5269

5370
Liquid::Template.register_tag('haml', Jekyll::HamlPartialTag)

0 commit comments

Comments
 (0)