Skip to content

Commit

Permalink
feat: MkJinjaTemplate now returns the rendered MkNodes as its children
Browse files Browse the repository at this point in the history
This way also the requirements from the nodes used in jinja blocks get evaluated.
  • Loading branch information
phil65 committed Sep 28, 2023
1 parent 9b4630b commit 1760603
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
8 changes: 5 additions & 3 deletions mknodes/resources/mkclasspage.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ An MkClassPage is an MkPage subclass used to display information about
graph are shown. The template can be adjusted manually in case a different layout
is preferred.

{{ example_class | MkClassPage(inclusion_level=False) | MkReprRawRendered }}
class_page = mk.MkClassPage(mk.MkCode, inclusion_level=False)
page += mk.MkReprRawRendered(class_page)

This is an example for an MkClassPage with the default layout:


{{ example_class | MkClassPage(inclusion_level=False) }}
6 changes: 5 additions & 1 deletion mknodes/resources/mkmodulepage.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ information. By default, the Docstrings and tables showing the contained classes
submodules. The template can be adjusted manually in case a different layout is
preferred.

{{ example_module | MkModulePage(inclusion_level=False) | MkReprRawRendered }}

This is an example for an MkModulePage with the default layout:


{{ example_module | MkModulePage(inclusion_level=False) }}
20 changes: 16 additions & 4 deletions mknodes/templatenodes/mkjinjatemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

from typing import Any

from mknodes.basenodes import mknode
from mknodes.basenodes import mkcontainer
from mknodes.utils import log, reprhelpers


logger = log.get_logger(__name__)


class MkJinjaTemplate(mknode.MkNode):
class MkJinjaTemplate(mkcontainer.MkContainer):
"""Node representing a jinja template."""

ICON = "simple/jinja"
Expand Down Expand Up @@ -41,6 +41,18 @@ def __repr__(self):
_filter_empty=True,
)

@property
def items(self):
self.env.rendered_nodes = []
self.env.render_template(self.template, variables=self.variables)
for i in self.env.rendered_nodes:
i.parent = self
return self.env.rendered_nodes

@items.setter
def items(self, val):
pass

@staticmethod
def create_example_page(page):
import mknodes
Expand All @@ -53,5 +65,5 @@ def _to_markdown(self) -> str:


if __name__ == "__main__":
node = MkJinjaTemplate.with_default_context("cli_index.jinja")
print(node)
node = MkJinjaTemplate("nodes_index.jinja")
print(node.get_requirements())

0 comments on commit 1760603

Please sign in to comment.