18 #include "clearsilver_p.h"
26 #include <QtCore/QLoggingCategory>
28 Q_LOGGING_CATEGORY(CUTELYST_CLEARSILVER,
"cutelyst.clearsilver", QtWarningMsg)
30 using namespace Cutelyst;
40 return d->includePaths;
46 d->includePaths = paths;
59 d->extension = extension;
76 NEOERR* cutelyst_render(
void *user,
char *data)
91 const QVariantHash &stash = c->
stash();
92 QString templateFile = stash.value(QStringLiteral(
"template")).toString();
95 templateFile = c->action()->
reverse() + d->extension;
99 c->
error(QStringLiteral(
"Cannot render template, template name or template stash key not defined"));
104 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template" <<templateFile;
106 if (!d->render(c, templateFile, stash, body)) {
110 if (!d->wrapper.isEmpty()) {
111 QString wrapperFile = d->wrapper;
113 QVariantHash data = stash;
114 data.
insert(QStringLiteral(
"content"), body);
117 if (!d->render(c, wrapperFile, data, body)) {
126 NEOERR* findFile(
void *c, HDF *hdf,
const char *filename,
char **contents)
129 const ClearSilverPrivate *priv = static_cast<ClearSilverPrivate*>(c);
131 return nerr_raise(NERR_NOMEM,
"Cound not cast ClearSilverPrivate");
134 for (
const QString &includePath : priv->includePaths) {
138 if (!file.open(QFile::ReadOnly)) {
139 return nerr_raise(NERR_IO,
"Cound not open file: %s", file.errorString().toLatin1().data());
142 *contents = qstrdup(file.readAll().constData());
143 qCDebug(CUTELYST_CLEARSILVER) <<
"Rendering template:" << file.fileName();
148 return nerr_raise(NERR_NOT_FOUND,
"Cound not find file: %s", filename);
151 bool ClearSilverPrivate::render(
Context *c,
const QString &filename,
const QVariantHash &stash,
QByteArray &output)
const
153 HDF *hdf = hdfForStash(c, stash);
157 error = cs_init(&cs, hdf);
161 nerr_error_traceback(error, &msg);
164 renderError(c, errorMsg);
172 cs_register_fileload(cs, const_cast<ClearSilverPrivate*>(
this), findFile);
178 nerr_error_traceback(error, &msg);
180 errorMsg = QStringLiteral(
"Failed to parse template file: +1\n+2").
arg(filename,
QString::fromLatin1(msg.buf, msg.len));
181 renderError(c, errorMsg);
182 nerr_log_error(error);
190 cs_render(cs, &output, cutelyst_render);
198 void ClearSilverPrivate::renderError(
Context *c,
const QString &error)
const
204 HDF *ClearSilverPrivate::hdfForStash(
Context *c,
const QVariantHash &stash)
const
209 serializeHash(hdf, stash);
216 serializeVariant(hdf, value, name);
221 void ClearSilverPrivate::serializeHash(HDF *hdf,
const QVariantHash &hash,
const QString &prefix)
const
228 auto it = hash.constBegin();
229 while (it != hash.constEnd()) {
230 serializeVariant(hdf, it.value(), _prefix + it.key());
235 void ClearSilverPrivate::serializeMap(HDF *hdf,
const QVariantMap &map,
const QString &prefix)
const
242 auto it = map.constBegin();
243 while (it != map.constEnd()) {
244 serializeVariant(hdf, it.value(), _prefix + it.key());
249 void ClearSilverPrivate::serializeVariant(HDF *hdf,
const QVariant &value,
const QString &key)
const
253 switch (value.
type()) {
254 case QVariant::String:
261 serializeHash(hdf, value.
toHash(), key);
264 serializeMap(hdf, value.
toMap(), key);
274 #include "moc_clearsilver.cpp"
bool canConvert(int targetTypeId) const
QHash< QString, QVariant > toHash() const
bool error() const
Returns true if an error was set.
QByteArray render(Context *c) const final
QString & insert(int position, QChar ch)
int toInt(bool *ok) const
QByteArray & append(char ch)
void setTemplateExtension(const QString &extension)
Sets the template extension, defaults to ".html".
QString wrapper() const
Returns the template wrapper.
QByteArray toLatin1() const
QMap< QString, QVariant > toMap() const
QString templateExtension() const
Returns the template extension.
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
void stash(const QVariantHash &unite)