Cutelee  6.1.0
Public Member Functions | List of all members
Cutelee::Engine Class Reference

Cutelee::Engine is the main entry point for creating Cutelee Templates. More...

#include <cutelee/engine.h>

Inheritance diagram for Cutelee::Engine:
Inheritance graph
[legend]

Public Member Functions

 Engine (QObject *parent=nullptr)
 
 ~Engine () override
 
void addDefaultLibrary (const QString &libName)
 
void addPluginPath (const QString &dir)
 
void addTemplateLoader (std::shared_ptr< AbstractTemplateLoader > loader)
 
QStringList defaultLibraries () const
 
void insertDefaultLibrary (const QString &name, TagLibraryInterface *lib)
 
void insertLibrary (const QString &name, TagLibraryInterface *lib)
 
Template loadByName (const QString &name) const
 
std::pair< QString, QStringmediaUri (const QString &fileName) const
 
Template newTemplate (const QString &content, const QString &name) const
 
QStringList pluginPaths () const
 
void removeDefaultLibrary (const QString &libName)
 
void removePluginPath (const QString &dir)
 
void setPluginPaths (const QStringList &dirs)
 
void setSmartTrimEnabled (bool enabled)
 
bool smartTrimEnabled () const
 
QList< std::shared_ptr< AbstractTemplateLoader > > templateLoaders ()
 

Detailed Description

The Cutelee::Engine is responsible for configuring and creating Template objects. In typical use, one or more AbstractTemplateLoader objects will be added to the Engine to load template objects, and plugin directories will be set to enable finding template tags and filters.

auto engine = new Engine();
auto loader = std::shared_ptr<FileSystemTemplateLoader>::create();
loader->setTemplateDirs( {"/usr/share/myapp/templates"} );
engine->addTemplateLoader( loader );
engine->addPluginPath( "/usr/lib/myapp" );
auto template1 = engine->newTemplate( "Template content", "template name" );
auto template2 = engine->loadByName( "templatefile.html" );
Engine(QObject *parent=nullptr)
Definition: engine.cpp:42

Once it is configured, the Engine can be used to create new templates by name by loading the templates with the loadByName method, or by defining the content in the newTemplate method.

By default the builtin tags and filters distributed with Cutelee are available in all templates without using the {% load %} tag in the template. These pre-loaded libraries may be configured if appropriate to the application. For example, an application which defines its own tags and filters may want them to be always available, or it may be desirable to restrict the features available to template authors by removing built in libraries.

Different Engine objects can be used to create templates with differing configurations.

Insignificant whitespace

