lektor / lektor-tags

For each tag on your site, build a list of pages with that tag. This can be used for standard tag-based blog navigation.
MIT License
27 stars 6 forks source link

Can’t make links to tags in templates in some places #8

Open chris-morgan opened 8 years ago

chris-morgan commented 8 years ago

Basically, you can’t reverse a URL like /blog@tag/rust in /blog because it hasn’t processed the blog-posts that actually use the “rust” tag yet.

Workaround is to write {{ ('/blog/tag/' ~ tag ~ '/')|url }} instead of {{ ('/blog@tag/' ~ tag)|url }}, so it’s not urgent.

Oh, and the “KeyError: u'/blog/@tag/boring/'” message you get in this case is rather unhelpful. Producing a more helpful error message (something along the lines of “no pages that have been loaded use such-and-such a tag”, I’m guessing) would be good.


configs/tags.ini:

parent = /blog
template = blog-tag.html

templates/blog.html contains a curated list of tags people might want to refer to:

<p><strong>Filter by tags:</strong>{% for tag in ['rust', 'web', 'python', 'fun'] %} {{ render_tag(tag) }}{% endfor %}

And the render_tag macro from macros/blog.html (slightly simplified):

{% macro render_tag(tag) %}
    <a class=tag href={{ ('/blog@tag/' ~ tag)|url }}>{{ bag('tags', tag, 'name') }}</a>
{% endmacro %}

lektor build output:

U blog/index.html
E blog/index.html (KeyError: u'/blog/@tag/rust/')
  Traceback (most recent call last):
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\builder.py", line 797, in update
      yield ctx
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\builder.py", line 1038, in build_artifact
      build_func(artifact)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\build_programs.py", line 183, in build_artifact
      self.source['_template'], this=self.source)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\builder.py", line 652, in render_template_into
      this=this, **extra)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\environment.py", line 517, in render_template
      return self.jinja_env.get_or_select_template(name).render(ctx)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\jinja2\environment.py", line 989, in render
      return self.environment.handle_exception(exc_info, True)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\jinja2\environment.py", line 754, in handle_exception
      reraise(exc_type, exc_value, tb)
    File "C:\path\to\site\templates\blog.html", line 2, in top-level template code
      {% from "macros/blog.html" import render_blog_post_excerpt, render_tag %}
    File "C:\path\to\site\templates\layout.html", line 31, in top-level template code
      {% block main %}{% endblock %}
    File "C:\path\to\site\templates\blog.html", line 16, in block "main"
      <p><strong>Filter by tags:</strong>{% for tag in ['rust', 'web', 'python', 'fun'] %} {{ render_tag(tag) }}{% endfor %}
    File "C:\path\to\site\templates\macros\blog.html", line 22, in template
      <a class=tag href={{ ('/blog@tag/' ~ tag)|url }}>{{ bag('tags', tag, 'name') }}</a>
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\environment.py", line 423, in <lambda>
      lambda ctx, *a, **kw: url_to(*a, **kw)),
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\context.py", line 17, in url_to
      return ctx.url_to(*args, **kwargs)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\context.py", line 161, in url_to
      rv = self.source.url_to(path, alt=alt, absolute=True)
    File "c:\users\me\appdata\local\lektor-cli\lib\lib\site-packages\lektor\sourceobj.py", line 118, in url_to
      path = source.url_path
    File "C:\Users\me\AppData\Roaming\Lektor\Cache\packages\114189be8b6f71dae18d14ee5e7fb06b\lektor_tags.py", line 37, in url_path
      return TagsPlugin.reverse_url_map[self.path]
  KeyError: u'/blog/@tag/rust/'
U blog/feed.xml
Error: Build failed with 1 failure.
Finished build in 2.64 sec
Started prune
  Tree: C:\path\to\site
  Output path: C:\Users\me\AppData\Roaming\Lektor\Cache\builds\379d56f7680c1ad15d29ecfd8efb424c
Finished prune in 0.11 sec