Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
injabridge.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef INJABRIDGE_H
5#define INJABRIDGE_H
6
7#include <QtCore/qlogging.h>
8
9// Override Inja's default error handling before including inja.hpp.
10// Inja's throw.hpp guards INJA_THROW with #ifndef, so defining it first
11// takes precedence. Without this override, -fno-exceptions causes a bare
12// std::abort() with no diagnostic output. With it, qFatal() logs the
13// error message (including source location) before terminating.
14#define INJA_THROW(exception)
15 qFatal("Inja template error: %s", (exception).what())
16
17#include <inja/inja.hpp>
18
19#include <QJsonObject>
20#include <QJsonArray>
21#include <QJsonValue>
22#include <QString>
23
24#include <functional>
25
26QT_BEGIN_NAMESPACE
27
28class InjaBridge
29{
30public:
31 using IncludeCallback = std::function<QString(const QString &name)>;
32
33 static nlohmann::json toInjaJson(const QJsonValue &value);
34 static nlohmann::json toInjaJson(const QJsonObject &obj);
35 static nlohmann::json toInjaJson(const QJsonArray &array);
36
37 static QString render(const QString &templateStr, const QJsonObject &data);
38 static QString render(const QString &templateStr, const QJsonObject &data,
39 const IncludeCallback &includeCallback);
40 static QString renderFile(const QString &templatePath, const QJsonObject &data);
41
42private:
43 InjaBridge() = default;
44};
45
46QT_END_NAMESPACE
47
48#endif // INJABRIDGE_H
static QT_BEGIN_NAMESPACE void registerCallbacks(inja::Environment &env)
#define INJA_THROW(exception)
Definition injabridge.h:14