The output of rendering a template depends on the content of the template. In some cases when generating content in which whitespace is significant, this can have undesired effects. For example, given a template to generate C++ code like:

  class MyClass {
  {# This loop creates the  #}
  {# methods in the class   #}
  {% for method in methods %}
    {% if method.hasDox %}
      {{ method.dox }}
    {% endif %}
      {{ method.signature }}
  {% endfor %}
  };

The output would have a lot of whitespace which is not necessarily wanted.

class MyClass {
void foo() const;
};

It is possible to strip insignificant whitespace by enabling the smartTrim feature with setSmartTrimEnabled. When enabled the output will not contain a newline for any line in the template which has only one token of template syntax, such as a comment, tag or variable.

class MyClass {
void foo() const;
};
Author
Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 120 of file engine.h.

Constructor & Destructor Documentation

◆ Engine()

Engine::Engine ( QObject *  parent = nullptr)

Constructor

Definition at line 42 of file engine.cpp.

◆ ~Engine()

Engine::~Engine ( )
override

Destructor.

Definition at line 53 of file engine.cpp.

Member Function Documentation

◆ addDefaultLibrary()

void Engine::addDefaultLibrary ( const QString libName)

Adds the library named libName to the libraries available by default to new Templates.

Definition at line 120 of file engine.cpp.

◆ addPluginPath()

void Engine::addPluginPath ( const QString dir)

Prepend dir to the list of plugin dirs.

Definition at line 93 of file engine.cpp.

◆ addTemplateLoader()

void Engine::addTemplateLoader ( std::shared_ptr< AbstractTemplateLoader loader)

Adds loader to the TemplateLoaders currently configured on the Engine.

Definition at line 68 of file engine.cpp.

◆ defaultLibraries()

QStringList Engine::defaultLibraries ( ) const

Returns the libraries available by default to new Templates.

Definition at line 114 of file engine.cpp.

Referenced by Cutelee::Parser::Parser().

◆ insertDefaultLibrary()

void Engine::insertDefaultLibrary ( const QString name,
TagLibraryInterface lib 
)

Inserts the tag library lib without requiring to load a plugin and adds it to the list of libraries available by default to new Templates.

See also
insertLibrary

Definition at line 231 of file engine.cpp.

References insertLibrary().

◆ insertLibrary()

void Engine::insertLibrary ( const QString name,
TagLibraryInterface lib 
)

Inserts the tag library lib without requiring to load a plugin.

In your template you need to use {% load name %} to make the tags and filters of this library available.

See also
insertDefaultLibrary

Definition at line 224 of file engine.cpp.

Referenced by insertDefaultLibrary().

◆ loadByName()

Template Engine::loadByName ( const QString name) const

Load the Template identified by name.

The Templates and plugins loaded will be determined by the Engine configuration.

Definition at line 370 of file engine.cpp.

◆ mediaUri()

std::pair< QString, QString > Engine::mediaUri ( const QString fileName) const

Returns a URI for a media item with the name name.

Typically this will be used for images. For example the media URI for the image "header_logo.png" may be "/home/user/common/header_logo.png" or "/home/user/some_theme/header_logo.png" depending on the templateLoaders configured.

This method will not usually be called by application code. To load media in a template, use the {% media_finder %} template tag.

Definition at line 74 of file engine.cpp.

◆ newTemplate()

Template Engine::newTemplate ( const QString content,
const QString name 
) const

Create a new Template with the content content identified by name.

The secondary Templates and plugins loaded will be determined by the Engine configuration.

Definition at line 391 of file engine.cpp.

Referenced by Cutelee::FileSystemTemplateLoader::loadByName(), and Cutelee::InMemoryTemplateLoader::loadByName().

◆ pluginPaths()

QStringList Engine::pluginPaths ( ) const

Returns the currently configured plugin dirs

Definition at line 108 of file engine.cpp.

◆ removeDefaultLibrary()

void Engine::removeDefaultLibrary ( const QString libName)

Removes the library named libName from the libraries available by default to new Templates.

Definition at line 126 of file engine.cpp.

◆ removePluginPath()

void Engine::removePluginPath ( const QString dir)

Removes all instances of dir from the list of plugin dirs.

Definition at line 102 of file engine.cpp.

◆ setPluginPaths()

void Engine::setPluginPaths ( const QStringList &  dirs)

Sets the plugin dirs currently configured on the Engine to dirs.

Warning
This overwrites the default paths. You normally want addPluginPath.
See also
Finding tags and filters

Definition at line 87 of file engine.cpp.

◆ setSmartTrimEnabled()

void Engine::setSmartTrimEnabled ( bool  enabled)

Sets whether the smart trim feature is enabled for newly loaded templates.

See also
smart_trim

Definition at line 400 of file engine.cpp.

◆ smartTrimEnabled()

bool Engine::smartTrimEnabled ( ) const

Returns whether the smart trim feature is enabled for newly loaded templates.

See also
smart_trim

This is false by default.

Definition at line 406 of file engine.cpp.

◆ templateLoaders()

QList< std::shared_ptr< AbstractTemplateLoader > > Engine::templateLoaders ( )

Returns the TemplateLoaders currently configured on the Engine.

Definition at line 62 of file engine.cpp.