NuriaProject Framework  0.1
The NuriaProject Framework
Public Member Functions | Friends | List of all members
Nuria::TemplateProgram Class Reference

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
 
TemplateProgramoperator= (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
 

Detailed Description

Represents a compiled Twig code program.

A TemplateProgram stores a Twig program independent from the TemplateEngine used to create it and all other classes.

Note
Instances of this class can only be created by TemplateEngine.
This structure is implicitly shared.
Caching

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.

Copy-Behaviour

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.

Note
The internal program is never copied nor changed by any operation.
Variables

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.

See also
neededVariables canRender render

Constructor & Destructor Documentation

Nuria::TemplateProgram::TemplateProgram ( )

Constructor for a invalid instance.

Nuria::TemplateProgram::TemplateProgram ( const TemplateProgram other)

Copy constructor.

Nuria::TemplateProgram::~TemplateProgram ( )

Destructor.

Member Function Documentation

void Nuria::TemplateProgram::addFunction ( const QString &  name,
const Callback function 
)

Adds function, making it known as name.

Note
Built-in functions can not be overridden.

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.

Warning
While it's possible to override user-defined functions here, overriding a constant function can have unwanted side-effects.
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.

See also
neededVariables
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.

See also
lastError
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.


The documentation for this class was generated from the following file: