Code Monkey home page Code Monkey logo

django-templatetag-sugar's Introduction

django-templatetag-sugar

A library to make writing templatetags in Django sweet.

Here's an example of using:

from django import template

from templatetag_sugar.register import tag
from templatetag_sugar.parser import Name, Variable, Constant, Optional, Model

register = template.Library()

@tag(register, [Constant("for"), Variable(), Optional([Constant("as"), Name()])]):
def example_tag(context, val, asvar=None):
    if asvar:
        context[asvar] = val
        return ""
    else:
        return val

As you can see it makes it super simple to define the syntax for a tag.

django-templatetag-sugar's People

Contributors

alex avatar bmihelac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-templatetag-sugar's Issues

django models loading import cache

I'm trying to track down a caching error on my site and found this warning from the templatetag-sugar app.

...\lib\site-packages\templatetag_sugar\parser.py:4: RemovedInDjango19Warning: The utilities in django.db.models.loading are deprecated in favor of the new application loading system.
from django.db.models.loading import cache

@alex

I saw this potential fix
adamchainz/django-cors-headers#64

More documentation

Hi, this is a great tool.

Could you please create a bit more documentation on each [whatever hyphenym there is for Variable, Optional, and so on]? I'm pretty sure the small example in the README does not reveal everything this sugar can do.

Allow some kind of `*args, **kwargs`

To allow passing arbitrary parameters after the fixed ones, declared the old way. I don't have clearly how it may look like, but recently I needed a tag, that retrieves a list of results, with the first parameter as the limit of results (fixed), and allow to pass an arbitrary number of strings and variables after that, that would be the terms to be used in the search.

Ambiguous errors on improper tag syntax in template

Syntax checking could be improved during parsing since it is possible to get some ambiguous errors if a tag's syntax is not specified properly in a template. For example, taking the fairly canonical context-populating tag:

@tag(register,
 [Constant("for"), Variable(), Optional([Constant("as"), Name()])])
    def get_comments_for_article(context, article_id, varname=None):
        #...

If you attempt to reference the tag in a template via:

{% get_comments_for_article %}

the error is severe enough that you don't even get the Django 500 page.

Multiple Optional objects

Having more then one Optional Object fails parsing the parameters.

@tag(register, [Constant('as'), Name(), Optional([Constant('for'), Variable()]), Optional([Constant('count'), Variable()])])
def get_tagcloud(context, asvar, forvar=None, countvar=None):

I think the problem lies inside the call method of the Parser Class, which assumes that all parameters are present in the order they are defined.

Setup fails

this is introduced in ff67a4e

IOError: [Errno 2] No such file or directory: 'README'

Support for boolean tags

It would be nice to have an easier way to create boolean tags.

Template:

{% ifequals thing1 thing2 %}
    All Things are created equal
{% else %}
    Things are not equal
{% endifequals %}

Tag:

@boolean_tag(register, [Variable(), Variable()])
def ifequals(context, thing1, thing2):
    return thing1 == thing2

I took a stab at it in my fork.
seanbrant/django-templatetag-sugar@ea353e5e50152fe5a353e5aa7fff1fdbb76cf2e0

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.