5 #include "clearsilver_p.h"
13 #include <QtCore/QLoggingCategory>
15 Q_LOGGING_CATEGORY(CUTELYST_CLEARSILVER,
"cutelyst.clearsilver", QtWarningMsg)
24 QStringList ClearSilver::includePaths()
const
27 return d->includePaths;
33 d->includePaths = paths;
37 QString ClearSilver::templateExtension()
const
46 d->extension = extension;
50 QString ClearSilver::wrapper()
const
63 NEOERR* cutelyst_render(
void *user,
char *data)
65 QByteArray *body =
static_cast<QByteArray*
>(user);
78 const QVariantHash &stash = c->
stash();
79 QString templateFile = stash.value(QStringLiteral(
"template")).toString();
80 if (templateFile.isEmpty()) {
81 if (c->action() && !c->action()->
reverse().isEmpty()) {
82 templateFile = c->action()->
reverse() + d->extension;
85 if (templateFile.isEmpty()) {
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) {
122 QFile file(includePath + QLatin1Char(
'/') + QString::fromLatin1(filename));
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);
150 errorMsg = QStringLiteral(
"Failed to init ClearSilver:\n+1").arg(QString::fromLatin1(msg.buf, msg.len));
151 renderError(c, errorMsg);
159 cs_register_fileload(cs,
const_cast<ClearSilverPrivate*
>(
this), findFile);
161 error = cs_parse_file(cs, filename.toLatin1().data());
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);
198 const QMetaObject *meta = c->metaObject();
199 for (
int i = 0; i < meta->propertyCount(); ++i) {
200 QMetaProperty prop = meta->property(i);
201 QString name = QLatin1String(
"c.") + QString::fromLatin1(prop.name());
202 QVariant value = prop.read(c);
203 serializeVariant(hdf, value, name);
208 void ClearSilverPrivate::serializeHash(HDF *hdf,
const QVariantHash &hash,
const QString &prefix)
const
211 if (!prefix.isEmpty()) {
212 _prefix = prefix + QLatin1Char(
'.');
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
225 if (!prefix.isEmpty()) {
226 _prefix = prefix + QLatin1Char(
'.');
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"
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.
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
void stash(const QVariantHash &unite)
Response * res() const noexcept
bool error() const noexcept
Returns true if an error was set.
void setBody(QIODevice *body)
Cutelyst View abstract view component
The Cutelyst namespace holds all public Cutelyst API.