stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 223 forks source link

Caching rendered blocks content to reuse them in further calls #182

Closed ilyapuchka closed 2 years ago

ilyapuchka commented 6 years ago

Resolves #158 This PR implements caching of rendered blocks context in the top context so that it can be later rendered again using variable expression.

Example:

<title>{% block title %}Hello World!{% endblock %}</title>
<h1>{{ block.title }}</h1>

Result:

<title>Hello World!</title>
<h1>Hello World!</h1>

In case of inheritance cached content is updated each time child block is rendered, so when block repeats in the base template its content will be the same as content of the first block invocation, containing all its children contents.

Example:

base.html

<title>{% block title %}Hello World!{% endblock %}</title>
<h1>{{ block.title }}</h1>
child.html

{% extends base.html %}
<title>{% block title %}{{ block.super}} Foo Bar {% endblock %}</title>

Result (when rendering child.html):

<title>Hello World! Foo Bar</title>
<h1>Hello World! Foo Bar</h1>
djbe commented 6 years ago

Should we still consider this, as we're planning on merging filters such as set from StencilSwiftKit?

ilyapuchka commented 6 years ago

@djbe I think it's a bit different than set, though they may be used to achieve similar results

ilyapuchka commented 5 years ago

@kylef @djbe @yonaskolb can we move on with that? @djbe speaking of differences with set, blocks are rendered when they are defined and they are designed primarily for inheritance.

djbe commented 5 years ago

Side Note: set is also rendered when it's defined.

I've just read through the related issue #158, where you mention what Jinja does. Seeing as Stencil tries to mirror Jinja where possible, shouldn't we do the same? I.e. have the block title be cached under self.title?

ilyapuchka commented 5 years ago

@djbe we can do self.title but I think block.title is more helpful to recognize what this variable stands for

djbe commented 5 years ago

I agree that block.title may be clearer, but we're basing ourselves of Jinja, so I think we should mirror that if possible.

@kylef thoughts? You have better knowledge about this.

ilyapuchka commented 5 years ago

@djbe I don't think we should prefer mirroring Jinja syntax to better API, where block.name is better and we seem to agree on that. We already have quite a few points of divergence with Jinja and it's just not a goal to be canonical Swift implementation of Jinja language 🤷‍♂️

ilyapuchka commented 4 years ago

Is this something we want to have or it can be closed?

SwiftGen-Eve commented 2 years ago

Hey 👋 I'm Eve, the friendly bot watching over Stencil 🤖

Thanks a lot for your contribution!


Seems like everything is in order 👍 You did a good job here! 🤝

Generated by :no_entry_sign: Danger