24 #include "blockcontext.h"
26 #include "exception.h"
27 #include "nodebuiltins_p.h"
29 #include "rendercontext.h"
35 ExtendsNodeFactory::ExtendsNodeFactory(QObject *parent)
47 QStringLiteral(
"Error: Include tag takes only one argument"));
53 auto t = qobject_cast<TemplateImpl *>(p->parent());
57 TagSyntaxError, QStringLiteral(
"Extends tag is not in a template."));
59 const auto nodeList = p->
parse(t);
60 n->setNodeList(nodeList);
65 QStringLiteral(
"Extends tag may only appear once in a template."));
72 :
Node(parent), m_filterExpression(fe)
76 ExtendsNode::~ExtendsNode() {}
78 static QHash<QString, BlockNode *> createNodeMap(
const QList<BlockNode *> &list)
80 QHash<QString, BlockNode *> map;
82 auto it = list.constBegin();
83 const auto end = list.constEnd();
85 for (; it != end; ++it) {
86 map.insert((*it)->name(), *it);
92 void ExtendsNode::setNodeList(
const NodeList &list)
97 m_blocks = createNodeMap(blockList);
102 const auto parentVar = m_filterExpression.
resolve(c);
103 if (parentVar.userType() == qMetaTypeId<Cutelee::Template>()) {
111 const auto t = ti->engine()->loadByName(parentName);
116 QStringLiteral(
"Template not found %1").arg(parentName));
126 const auto parentTemplate = getParent(c);
128 if (!parentTemplate) {
130 QStringLiteral(
"Cannot load template."));
135 blockContext.addBlocks(m_blocks);
136 variant.setValue(blockContext);
138 const auto nodeList = parentTemplate->nodeList();
140 const auto parentBlocks
141 = createNodeMap(parentTemplate->findChildren<
BlockNode *>());
143 for (
auto n : nodeList) {
144 auto tn = qobject_cast<TextNode *>(n);
146 auto en = qobject_cast<ExtendsNode *>(n);
148 blockContext.addBlocks(parentBlocks);
149 variant.setValue(blockContext);
154 variant.setValue(blockContext);
155 parentTemplate->nodeList().render(stream, c);
157 auto nodes = parentTemplate->findChildren<
BlockNode *>();
158 blockContext.remove(nodes);
159 variant.setValue(blockContext);
162 void ExtendsNode::appendNode(
Node *node)
165 node->setParent(parent());
Base class for all NodeFactories.
Q_INVOKABLE QStringList smartSplit(const QString &str) const
The Context class holds the context to render a Template with.
RenderContext * renderContext() const
An exception for use when implementing template tags.
A FilterExpression object represents a filter expression in a template.
QVariant resolve(OutputStream *stream, Context *c) const
A list of Nodes with some convenience API for rendering them.
QList< T > findChildren()
void append(Cutelee::Node *node)
Base class for all nodes.
TemplateImpl * containerTemplate() const
The OutputStream class is used to render templates to a QTextStream.
The Parser class processes a string template into a tree of nodes.
NodeList parse(Node *parent, const QStringList &stopAt={})
QVariant & data(const Node *const scopeNode)
The Template class is a tree of nodes which may be rendered.
Node * getNode(const QString &tagContent, Parser *p) const override
void render(OutputStream *stream, Context *c) const override
The Cutelee namespace holds all public Cutelee API.
Cutelee::SafeString getSafeString(const QVariant &input)
Utility functions used throughout Cutelee.