Grantlee: Differences between Django and Grantlee

Grantlee 5.1.0

Differences between Django and Grantlee

There are several notable differences between Django and Grantlee, which arise mostly due to different implementation languages and features of the Qt framework.

Everything in python is a first class object, which means that django filter functions and tag functions can have properties and attributes. In Grantlee, it is necessary to use classes to obtain the same effect, where Django uses only functions for filters. Additionally, C++ classes need to share an interface if they are to be treated the same way. This is not necessary in Django because python uses dynamic typing, and all methods behave similarly to virtual functions. Grantlee uses the Abstract Factory pattern to achieve the same affect for tags, and a simple interface for filters.

Django tag libraries are loaded through normal python modules. This means that all libraries can be loaded dynamically. Grantlee achieves the same effect by using the QtPlugin system to load additional libraries dynamically.

In Django, any python object can be inserted into a Context. Then, methods which can change a class must have the 'alters_data' attribute, so that rendering a template can not alter the objects being rendered. Objects in Grantlee are only introspectable to a limited amount determined by Q_PROPERTIES defined in wrapper classes. Therefore, the programmer can decide which properties to make available to the template system by wrapping them or not. It is encouraged, but not enforced to make sure scriptable wrapper methods are const.

Python dictionary objects can be processed in templates. In Grantlee, QVariantHash is supported, which has the same effect. QVariantHash keys must be strings, whereas python dictionary keys may be any pickle-able object.

Django uses PHP datetime string format for the `{% now %}` tag, whereas Grantlee uses QDateTime format.

The Django autoescape system is based on marking a string object as safe. In Qt, this is not directly possible, so a wrapper class, Grantlee::SafeString is provided with several convenient operator overloads. This has (hopefully) minimal impact on plugin writers.

Django performs string escaping on the assumption that the output string is HTML. In Grantlee it is possible to implement escaping for other markups and text outputs by reimplementing OutputStream::escape.

The Django dictsort filter only works on a list of dictionary-like-objects. In Grantlee, is is possible to sort a list of any kinds of objects by any property of the objects. For example a list of QObjects can be sorted by a certain property, a list of lists can be sorted by size etc.

The Django cache system makes a lot of sense where templates are rendered in a fire-and-forget manner for a stateless protocol. It is not implemented in Grantlee because we're generally not rendering similar templates from scratch multiple times and the templates can keep state for multiple uses.