Permalink
Browse files

Fix broken tag extension example code to compile cleanly

* CurrentTimeTag::getNode should be marked "override"
* CurrentTimeTag::getNode 'p' should be marked "Q_UNUSED()"
* Grantlee:: namespace missing in several places
* CurrentTimeNode constructor needs to initialize the base class "Grantlee::Node", not "QObject"
* CurrentTimeNode constructor needs default initializer
* CurrentTimeNode::render needs to be marked "const override"
* CurrentTimeNode::render doesn't return anything, it needs to write it to the stream
* To be consistent with the builtins, CurrentTimeTag should be called CurrentTimeTagFactory
  • Loading branch information...
asmaloney authored and steveire committed Aug 15, 2017
1 parent bfaaae3 commit fb191888527bccc1de33eaac54bfe2e047f08b4d
Showing with 29 additions and 26 deletions.
  1. +29 −26 dox/extension.dox
@@ -96,38 +96,41 @@ namespace Grantlee
Here is an example of a @gr_tag{current_time} tag which displays the current time.
@code
class CurrentTimeTag : public Grantlee::AbstractNodeFactory
class CurrentTimeNode : public Grantlee::Node
{
Grantlee::Node *getNode(const QString &tagContent, Parser *p) const
{
// You almost always want to use smartSplit.
QStringList parts = smartSplit(tagContent);
parts.removeFirst(); // Not interested in the name of the tag.
if (!parts.isEmpty())
// The remaining parts are the arguments to the tag. If an incorrect number of arguments
// is supplied, and exception should be thrown.
throw Grantlee::Exception( TagSyntaxError, "current_time does not take any arguments" );
return new CurrentTimeNode();
}
Q_OBJECT
public:
CurrentTimeNode( QObject *parent = nullptr )
: Grantlee::Node( parent )
{
}
virtual void render( Grantlee::OutputStream *stream, Grantlee::Context *c) const override
{
Q_UNUSED(c);
(*stream) << QDateTime::currentDateTime().toString();
}
};
class CurrentTimeNode : public Grantlee::Node
class CurrentTimeTagFactory : public Grantlee::AbstractNodeFactory
{
Q_OBJECT
public:
CurrentTimeNode(QObject *parent)
: QObject(parent)
{
Grantlee::Node *getNode(const QString &tagContent, Grantlee::Parser *p) const override
{
Q_UNUSED(p);
}
// You almost always want to use smartSplit.
QStringList parts = smartSplit(tagContent);
parts.removeFirst(); // Not interested in the name of the tag.
void render( Grantlee::OutputStream *stream, Context *c) const
{
Q_UNUSED(c);
return QDateTime::currentDateTime().toString();
}
if (!parts.isEmpty())
// The remaining parts are the arguments to the tag. If an incorrect number of arguments
// is supplied, and exception should be thrown.
throw Grantlee::Exception( Grantlee::TagSyntaxError, "current_time does not take any arguments" );
return new CurrentTimeNode();
}
};
@endcode

0 comments on commit fb19188

Please sign in to comment.