Browse Source

Issue #86: Bug Fix: Ensure that context providers can exist without context data

main
Lakshmi Vyasarajan 13 years ago
parent
commit
62da52adc6
2 changed files with 54 additions and 4 deletions
  1. +13
    -4
      hyde/model.py
  2. +41
    -0
      hyde/tests/test_generate.py

+ 13
- 4
hyde/model.py View File

@@ -95,13 +95,22 @@ class Context(object):
context = {}
try:
context.update(ctx.data.__dict__)
for provider_name, resource_name in ctx.providers.__dict__.items():
res = File(Folder(sitepath).child(resource_name))
if res.exists:
context[provider_name] = Expando(yaml.load(res.read_all()))
except AttributeError:
# No context data found
pass

providers = {}
try:
providers.update(ctx.providers.__dict__)
except AttributeError:
# No providers found
pass

for provider_name, resource_name in providers.items():
res = File(Folder(sitepath).child(resource_name))
if res.exists:
context[provider_name] = Expando(yaml.load(res.read_all()))

return context

class Dependents(IterableUserDict):


+ 41
- 0
hyde/tests/test_generate.py View File

@@ -162,6 +162,47 @@ main:
assert "articles" in out
assert "projects" in out

def test_context_providers_no_data(self):
site = Site(TEST_SITE, Config(TEST_SITE, config_dict={
"context": {
"providers": {
"nav": "nav.yaml"
}
}
}))
nav = """
main:
- home
- articles
- projects
"""
text = """
{% extends "base.html" %}

{% block main %}
{{nav}}
{% for item in nav.main %}
{{item}}
{% endfor %}
abc = {{ abc }}
Hi!

I am a test template to make sure jinja2 generation works well with hyde.
{{resource.name}}
{% endblock %}
"""
File(TEST_SITE.child('nav.yaml')).write(nav)
site.load()
resource = site.content.resource_from_path(TEST_SITE.child('content/about.html'))
gen = Generator(site)
resource.source_file.write(text)
gen.generate_all()
target = File(site.config.deploy_root_path.child(resource.name))
out = target.read_all()
assert "home" in out
assert "articles" in out
assert "projects" in out

def test_context_providers_equivalence(self):
import yaml
events = """


Loading…
Cancel
Save