KTextTemplate::Context

Search for usage in LXR

KTextTemplate::Context Class Reference

#include <KTextTemplate/Context>

Public Types

enum  UrlType { AbsoluteUrls , RelativeUrls }
 

Public Member Functions

 Context ()
 
 Context (const Context &other)
 
 Context (const QVariantHash &hash)
 
 ~Context ()
 
QList< std::pair< QString, QString > > externalMedia () const
 
void insert (const QString &name, const QVariant &variant)
 
void insert (const QString &name, QObject *object)
 
QSharedPointer< AbstractLocalizerlocalizer () const
 
QVariant lookup (const QString &str) const
 
Contextoperator= (const Context &other)
 
void pop ()
 
void push ()
 
QString relativeMediaPath () const
 
RenderContextrenderContext () const
 
void setLocalizer (QSharedPointer< AbstractLocalizer > localizer)
 
void setRelativeMediaPath (const QString &relativePath)
 
void setUrlType (UrlType type)
 
UrlType urlType () const
 

Detailed Description

The Context class holds the context to render a Template with.

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.

auto t = engine->newTemplate(
"Name is {% name %} and age is {% age %}.", "some_template" );
c1.insert( "name", "Tom" );
c1.insert( "age", 34 );
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."
The Context class holds the context to render a Template with.
Definition context.h:107
void insert(const QString &name, QObject *object)
Insert the context object object identified by name into the Context.
Definition context.cpp:140

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.

See also
Custom objects

Context Stack.

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 ) const
{
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.
}
void pop()
Pops the context.
Definition context.cpp:126
void push()
Pushes a new context.
Definition context.cpp:118
The OutputStream class is used to render templates to a QTextStream.

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 %}
Author
Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 106 of file context.h.

Member Enumeration Documentation

◆ UrlType

The type of urls to external media that should be put in the template.

Enumerator
AbsoluteUrls 

Absolute URLs should be put in the template.

RelativeUrls 

Relative URLs should be put in the template.

Definition at line 229 of file context.h.

Constructor & Destructor Documentation

◆ Context() [1/3]

Context::Context ( )

Creates an empty context.

Definition at line 51 of file context.cpp.

◆ Context() [2/3]

Context::Context ( const QVariantHash & hash)
explicit

Sets every key in the hash as a property name with the variant as the value.

Definition at line 56 of file context.cpp.

◆ Context() [3/3]

Context::Context ( const Context & other)

Copy Constructor.

Definition at line 61 of file context.cpp.

◆ ~Context()

Context::~Context ( )

Destructor.

Definition at line 80 of file context.cpp.

Member Function Documentation

◆ externalMedia()

QList< std::pair< QString, QString > > Context::externalMedia ( ) const

Returns the external media encountered in the Template while rendering.

Definition at line 172 of file context.cpp.

◆ insert() [1/2]

void Context::insert ( const QString & name,
const QVariant & variant )

Insert the context object variant identified by name into the Context.

Definition at line 133 of file context.cpp.

◆ insert() [2/2]

void Context::insert ( const QString & name,
QObject * object )

Insert the context object object identified by name into the Context.

Definition at line 140 of file context.cpp.

◆ localizer()

QSharedPointer< AbstractLocalizer > Context::localizer ( ) const

Returns the localizer currently in use.

Definition at line 224 of file context.cpp.

◆ lookup()

QVariant Context::lookup ( const QString & str) const

Returns the context object identified by the key str.

Definition at line 97 of file context.cpp.

◆ operator=()

Context & Context::operator= ( const Context & other)

Assignmant operator.

Definition at line 67 of file context.cpp.

◆ pop()

void Context::pop ( )

Pops the context.

See also
Context Stack.

Definition at line 126 of file context.cpp.

◆ push()

void Context::push ( )

Pushes a new context.

See also
Context Stack.

Definition at line 118 of file context.cpp.

◆ relativeMediaPath()

QString Context::relativeMediaPath ( ) const

The relative path to external media to be used in templates.

Definition at line 202 of file context.cpp.

◆ renderContext()

RenderContext * Context::renderContext ( ) const

Returns a modifiable RenderContext.

This may be used to make Template rendering threadsafe so that render state does not need to be stored in the Node implementation itself.

Definition at line 208 of file context.cpp.

◆ setLocalizer()

void Context::setLocalizer ( QSharedPointer< AbstractLocalizer > localizer)

Sets the localizer to be used.

The Context takes ownerwhip of the localizer.

Definition at line 214 of file context.cpp.

◆ setRelativeMediaPath()

void Context::setRelativeMediaPath ( const QString & relativePath)

Sets the relative path to external media to be used in templates to relativePath.

See also
media_finder

Definition at line 196 of file context.cpp.

◆ setUrlType()

void Context::setUrlType ( Context::UrlType type)

Sets the type of external media URL to be used in the template to type.

See also
media_finder

Definition at line 184 of file context.cpp.

◆ urlType()

Context::UrlType Context::urlType ( ) const

The type of URL used in the template.

Definition at line 190 of file context.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.