Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
schilder2000
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
schilder
schilder2000
Commits
0d8546d6
Commit
0d8546d6
authored
9 months ago
by
Thomas Schneider
Browse files
Options
Downloads
Patches
Plain Diff
helpers: Add get_template_attribute()
parent
783baf5a
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
schilder2000/helpers.py
+44
-1
44 additions, 1 deletion
schilder2000/helpers.py
with
44 additions
and
1 deletion
schilder2000/helpers.py
+
44
−
1
View file @
0d8546d6
import
typing
as
t
from
flask
import
Flask
as
_Flask
,
Blueprint
as
FlaskBlueprint
,
render_template
from
flask
import
(
Flask
as
_Flask
,
Blueprint
as
FlaskBlueprint
,
current_app
,
render_template
,
)
from
jinja2
import
BaseLoader
,
ChoiceLoader
,
PrefixLoader
,
Template
...
...
@@ -43,3 +48,41 @@ class Flask(_Flask):
self
.
jinja_env
.
loader
.
loaders
[
0
].
mapping
[
blueprint
.
name
]
=
(
blueprint
.
jinja_loader
)
_sentinel
=
object
()
def
get_template_attribute
(
template_name
:
str
,
attribute
:
str
,
default
:
t
.
Any
=
_sentinel
,
vars
:
t
.
Dict
[
str
,
t
.
Any
]
|
None
=
None
,
shared
:
bool
=
False
,
locals
:
t
.
Mapping
[
str
,
t
.
Any
]
|
None
=
None
,
)
->
t
.
Any
:
"""
Loads a macro (or variable) a template exports. This can be used to
invoke a macro from within Python code. If you for example have a
template named :file:`_cider.html` with the following contents:
.. sourcecode:: html+jinja
{% macro hello(name) %}Hello {{ name }}!{% endmacro %}
You can access this from Python code like this::
hello = get_template_attribute(
'
_cider.html
'
,
'
hello
'
)
return hello(
'
World
'
)
.. versionadded:: 0.2
:param template_name: the name of the template
:param attribute: the name of the variable of macro to access
"""
mod
=
current_app
.
jinja_env
.
get_template
(
template_name
).
make_module
(
vars
,
shared
,
locals
)
if
default
is
_sentinel
:
return
getattr
(
mod
,
attribute
)
else
:
return
getattr
(
mod
,
attribute
,
default
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment