6#include "clearsilver_p.h"
12#include <QtCore/QLoggingCategory>
14Q_LOGGING_CATEGORY(CUTELYST_CLEARSILVER,
"cutelyst.clearsilver", QtWarningMsg)
26 return d->includePaths;
32 d->includePaths = paths;
45 d->extension = extension;
62NEOERR *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)) {
113NEOERR *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);
139bool ClearSilverPrivate::render(
Context *c,
140 const QString &filename,
141 const QVariantHash &stash,
142 QByteArray &output)
const
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);
191void ClearSilverPrivate::renderError(
Context *c,
const QString &error)
const
197HDF *ClearSilverPrivate::hdfForStash(
Context *c,
const QVariantHash &stash)
const
202 serializeHash(hdf, stash);
206 QMetaProperty prop = meta->
property(i);
208 QVariant value = prop.
read(c);
209 serializeVariant(hdf, value, name);
214void ClearSilverPrivate::serializeHash(HDF *hdf,
215 const QVariantHash &hash,
216 const QString &prefix)
const
220 _prefix = prefix + QLatin1Char(
'.');
223 auto it = hash.constBegin();
224 while (it != hash.constEnd()) {
225 serializeVariant(hdf, it.value(), _prefix + it.key());
230void ClearSilverPrivate::serializeMap(HDF *hdf,
const QVariantMap &map,
const QString &prefix)
const
234 _prefix = prefix + QLatin1Char(
'.');
237 auto it = map.constBegin();
238 while (it != map.constEnd()) {
239 serializeVariant(hdf, it.value(), _prefix + it.key());
244void 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 wrapper() const
Returns the template wrapper.
void setTemplateExtension(const QString &extension)
Sets the template extension, defaults to ".html".
void setWrapper(const QString &name)
Sets the template wrapper name, the template will be rendered into content variable in which the wrap...
ClearSilver(QObject *parent=nullptr, const QString &name=QString())
Constructs a ClearSilver object with the given parent and name.
QStringList includePaths() const
Returns the list of include paths.
void setIncludePaths(const QStringList &paths)
Sets the list of include paths which will be looked for when resolving templates files.
QByteArray render(Context *c) const final
QString templateExtension() const
Returns the template extension.
void stash(const QVariantHash &unite)
Response * res() const noexcept
bool error() const noexcept
Returns true if an error was set.
void setBody(QIODevice *body)
View(QObject *parent, const QString &name)
The Cutelyst namespace holds all public Cutelyst API.
QByteArray & append(char ch)
QObject * parent() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QString & insert(int position, QChar ch)
bool isEmpty() const const
QByteArray toLatin1() const const