NuriaProject Framework
0.1
The NuriaProject Framework
|
Represents a compiled Twig code program. More...
#include <templateprogram.hpp>
Public Member Functions | |
TemplateProgram () | |
TemplateProgram (const TemplateProgram &other) | |
~TemplateProgram () | |
void | addFunction (const QString &name, const Callback &function) |
bool | canRender () const |
QStringList | dependencies () const |
bool | hasFunction (const QString &name) |
bool | isValid () const |
TemplateError | lastError () const |
QLocale | locale () const |
QStringList | neededVariables () const |
TemplateProgram & | operator= (const TemplateProgram &other) |
QString | render () |
void | setLocale (const QLocale &locale) |
bool | setValue (const QString &variable, const QVariant &value) |
QVariant | value (const QString &variable) const |
Friends | |
class | TemplateEngine |
Represents a compiled Twig code program.
A TemplateProgram stores a Twig program independent from the TemplateEngine used to create it and all other classes.
You're free to cache instances of this class wherever you want, though you should be advised that in this case you won't be notified when a template loader notifies the application of a changed Twig template.
Because of this, it's advisable to create a TemplateEngine once and keep using it throughout the application. See Nuria::DependencyManager for a easy way of doing this.
This class stores variables implicitly shared, meaning that using setValue() on a instance will change the variable only in this program. Note that this is a comparably cheap operation, as the internal program is stored in its own structure.
By default, a program is in strict mode, meaning that all referenced variables and functions must be present upon rendering. If there's a variable without a value set, then render() will fail by returning an empty string and setting the last error accordingly. You can use neededVariables() to get a list of variables needed by the program to execute.
Nuria::TemplateProgram::TemplateProgram | ( | ) |
Constructor for a invalid instance.
Nuria::TemplateProgram::TemplateProgram | ( | const TemplateProgram & | other | ) |
Copy constructor.
Nuria::TemplateProgram::~TemplateProgram | ( | ) |
Destructor.
void Nuria::TemplateProgram::addFunction | ( | const QString & | name, |
const Callback & | function | ||
) |
Adds function, making it known as name.
It's also possible to explicitly define constant functions which are evaluated (if possible) during compilation. If you want to do that, please use TemplateEngine::addFunction() instead.
bool Nuria::TemplateProgram::canRender | ( | ) | const |
Checks if all needed variables are set. If yes, then true
is returned. If not, then false
is returned and lastError() will return the TemplateError indicating which variable is missing.
QStringList Nuria::TemplateProgram::dependencies | ( | ) | const |
Returns a list of all templates this program depends on, meaning, which have been included or extended.
bool Nuria::TemplateProgram::hasFunction | ( | const QString & | name | ) |
Returns true
, if there's a user-defined function called name.
bool Nuria::TemplateProgram::isValid | ( | ) | const |
Returns true
if this instance is valid.
TemplateError Nuria::TemplateProgram::lastError | ( | ) | const |
Returns the last error.
QLocale Nuria::TemplateProgram::locale | ( | ) | const |
Returns the locale used by this program.
QStringList Nuria::TemplateProgram::neededVariables | ( | ) | const |
Returns a unordered list of variable names needed by this program.
TemplateProgram& Nuria::TemplateProgram::operator= | ( | const TemplateProgram & | other | ) |
Assignment operator.
QString Nuria::TemplateProgram::render | ( | ) |
Executes the program and returns the result. A empty result indicates an error.
void Nuria::TemplateProgram::setLocale | ( | const QLocale & | locale | ) |
Sets the locale used by this program.
bool Nuria::TemplateProgram::setValue | ( | const QString & | variable, |
const QVariant & | value | ||
) |
Sets the value of variable to value. Returns true
if this program needs variable, else false
is returned.
QVariant Nuria::TemplateProgram::value | ( | const QString & | variable | ) | const |
Returns the current value of variable.