Cutelee
6.1.0
include.cpp
1
/*
2
This file is part of the Cutelee template system.
3
4
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
5
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2.1 of the Licence, or (at your option) any later version.
10
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
18
19
*/
20
21
#include "include.h"
22
23
#include "block.h"
24
#include "blockcontext.h"
25
#include "engine.h"
26
#include "exception.h"
27
#include "parser.h"
28
#include "rendercontext.h"
29
#include "template.h"
30
#include "
util.h
"
31
32
IncludeNodeFactory::IncludeNodeFactory() {}
33
34
Node
*
IncludeNodeFactory::getNode
(
const
QString
&tagContent,
Parser
*p)
const
35
{
36
auto
expr =
smartSplit
(tagContent);
37
38
if
(expr.size() != 2)
39
throw
Cutelee::Exception
(
40
TagSyntaxError,
41
QStringLiteral(
"Error: Include tag takes only one argument"
));
42
43
auto
includeName = expr.at(1);
44
auto
size = includeName.size();
45
46
if
((includeName.startsWith(
QLatin1Char
(
'"'
))
47
&& includeName.endsWith(
QLatin1Char
(
'"'
)))
48
|| (includeName.startsWith(
QLatin1Char
(
'\''
))
49
&& includeName.endsWith(
QLatin1Char
(
'\''
)))) {
50
return
new
ConstantIncludeNode
(includeName.mid(1, size - 2));
51
}
52
return
new
IncludeNode
(
FilterExpression
(includeName, p), p);
53
}
54
55
IncludeNode::IncludeNode(
const
FilterExpression
&fe,
QObject
*parent)
56
:
Node
(parent), m_filterExpression(fe)
57
{
58
}
59
60
void
IncludeNode::render
(
OutputStream
*stream,
Context
*c)
const
61
{
62
QString
filename =
getSafeString
(m_filterExpression.resolve(c));
63
64
auto
ti =
containerTemplate
();
65
66
auto
t = ti->engine()->loadByName(filename);
67
68
if
(!t)
69
throw
Cutelee::Exception
(
70
TagSyntaxError, QStringLiteral(
"Template not found %1"
).arg(filename));
71
72
if
(t->error())
73
throw
Cutelee::Exception
(t->error(), t->errorString());
74
75
t->render(stream, c);
76
77
if
(t->error())
78
throw
Cutelee::Exception
(t->error(), t->errorString());
79
}
80
81
ConstantIncludeNode::ConstantIncludeNode(
const
QString
&name,
QObject
*parent)
82
:
Node
(parent)
83
{
84
m_name = name;
85
}
86
87
void
ConstantIncludeNode::render
(
OutputStream
*stream,
Context
*c)
const
88
{
89
auto
ti =
containerTemplate
();
90
91
auto
t = ti->engine()->loadByName(m_name);
92
if
(!t)
93
throw
Cutelee::Exception
(
94
TagSyntaxError, QStringLiteral(
"Template not found %1"
).arg(m_name));
95
96
if
(t->error())
97
throw
Cutelee::Exception
(t->error(), t->errorString());
98
99
t->render(stream, c);
100
101
if
(t->error())
102
throw
Cutelee::Exception
(t->error(), t->errorString());
103
104
QVariant
&variant = c->
renderContext
()->
data
(0);
105
auto
blockContext = variant.
value
<
BlockContext
>();
106
auto
nodes = t->findChildren<
BlockNode
*>();
107
blockContext.remove(nodes);
108
variant.
setValue
(blockContext);
109
}
BlockContext
Definition
blockcontext.h:30
BlockNode
Definition
block.h:43
ConstantIncludeNode
Definition
include.h:54
ConstantIncludeNode::render
void render(OutputStream *stream, Context *c) const override
Definition
include.cpp:87
Cutelee::AbstractNodeFactory::smartSplit
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition
node.cpp:202
Cutelee::Context
The Context class holds the context to render a Template with.
Definition
context.h:119
Cutelee::Context::renderContext
RenderContext * renderContext() const
Definition
context.cpp:214
Cutelee::Exception
An exception for use when implementing template tags.
Definition
exception.h:85
Cutelee::FilterExpression
A FilterExpression object represents a filter expression in a template.
Definition
filterexpression.h:120
Cutelee::Node
Base class for all nodes.
Definition
node.h:78
Cutelee::Node::containerTemplate
TemplateImpl * containerTemplate() const
Definition
node.cpp:107
Cutelee::OutputStream
The OutputStream class is used to render templates to a QTextStream.
Definition
outputstream.h:81
Cutelee::Parser
The Parser class processes a string template into a tree of nodes.
Definition
parser.h:49
Cutelee::RenderContext::data
QVariant & data(const Node *const scopeNode)
Definition
rendercontext.cpp:58
IncludeNodeFactory::getNode
Node * getNode(const QString &tagContent, Parser *p) const override
Definition
include.cpp:34
IncludeNode
Definition
include.h:43
IncludeNode::render
void render(OutputStream *stream, Context *c) const override
Definition
include.cpp:60
Cutelee::getSafeString
Cutelee::SafeString getSafeString(const QVariant &input)
Definition
util.cpp:108
QLatin1Char
QObject
QString
QVariant
QVariant::setValue
void setValue(const T &value)
QVariant::value
T value() const const
util.h
Utility functions used throughout Cutelee.
templates
loadertags
include.cpp
Generated by
1.16.1