terminal-labs / lektor-jinja-content

Render Lektor content fields with Jinja2.
Other
11 stars 5 forks source link

Model labels causes errors in field rendering #19

Open leogzyl opened 1 year ago

leogzyl commented 1 year ago

Using the basic blog from lektor quickstart, I get strange behavior with some fields.

Take the start page, page.html for example:

{% extends "layout.html" %}
{% block title %}{{ this.title }}{% endblock %}
{% block body %}
  <h2>{{ this.title }}</h2>
  {{ this.body }}
{% endblock %} 

When running lektor serve...

This markdown:

title: Welcome to myblog! {{"In JINJA!"}} 
---
body:
{{"In JINJA!"}}
This is a basic demo website that shows how to use Lektor for a basic
website with some pages and a blog.

Renders like this: Screenshot from 2023-01-14 17-07-02

If I make a trivial whitespace change to page.html to force the watcher to do a refresh, everything works: Screenshot from 2023-01-14 17-10-49

Here's the Page model:

[model]
name = Page
label = {{ this.title }}

[fields.title]
label = Title
type = string

[fields.body]
label = Body
type = markdown

Removing label in the model file makes everything work.

nixjdm commented 1 year ago

I've reproduced your steps down get the error to show, and that a whitespace change to the template triggers a successful build. Can you elaborate on what you mean by

Removing label in the model file makes everything work.

Which label? Thank you for the report.

I'm actually experiencing something similar I think in another site.

I'm not sure what's causing this atm, or if it's always been present or somehow came about because of a change in behavior of a dependency.

nixjdm commented 1 year ago

It's worth noting that your report is a failure of jinja to render a string type, whereas mine in another project is a failure to render a markdown type, but you have a markdown type succeeding on the first pass.

This feels like something non-deterministic, yet once a page is built, it's kind of stuck that way until we trick the page into being built again.

nixjdm commented 1 year ago

This sort of seems deterministic. For me, it fails on my other site every time I lektor clean --yes; lektor b, but succeeds every time I lektor clean --yes; lektor s. But in the quickstart project I seem to have the opposite behavior, where building first is the successful one, but going straight to server produces the failing render.

leogzyl commented 1 year ago

I've reproduced your steps down get the error to show, and that a whitespace change to the template triggers a successful build. Can you elaborate on what you mean by

Removing label in the model file makes everything work.

Which label? Thank you for the report.

I'm actually experiencing something similar I think in another site.

I'm not sure what's causing this atm, or if it's always been present or somehow came about because of a change in behavior of a dependency.

I mean the "label" line on the Page model file (line 3 in the last code snippet in my original comment). To be completely honest, I'm not 100% sure that causes the issue... or simply triggers a rebuild somehow.

I also played around with cleaning and rebuilding and managed to get it to work at times, but never got to the bottom of it, hence the vague bug report.

Anyway, glad to contribute, and thanks for looking into it!

nixjdm commented 1 year ago

I have a fix for this. I'll merge it in and publish a fixed version in a day or two.

transplier commented 8 months ago

I've set a breakpoint in JinjaContentPlugin##render_as_jinja and I see my test field being rendered properly (in my case, the default author field in the default Blog sample site). At the end of on_process_template_context, the context object is correctly populated with a rendered version of all the fields. However, despite this, Lektor goes on to use the original field values, as if the context object were never modified.

Perhaps Lektor itself is using a different copy of this object? I can debug further but I'd like to hear what @nixjdm found, before expending a bunch of effort. Can I help get the patch over the finish line?