GithubHelp home page GithubHelp logo

Comments (1)

pix666 avatar pix666 commented on June 29, 2024

I do not quite understand the purpose of such a design. This looks like an alternative {% if ... %} ... {% else %} tag.
Perhaps this example will help you:

from jinja2 import nodes
from jinja2_simple_tags import BaseTemplateTag

class YetAnotherIfTag(BaseTemplateTag):
    tags = {"if2"}

    def render(self, value):
        # Some logic here...
        return bool(value)

    def output(self, parser, call_node, target, tag_name, lineno):
        body = parser.parse_statements(["name:else", "name:end%s" % tag_name])
        else_ = []
        token = next(parser.stream)
        if token.test("name:else"):
            else_ = parser.parse_statements(["name:end%s" % tag_name], drop_needle=True)

        if_node = nodes.If(call_node, body, [], else_).set_lineno(lineno)
        if target:
            target_node = nodes.Name(target, "store", lineno=lineno)
            return nodes.AssignBlock(target_node, None, [if_node], lineno=lineno)
        return if_node

Usage:

{% if2 26 % 2 == 0 %}
  <p>Number is even</p>
{% else %}
  <p>Number is odd</p>
{% endif2 %}


{% if2 True %}
  <p>Definitely true</p>
{% endif2 %}


<!-- It's funny, but assignment will work too -->
{% if2 True as result %}
  <p>Hello, world!</p>
{% endif2 %}

{{ result }}

from jinja2-simple-tags.

Related Issues (7)

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.