/usr/include/grantlee/context.h is in libgrantlee5-dev 5.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | /*
This file is part of the Grantlee template system.
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either version
2.1 of the Licence, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRANTLEE_CONTEXT_H
#define GRANTLEE_CONTEXT_H
#include "abstractlocalizer.h"
#include "grantlee_templates_export.h"
#include <QtCore/QVariantHash>
namespace Grantlee
{
class RenderContext;
class ContextPrivate;
/// @headerfile context.h grantlee/context.h
/**
@brief 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.
@code
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."
@endcode
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 @ref custom_objects
@section context_stack 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 @gr_tag{with} tag. In a template such as
@code
Some content
{% with person.name|toUpper as lowerName %}
Name is {% lowerName %}
{% endwith %}
@endcode
In this case, lowerName is available in the context only between the
@gr_tag{with} and @gr_tag{endwith} tags. The implementation of
the @gr_tag{with} tag render method is:
@code
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.
}
@endcode
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.
@code
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 %}
@endcode
@author Stephen Kelly <steveire@gmail.com>
*/
class GRANTLEE_TEMPLATES_EXPORT Context
{
public:
/**
Creates an empty context
*/
Context();
/**
Sets every key in the hash as a property name with the variant as the
value.
*/
explicit Context(const QVariantHash &hash);
/**
Copy Constructor
*/
Context(const Context &other);
/**
Assignmant operator
*/
Context &operator=(const Context &other);
#ifndef Q_QDOC
/**
@internal
Whether to automatically escape all context content. This is not usually
used directly. Use the @gr_tag{autoescape} tag instead.
*/
bool autoEscape() const;
/**
@internal
Sets whether to automatically escape all context content. This is not
usually used directly. Use the @gr_tag{autoescape} tag instead.
*/
void setAutoEscape(bool autoescape);
#endif
/**
Destructor
*/
~Context();
/**
Returns the context object identified by the key @p str
*/
QVariant lookup(const QString &str) const;
/**
Insert the context object @p object identified by @p name into the
Context.
*/
void insert(const QString &name, QObject *object);
/**
Insert the context object @p variant identified by @p name into the
Context.
*/
void insert(const QString &name, const QVariant &variant);
/**
Pushes a new context.
@see @ref context_stack
*/
void push();
/**
Pops the context.
@see @ref context_stack
*/
void pop();
#ifndef Q_QDOC
/**
@internal Returns the context hash at depth @p depth.
*/
QVariantHash stackHash(int depth) const;
/**
@internal
Returns whether template being rendered is being mutated.
*/
bool isMutating() const;
/**
@internal
Sets whether template being rendered is being mutated to @p mutating.
*/
void setMutating(bool mutating);
/**
@internal
*/
void addExternalMedia(const QString &absolutePart,
const QString &relativePart);
/**
@internal
*/
void clearExternalMedia();
#endif
/**
Sets the localizer to be used.
The Context takes ownerwhip of the localizer.
*/
void setLocalizer(QSharedPointer<AbstractLocalizer> localizer);
/**
Returns the localizer currently in use.
*/
QSharedPointer<AbstractLocalizer> localizer() const;
/**
Returns the external media encountered in the Template while rendering.
*/
QList<QPair<QString, QString>> externalMedia() const;
/**
The type of urls to external media that should be put in the template.
*/
enum UrlType {
AbsoluteUrls, ///< Absolute URLs should be put in the template.
RelativeUrls ///< Relative URLs should be put in the template.
};
/**
Sets the type of external media URL to be used in the template to @p type.
@see @ref media_finder_tag
*/
void setUrlType(UrlType type);
/**
The type of URL used in the template.
*/
UrlType urlType() const;
/**
Sets the relative path to external media to be used in templates to @p
relativePath
@see @ref media_finder_tag
*/
void setRelativeMediaPath(const QString &relativePath);
/**
The relative path to external media to be used in templates.
*/
QString relativeMediaPath() const;
/**
* Returns a modifiable RenderContext for the Node @p 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.
*/
RenderContext *renderContext() const;
private:
Q_DECLARE_PRIVATE(Context)
ContextPrivate *const d_ptr;
};
}
#endif
|