Reference¶
classytags.arguments
¶
This module contains standard argument types.
A basic single value argument with name as it’s name.
default is used if required is False and this argument is not given.
If resolve is
False
, the argument will not try to resolve it’s contents against the context. This is especially useful for ‘as varname’ arguments. Note that quotation marks around the argument will be removed if there are any.The class to be used to wrap the value in. Defaults to
classytags.values.StringValue.
Returns the default value for this argument
Parses a single token into kwargs. Should return
True
if it consumed this token orFalse
if it didn’t.
Parses a single token using parser into an object which is can be resolved against a context. Usually this is a template variable, a filter expression or a
classytags.utils.TemplateConstant
.
-
class
classytags.arguments.
Argument
(name[, default=None][, required=True], [resolve=True]) Same as
classytags.arguments.Argument
but withclassytags.values.StrictStringValue
asvalue_class
.
An argument that allows
key=value
notation.defaultkey is used as key if no key is given or the default value should be used.
splitter is used to split the key value pair.
Class to use to wrap the key value pair in. Defaults to
classytags.values.DictValue
.
Same as
classytags.arguments.Argument
but withclassytags.values.IntegerValue
asvalue_class
.
An argument which validates it’s input against predefined choices.
An argument which accepts a variable amount of values. The maximum amount of accepted values can be controlled with the max_values argument which defaults to
None
, meaning there is no maximum amount of values.default is an empty list if required is
False
.resolve has the same effects as in
classytags.arguments.Argument
however applies to all values of this argument.The default value for
value_class
is$classytags.values.ListValue
.Class to be used to build the sequence. Defaults to
classytags.utils.ResolvableList
.
Similar to
classytags.arguments.KeywordArgument
but allows multiple key value pairs to be given. The will be merged into one dictionary.Arguments are the same as for
classytags.arguments.KeywordArgument
andclassytags.arguments.MultiValueArgument
, except that default_key is not accepted and default should be a dictionary orNone
.
A boolean flag. Either true_values or false_values must be provided.
If default is not given, this argument is required.
true_values and false_values must be either a list or a tuple of strings. If both true_values and false_values are given, any value not in those sequences will raise a
classytag.exceptions.InvalidFlag
exception.case_sensitive defaults to
False
and controls whether the values are matched case sensitive or not.
classytags.blocks
¶
This module contains classes for Advanced Block Definition.
A block definition with the given alias and a sequence of names. The members of the names sequence must either be strings,
classytags.blocks.VariableBlockName
instances or other objects implementing at least acollect()
method compatible with the one ofclassytags.blocks.VariableBlockName
.The alias for this definition to be used in the tag’s kwargs.
Sequence of strings or block name definitions.
Validates this definition against an instance of
classytags.core.Options
by calling thevalidate()
on all it’snames
if such a method is available.
Returns a sequence of strings to be used in the
parse_until
statement. This is a sequence of strings that this block accepts to be handled. The parser argument is an instance ofclassytags.parser.Parser
.
A block name definition to be used in
classytags.blocks.BlockDefinition
to implement block names that depend on the (unresolved) value of an argument. The template argument to this class should be a string with thevalue
string substitution placeholder. For example:'end_my_block %(value)s'
. The argname argument is the name of the argument from which the value should be extracted.Validates that the given argname is actually available on the tag.
Returns the template substitued with the value extracted from the tag.
classytags.core
¶
This module contains the core objects to create tags.
Holds the options of a tag. options should be a sequence of
classytags.arguments.Argument
subclasses or strings (for breakpoints). You can give they keyword argument blocks to define a list of blocks to parse until. You can specify a custom argument parser by providing the keyword argument parser_class.A list of all argument names in this tag options. Used by
classytags.blocks.VariableBlockName
to validate it’s definition.
Returns
classytags.parser.Parser
or a subclass of it.
An internal method to bootstrap the arguments. Returns an instance of
classytags.utils.StructuredOptions
.
-
parse(parser, token):
An internal method to parse the template tag. Returns a tuple
(arguments, blocks)
.
The metaclass of
classytags.core.Tag
which ensures the tag has a name attribute by setting one based on the classes name if none is provided.
The
Tag
class is nothing other than a subclass ofdjango.template.Node
which handles argument parsing in it’s__init__()
method rather than an external function. In a normal use case you should only overridename
,options
andrender_tag()
.Note
When registering your template tag, register the class object, not an instance of it.
The name of this tag (for use in templates). This attribute is optional and if not provided, the un-camelcase class name will be used instead. So MyTag becomes my_tag.
An instance of
classytags.core.Options
which holds the options of this tag.
-
__init__(parser, token):
Warning
This is an internal method. It is only documented here for those who would like to extend django-classy-tags.
This is where the arguments to this tag get parsed. It’s the equivalent to a compile function in Django’s standard templating system. This method does nothing else but assing the
kwargs
andblocks
attributes to the output ofoptions.parse()
with the given parser and token.
Warning
This is an internal method. It is only documented here for those who would like to extend django-classy-tags.
This method resolves the arguments to this tag against the context and then calls
render_tag()
with the context and those arguments and returns the return value of that method.
The method used to render this tag for a given context. kwargs is a dictionary of the (already resolved) options of this tag as well as the blocks (as nodelists) this tag parses until if any are given. This method should return a string.
classytags.exceptions
¶
This module contains the custom exceptions used by django-classy-tags.
The base class for all custom excpetions, should never be raised directly.
Gets raised if an option of a tag is required but not provided.
Gets raised if a given value for a flag option is neither in true_values nor false_values.
Gets raised if a breakpoint was expected, but another argument was found.
Gets raised if too many arguments are provided for a tag.
classytags.helpers
¶
This modules contains helper classes to make building template tags even easier.
A helper tag base class to build ‘as varname’ tags. Note that the option class still has to contain the ‘as varname’ information. This tag will use the last argument in the options class to set the value into the context.
This class implements the method
classytags.helpers.AsTag.get_value()
which gets the context and all arguments except for the varname argument as arguments. It should always return the value this tag comes up with, the class then takes care of either putting the value into the context or returns it if the varname argument is not provided.Note
You should not override the
render_tag()
method of this class.Should return the value of this tag. The context setting is done in the
classytags.core.Tag.render_tag()
method of this class.
A helper class for writing inclusion tags (template tags which render a template).
Note
You should not override the
render_tag()
method of this class.The template to use if
get_template()
is not overridden.
By default, this is
True
. If it’s set toFalse
the context will not be pushed and popped around the rendering of the included template, possibly resulting in context pollution.
This method should return a template (path) for this context and arguments. By default returns the value of
template
.
Should return the context (as a dictionary or an instance of
django.template.Context
or a subclass of it) to use to render the template. By default returns an empty dictionary.
classytags.parser
¶
The default argument parser lies here.
The default argument parser class. It get’s initialized with an instance of
classytags.utils.StructuredOptions
.The
classytags.utils.StructuredOptions
instance given when the parser was instantiated.
The (template) parser used to parse this tag.
The split tokens.
Name of this tag.
The data extracted from the bits.
A dictionary holding the block nodelists.
The arguments in the current breakpoint scope.
The current argument if any.
Remaining bits. Used for more helpful exception messages.
Parses a token stream. This is called when your template tag is parsed.
Handle the current bit (token).
The current bit is the next breakpoint. Make sure the current scope can be finished successfully and shift to the next one.
The current bit is a future breakpoint, try to close all breakpoint scopes before that breakpoint and shift to it.
The current bit is an argument. Handle it and contribute to
kwargs
.
Parses the blocks this tag wants to parse until if any are provided.
After all bits have been parsed, finish all remaining breakpoint scopes.
A helper method to check if there’s any required arguments left in the current breakpoint scope. Raises a
classytags.exceptions.ArgumentRequiredError
if one is found and contributes all optional arguments tokwargs
.
classytags.utils
¶
Utility classes and methods for django-classy-tags.
A pseudo type.
A constant pseudo template variable which always returns it’s initial value when resolved.
Used by the
classytags.blocks.VariableBlockName
to generate it’s final name.
A helper class to organize options.
The arguments in this options.
A copy of the breakpoints in this options
A copy of the list of tuples (blockname, alias) of blocks of this tag.
The current breakpoint.
The next breakpoint (if there is any).
Shift to the next breakpoint and update
current_breakpoint
andnext_breakpoint
.
Returns a copy of the arguments in the current breakpoint scope.
A subclass of list which resolves all it’s items against a context when it’s resolve method gets called.
Turns ‘CamelCase’ into ‘camel_case’.
classytags.values
¶
A dictionary holding error messages which can be caused by this value class. Defaults to an empty dictionary.
The value to use when the validation of a input value fails in non-debug mode. Defaults to an empty string.
The variable wrapped by this value instance.
Validates and/or cleans a resolved value. This method should always return something. If validation fails, the
error()
helper method should be used to properly handle debug modes.
Handles an error in category caused by value. In debug mode this will cause a
django.template.TemplateSyntaxError
to be raised, otherwise a TemplateSyntaxWarning is called andvalue_on_error
is returned. The message to be used for both the exception and the warning will be constructed by the message inerrors
if category is in it. The value can be used as a named string formatting parameter.
Same as
StringValue
but enforces that the value passed to it is a string (instance ofbasestring
).
Subclass of
StringValue
.Tries to convert the value to an integer.
Subclass of
StringValue
andlist
.Appends the initial value to itself in initialization.
Resolves all items in itself against context and calls
clean()
with the list of resolved values.
Subclass of
StringValue
anddict
.Resolves all values against context and calls
clean()
with the resolved dictionary.