Grantlee
0.2.0
|
The Context class holds the context to render a template with. More...
#include <grantlee/context.h>
Public Types | |
enum | UrlType { AbsoluteUrls, RelativeUrls } |
Public Member Functions | |
Context () | |
Context (const QVariantHash &hash) | |
Context (const Context &other) | |
~Context () | |
QList< QPair< QString, QString > > | externalMedia () const |
void | insert (const QString &name, QObject *object) |
void | insert (const QString &name, const QVariant &variant) |
AbstractLocalizer::Ptr | localizer () const |
QVariant | lookup (const QString &str) const |
Context & | operator= (const Context &other) |
void | pop () |
void | push () |
QString | relativeMediaPath () const |
RenderContext * | renderContext () const |
void | setLocalizer (AbstractLocalizer::Ptr localizer) |
void | setRelativeMediaPath (const QString &relativePath) |
void | setUrlType (UrlType type) |
UrlType | urlType () const |
For application developers, using the Context class is a matter of inserting keys and values as appropriate for rendering a template using the insert method.
Template t = engine->newTemplate( "Name is {% name %} and age is {% age %}.", "some_template" ); Context c1; c1.insert( "name", "Tom" ); c1.insert( "age", 34 ); Context c2; c2.insert( "name", "Harry" ); c2.insert( "age", 43 ); t->render(c1); // Returns "Name is Tom and age is 43." t->render(c2); // Returns "Name is Harry and age is 34."
Note that one Template may be rendered multiple times with different contexts. Note also that any QVariant may be inserted into a Context object. Most commonly, QObjects will be used here.
For template tag developers, some other Context API is relevant.
It is possible to push and pop layers of context while a template is being rendered. This is useful if your template tag makes additional variables temporarily available in a part of a template. Template tags should only modify layers of context that they push themselves, and should pop any layers created before finishing its rendering step.
See for example the {% with %}
tag. In a template such as
Some content {% with person.name|toUpper as lowerName %} Name is {% lowerName %} {% endwith %}
In this case, lowerName is available in the context only between the {% with %}
and {% endwith %}
tags. The implementation of the {% with %}
tag render method is:
void WithNode::render( OutputStream *stream, Context *c ) { c->push(); // {% with m_filterExpression as m_name %} c->insert( m_name, m_filterExpression.resolve( c ) ); m_list.render( stream, c ); c->pop(); // The section of context defining m_name is removed. }
Note that a context may temporarily override a variable in a parent context. This is why it is important to push a new context when adding items to context and pop it when finished.
Some content {% with "foo" as var %} Var is {% var %} // Var is "foo" {% with "bar" as var %} Var is {% var %} // Var is "bar" {% endwith %} Var is {% var %} // Var is "foo" {% endwith %}
Grantlee::Context::Context | ( | ) |
Creates an empty context
Grantlee::Context::Context | ( | const QVariantHash & | hash | ) | [explicit] |
Sets every key in the hash as a property name with the variant as the value.
Grantlee::Context::Context | ( | const Context & | other | ) |
Copy Constructor
Grantlee::Context::~Context | ( | ) |
Destructor
QList<QPair<QString, QString> > Grantlee::Context::externalMedia | ( | ) | const |
Returns the external media encountered in the Template while rendering.
void Grantlee::Context::insert | ( | const QString & | name, |
QObject * | object | ||
) |
Insert the context object object
identified by name
into the Context.
void Grantlee::Context::insert | ( | const QString & | name, |
const QVariant & | variant | ||
) |
Insert the context object variant
identified by name
into the Context.
AbstractLocalizer::Ptr Grantlee::Context::localizer | ( | ) | const |
Returns the localizer currently in use.
QVariant Grantlee::Context::lookup | ( | const QString & | str | ) | const |
Returns the context object identified by the key str
void Grantlee::Context::pop | ( | ) |
Pops the context.
void Grantlee::Context::push | ( | ) |
Pushes a new context.
QString Grantlee::Context::relativeMediaPath | ( | ) | const |
The relative path to external media to be used in templates.
RenderContext* Grantlee::Context::renderContext | ( | ) | const |
Returns a modifiable RenderContext for the Node scopeNode
. This may be used to make Template rendering threadsafe so that render state does not need to be stored in the Node implementation itself.
void Grantlee::Context::setLocalizer | ( | AbstractLocalizer::Ptr | localizer | ) |
Sets the localizer to be used.
The Context takes ownerwhip of the localizer.
void Grantlee::Context::setRelativeMediaPath | ( | const QString & | relativePath | ) |
Sets the relative path to external media to be used in templates to relativePath
void Grantlee::Context::setUrlType | ( | UrlType | type | ) |
Sets the type of external media URL to be used in the template to type
.
UrlType Grantlee::Context::urlType | ( | ) | const |
The type of URL used in the template.