Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jul 28, 2022

Conversation

ilyapuchka
Copy link
Collaborator

@ilyapuchka ilyapuchka commented Dec 30, 2017

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
Copy link
Contributor

djbe commented Jul 11, 2018

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

@ilyapuchka ilyapuchka requested a review from djbe August 18, 2018 20:23
@ilyapuchka
Copy link
Collaborator Author

ilyapuchka commented Aug 18, 2018

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

@ilyapuchka
Copy link
Collaborator Author

@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
Copy link
Contributor

djbe commented Sep 11, 2018

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
Copy link
Collaborator Author

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

@djbe
Copy link
Contributor

djbe commented Sep 21, 2018

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
Copy link
Collaborator Author

ilyapuchka commented Sep 22, 2018

@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
Copy link
Collaborator Author

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

@kylef kylef removed their request for review December 17, 2019 11:20
@djbe djbe added this to the 0.15.0 milestone Jul 28, 2022
@SwiftGen-Eve
Copy link

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 🚫 Danger

Copy link
Contributor

@djbe djbe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I've made the cacheBlock function public in case anyone with custom blocks wants to do the same.

@djbe djbe merged commit 248d664 into master Jul 28, 2022
@djbe djbe deleted the blocks-cache branch July 28, 2022 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using blocks more than once?
3 participants