6 #include "clearsilver_p.h" 12 #include <QtCore/QLoggingCategory> 14 Q_LOGGING_CATEGORY(CUTELYST_CLEARSILVER,
"cutelyst.clearsilver", QtWarningMsg)
19 :
View(new ClearSilverPrivate, parent, name)
26 return d->includePaths;
32 d->includePaths = paths;
45 d->extension = extension;
62 NEOERR *cutelyst_render(
void *user,
char *data)
77 const QVariantHash &stash = c->
stash();
78 QString templateFile = stash.value(QStringLiteral(
"template")).toString();
81 templateFile = c->action()->
reverse() + d->extension;
85 c->
error(QStringLiteral(
86 "Cannot render template, template name or template stash key not defined"));
91 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template" << templateFile;
93 if (!d->render(c, templateFile, stash, body)) {
97 if (!d->wrapper.isEmpty()) {
98 QString wrapperFile = d->wrapper;
100 QVariantHash data = stash;
101 data.
insert(QStringLiteral(
"content"), body);
104 if (!d->render(c, wrapperFile, data, body)) {
113 NEOERR *findFile(
void *c, HDF *hdf,
const char *filename,
char **contents)
116 const ClearSilverPrivate *priv =
static_cast<ClearSilverPrivate *
>(c);
118 return nerr_raise(NERR_NOMEM,
"Cound not cast ClearSilverPrivate");
121 for (
const QString &includePath : priv->includePaths) {
127 NERR_IO,
"Cound not open file: %s", file.errorString().toLatin1().data());
130 *contents = qstrdup(file.readAll().constData());
131 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template:" << file.fileName();
136 return nerr_raise(NERR_NOT_FOUND,
"Cound not find file: %s", filename);
139 bool ClearSilverPrivate::render(
Context *c,
141 const QVariantHash &stash,
144 HDF *hdf = hdfForStash(c, stash);
148 error = cs_init(&cs, hdf);
152 nerr_error_traceback(error, &msg);
154 errorMsg = QStringLiteral(
"Failed to init ClearSilver:\n+1")
156 renderError(c, errorMsg);
164 cs_register_fileload(cs, const_cast<ClearSilverPrivate *>(
this), findFile);
170 nerr_error_traceback(error, &msg);
172 errorMsg = QStringLiteral(
"Failed to parse template file: +1\n+2")
174 renderError(c, errorMsg);
175 nerr_log_error(error);
183 cs_render(cs, &output, cutelyst_render);
191 void ClearSilverPrivate::renderError(
Context *c,
const QString &error)
const 197 HDF *ClearSilverPrivate::hdfForStash(
Context *c,
const QVariantHash &stash)
const 202 serializeHash(hdf, stash);
209 serializeVariant(hdf, value, name);
214 void ClearSilverPrivate::serializeHash(HDF *hdf,
215 const QVariantHash &hash,
223 auto it = hash.constBegin();
224 while (it != hash.constEnd()) {
225 serializeVariant(hdf, it.value(), _prefix + it.key());
230 void ClearSilverPrivate::serializeMap(HDF *hdf,
const QVariantMap &map,
const QString &prefix)
const 237 auto it = map.constBegin();
238 while (it != map.constEnd()) {
239 serializeVariant(hdf, it.value(), _prefix + it.key());
244 void ClearSilverPrivate::serializeVariant(HDF *hdf,
const QVariant &value,
const QString &key)
const 248 switch (value.type()) {
250 hdf_set_value(hdf, key.
toLatin1().
data(), value.toString().toLatin1().data());
253 hdf_set_int_value(hdf, key.
toLatin1().
data(), value.toInt());
256 serializeHash(hdf, value.toHash(), key);
259 serializeMap(hdf, value.toMap(), key);
263 hdf_set_value(hdf, key.
toLatin1().
data(), value.toString().toLatin1().data());
269 #include "moc_clearsilver.cpp"
QString templateExtension() const
Returns the template extension.
bool error() const noexcept
Returns true if an error was set.
Response * res() const noexcept
QString wrapper() const
Returns the template wrapper.
ClearSilver(QObject *parent=nullptr, const QString &name=QString())
Constructs a ClearSilver object with the given parent and name.
QByteArray render(Context *c) const final
QString & insert(int position, QChar ch)
void stash(const QVariantHash &unite)
bool isEmpty() const const
The Cutelyst namespace holds all public Cutelyst API.
QByteArray & append(char ch)
void setTemplateExtension(const QString &extension)
Sets the template extension, defaults to ".html".
QStringList includePaths() const
Returns the list of include paths.
QByteArray toLatin1() const const
void setIncludePaths(const QStringList &paths)
Sets the list of include paths which will be looked for when resolving templates files.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
Cutelyst View abstract view component
void setWrapper(const QString &name)
Sets the template wrapper name, the template will be rendered into content variable in which the wrap...
QString fromLatin1(const char *str, int size)
void setBody(QIODevice *body)