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 
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 }
void render(OutputStream *stream, Context *c) const override
Definition: include.cpp:87
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Definition: node.cpp:202
The Context class holds the context to render a Template with.
Definition: context.h:119
RenderContext * renderContext() const
Definition: context.cpp:214
An exception for use when implementing template tags.
Definition: exception.h:85
A FilterExpression object represents a filter expression in a template.
QVariant resolve(OutputStream *stream, Context *c) const
Base class for all nodes.
Definition: node.h:78
TemplateImpl * containerTemplate() const
Definition: node.cpp:107
The OutputStream class is used to render templates to a QTextStream.
Definition: outputstream.h:81
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49
QVariant & data(const Node *const scopeNode)
Node * getNode(const QString &tagContent, Parser *p) const override
Definition: include.cpp:34
void render(OutputStream *stream, Context *c) const override
Definition: include.cpp:60
Cutelee::SafeString getSafeString(const QVariant &input)
Definition: util.cpp:108
Utility functions used throughout Cutelee.