Skip to content

Commit

Permalink
Allow passing :strict_variables and :strict_filters options to Liquid…
Browse files Browse the repository at this point in the history
…'s renderer (#6726)

Merge pull request 6726
  • Loading branch information
ashmaroli authored and jekyllbot committed Mar 14, 2018
1 parent 86d8625 commit 51bdea1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
12 changes: 11 additions & 1 deletion docs/_docs/configuration.md
Expand Up @@ -684,7 +684,9 @@ verbose: false
defaults: []

liquid:
error_mode: warn
error_mode: warn
strict_filters: false
strict_variables: false

# Markdown Processors
rdiscount:
Expand Down Expand Up @@ -713,6 +715,14 @@ options are
- `warn` --- Output a warning on the console for each error.
- `strict` --- Output an error message and stop the build.

You can also configure Liquid's renderer to catch non-assigned variables and
non-existing filters by setting `strict_variables` and / or `strict_filters`
to `true` respectively. {% include docs_version_badge.html version="3.8.0" %}

Do note that while `error_mode` configures Liquid's parser, the `strict_variables`
and `strict_filters` options configure Liquid's renderer and are consequently,
mutually exclusive.

## Markdown Options

The various Markdown renderers supported by Jekyll sometimes have extra options
Expand Down
44 changes: 44 additions & 0 deletions features/rendering.feature
Expand Up @@ -30,6 +30,50 @@ Feature: Rendering
Then I should get a non-zero exit-status
And I should see "Liquid Exception: Liquid error \(.+/_includes/invalid\.html line 1\): wrong number of arguments (\(given 1, expected 2\)|\(1 for 2\)) included in index\.html" in the build output

Scenario: Rendering a default site containing a file with rogue Liquid constructs
Given I have a "index.html" page with title "Simple Test" that contains "{{ page.title | foobar }}\n\n{{ page.author }}"
When I run jekyll build
Then I should get a zero exit-status
And I should not see "Liquid Exception:" in the build output

Scenario: Rendering a custom site containing a file with a non-existent Liquid variable
Given I have a "index.html" file with content:
"""
---
title: Simple Test
---
{{ page.title }}
{{ page.author }}
"""
And I have a "_config.yml" file with content:
"""
liquid:
strict_variables: true
"""
When I run jekyll build
Then I should get a non-zero exit-status
And I should see "Liquid error \(line 3\): undefined variable author in index.html" in the build output

Scenario: Rendering a custom site containing a file with a non-existent Liquid filter
Given I have a "index.html" file with content:
"""
---
author: John Doe
---
{{ page.title }}
{{ page.author | foobar }}
"""
And I have a "_config.yml" file with content:
"""
liquid:
strict_filters: true
"""
When I run jekyll build
Then I should get a non-zero exit-status
And I should see "Liquid error \(line 3\): undefined filter foobar in index.html" in the build output

Scenario: Render Liquid and place in layout
Given I have a "index.html" page with layout "simple" that contains "Hi there, Jekyll {{ jekyll.environment }}!"
And I have a simple layout that contains "{{ content }}Ahoy, indeed!"
Expand Down
4 changes: 3 additions & 1 deletion lib/jekyll/configuration.rb
Expand Up @@ -61,7 +61,9 @@ class Configuration < Hash
"defaults" => [],

"liquid" => {
"error_mode" => "warn",
"error_mode" => "warn",
"strict_filters" => false,
"strict_variables" => false,
},

"rdiscount" => {
Expand Down
10 changes: 9 additions & 1 deletion lib/jekyll/renderer.rb
Expand Up @@ -68,8 +68,11 @@ def run
# rubocop: disable AbcSize
def render_document
info = {
:registers => { :site => site, :page => payload["page"] },
:registers => { :site => site, :page => payload["page"] },
:strict_filters => liquid_options["strict_filters"],
:strict_variables => liquid_options["strict_variables"],
}

output = document.content
if document.render_with_liquid?
Jekyll.logger.debug "Rendering Liquid:", document.relative_path
Expand Down Expand Up @@ -265,5 +268,10 @@ def output_exts
c.output_ext(document.extname)
end.compact
end

private
def liquid_options
@liquid_options ||= site.config["liquid"]
end
end
end

0 comments on commit 51bdea1

Please sign in to comment.