GithubHelp home page GithubHelp logo

Jinja2 support about pyyaml-include HOT 3 CLOSED

tanbro avatar tanbro commented on August 10, 2024
Jinja2 support

from pyyaml-include.

Comments (3)

tanbro avatar tanbro commented on August 10, 2024

I have another project jinjyaml, it provides pyYAML parser and loader for Jinja2 template express/object

But i haven't tried to use them together.

from pyyaml-include.

elvis2 avatar elvis2 commented on August 10, 2024

Thanks, I'll keep an eye on your new project.

To make this more clear. I was using the pyyaml-include first, then the jinja2 render last. I found out the yaml dump from the pyyaml include module was changing the quotes to where the jinja2 enginer render couldn't process it.

Instead, I force the jinja2 render first, for all templates that will be used by pyyaml include, then run that include module last. It works as expected now.

Thanks

from pyyaml-include.

tanbro avatar tanbro commented on August 10, 2024

The project is based on pyYAML Constructor, which is a part of it's YAML parser.

When including another YAML file, it (the file) will be parsed into YAML objects(Scalar, Sequence, or Mapping object), then put to the place where the "!include" tag is.

So, the Jinja2 template expressions won't be recognized during the including process.

for example, a file like:

{% for employee in department.employees %}
- name: employee.name
  age: employee.age
{% endfor %}

can not be included, cause it's legal for Jinja, but illegal for YAML. Unless we render it before including.

For a "render after include" usage, i think jinjyaml is helpful.

It provides Jinja2 template constructor/tag.
We can rewrite Jinja2 template in above example to a YAML with a special tag:

!jinja2 |
{% for employee in department.employees %}
- name: employee.name
  age .employee.age
{% endfor %}

Such a fragment will be parsed to a jinjyaml.JinjyamlObject object (note that we shall add jinjyaml constructor to FullLoader first). Thus we can render it later.

from pyyaml-include.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.