5 #include "clearsilver_p.h" 13 #include <QtCore/QLoggingCategory> 15 Q_LOGGING_CATEGORY(CUTELYST_CLEARSILVER,
"cutelyst.clearsilver", QtWarningMsg)
27 return d->includePaths;
33 d->includePaths = paths;
46 d->extension = extension;
63 NEOERR* cutelyst_render(
void *user,
char *data)
78 const QVariantHash &stash = c->
stash();
79 QString templateFile = stash.value(QStringLiteral(
"template")).toString();
82 templateFile = c->action()->
reverse() + d->extension;
86 c->
error(QStringLiteral(
"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) {
125 if (!file.open(QFile::ReadOnly)) {
126 return nerr_raise(NERR_IO,
"Cound not open file: %s", file.errorString().toLatin1().data());
129 *contents = qstrdup(file.readAll().constData());
130 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template:" << file.fileName();
135 return nerr_raise(NERR_NOT_FOUND,
"Cound not find file: %s", filename);
138 bool ClearSilverPrivate::render(
Context *c,
const QString &filename,
const QVariantHash &stash,
QByteArray &output)
const 140 HDF *hdf = hdfForStash(c, stash);
144 error = cs_init(&cs, hdf);
148 nerr_error_traceback(error, &msg);
151 renderError(c, errorMsg);
159 cs_register_fileload(cs, const_cast<ClearSilverPrivate*>(
this), findFile);
165 nerr_error_traceback(error, &msg);
167 errorMsg = QStringLiteral(
"Failed to parse template file: +1\n+2").
arg(filename,
QString::fromLatin1(msg.buf, msg.len));
168 renderError(c, errorMsg);
169 nerr_log_error(error);
177 cs_render(cs, &output, cutelyst_render);
185 void ClearSilverPrivate::renderError(
Context *c,
const QString &error)
const 191 HDF *ClearSilverPrivate::hdfForStash(
Context *c,
const QVariantHash &stash)
const 196 serializeHash(hdf, stash);
203 serializeVariant(hdf, value, name);
208 void ClearSilverPrivate::serializeHash(HDF *hdf,
const QVariantHash &hash,
const QString &prefix)
const 215 auto it = hash.constBegin();
216 while (it != hash.constEnd()) {
217 serializeVariant(hdf, it.value(), _prefix + it.key());
222 void ClearSilverPrivate::serializeMap(HDF *hdf,
const QVariantMap &map,
const QString &prefix)
const 229 auto it = map.constBegin();
230 while (it != map.constEnd()) {
231 serializeVariant(hdf, it.value(), _prefix + it.key());
236 void ClearSilverPrivate::serializeVariant(HDF *hdf,
const QVariant &value,
const QString &key)
const 240 switch (value.type()) {
241 case QVariant::String:
242 hdf_set_value(hdf, key.
toLatin1().
data(), value.toString().toLatin1().data());
245 hdf_set_int_value(hdf, key.
toLatin1().
data(), value.toInt());
248 serializeHash(hdf, value.toHash(), key);
251 serializeMap(hdf, value.toMap(), key);
254 if (value.canConvert(QMetaType::QString)) {
255 hdf_set_value(hdf, key.
toLatin1().
data(), value.toString().toLatin1().data());
261 #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.
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)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const