Skip to content

Modifying global variables #128

Answered by fralau
maxhenze asked this question in Q&A
Feb 18, 2022 · 5 comments · 2 replies
Discussion options

You must be logged in to vote

The counter initial value is of course, read from the YAML file and is used to initialize counter variable, but it is not "pushed" back to it.

I have a solution that works, with a minimal example. The trick is to reset the counter for every page (or when you want). Since the building of the pages is sequential (not parallel), page by page, this is going to work:

def define_env(env):
    """
    This is the hook for defining variables, macros and filters
    """
    counter = 0

    @env.macro
    def set_counter(value:int=0):
        nonlocal counter
        counter = value
        return ''

    @env.macro
    def increment():
        nonlocal counter
        counter += 1
        return c…

Replies: 5 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@maxhenze
Comment options

Comment options

You must be logged in to vote
1 reply
@maxhenze
Comment options

Answer selected by maxhenze
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
useful tip A how-to, good to know
2 participants
Converted from issue

This discussion was converted from issue #127 on February 18, 2022 16:00.