QString TemplateEngine::render(const string& file) { Grantlee::Template t = m_engine->loadByName(QString::fromStdString(file)); if (!t) { qDebug() << "Unable to load template" << QString("Error loading template: %1").arg(QString::fromStdString(file)); return QString(); } if (t->error()) { qDebug() << "Unable to load template" << QString("Error loading template: %1").arg(t->errorString()); return QString(); } QString content = t->render(&c); if (t->error()) { qDebug() << "Unable render template" << QString("Error rendering template: %1").arg(t->errorString()); return QString(); } content.replace("\n\n", "\n"); LogDebug << content.toStdString(); return content; }
void BookWindow::renderBooks() const { int rows = model->rowCount(); QVariantHash mapping; QVariantList bookList; for (int row = 0; row < rows; ++row) { QString title = model->index(row, 1).data().toString(); QString author = model->index(row, 2).data().toString(); QString genre = model->index(row, 3).data().toString(); int rating = model->index(row, 5).data().toInt(); QObject *book = new BookWrapper(author, title, genre, rating, this); QVariant var = QVariant::fromValue(book); bookList.append(var); } mapping.insert("books", bookList); QString themeName = ui.exportTheme->currentText(); Grantlee::Context c(mapping); Grantlee::Template t = m_engine->loadByName( themeName + ".html" ); if (!t) { QMessageBox::critical(this, "Unable to load template", QString( "Error loading template: %1" ).arg( themeName + ".html" ) ); return; } if ( t->error() ) { QMessageBox::critical(this, "Unable to load template", QString( "Error loading template: %1" ).arg( t->errorString() ) ); return; } bool ok; QString text = QInputDialog::getText(this, tr("Export Location"), tr("file name:"), QLineEdit::Normal, QDir::home().absolutePath() + "/book_export.html", &ok); if (!ok || text.isEmpty()) return; QFile file( text ); if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) ) return; QString content = t->render(&c); if ( t->error() ) { QMessageBox::critical(this, "Unable render template", QString( "Error rendering template: %1" ).arg( t->errorString() ) ); return; } file.write(content.toLocal8Bit()); file.close(); }
void MainWindow::generateOutput() { m_tabWidget->clear(); QString outputType = m_designWidget->outputType(); m_loader->setTheme(outputType); if (outputType == "cpp") return generateCpp(); Grantlee::Template classTemplate = m_engine->loadByName("class." + outputType); if (classTemplate->error()) { createOutputTab("Class", classTemplate->errorString()); return; } Grantlee::Context c = m_designWidget->getContext(); QString output; QTextStream textStream(&output); NoEscapeOutputStream stream(&textStream); classTemplate->render(&stream, &c); createOutputTab("Class", classTemplate->error() ? classTemplate->errorString() : output); }
Grantlee::Template Templates::getTemplate(const QString& name) { Grantlee::Template t = self()->engine->loadByName(name); qDebug() << name << t->nodeList().size(); if (t->error() != Grantlee::NoError) { qDebug() << t->errorString(); } return t; }
static void renderTemplate( Grantlee::Engine *engine, const QString &templateName ) { Grantlee::Template t = engine->loadByName( templateName ); if ( t->error() ) { qDebug() << t->errorString(); Q_ASSERT( !"Failed to get template" ); } Grantlee::Context c; QString result = t->render( &c ); if ( t->error() ) { qDebug() << t->errorString(); Q_ASSERT( !"Failed to render template" ); } qDebug() << result; }
void PublicServerSystem::Web::View::render(const QString &templateName, QTextStream &stream, Grantlee::Engine *templateEngine, Grantlee::Context *requestContext) { Grantlee::OutputStream out(&stream); Grantlee::Template tem = templateEngine->loadByName(templateName); if ( tem->error() != Grantlee::NoError ) { qDebug() << tem->errorString(); } tem->render(&out, requestContext); }
void MainWindow::generateCpp() { Grantlee::Template headerTemplate = m_engine->loadByName("header.h"); if (headerTemplate->error()) { createOutputTab("Header", headerTemplate->errorString()); return; } Grantlee::Context c = m_designWidget->getContext(); QString output; QTextStream textStream(&output); NoEscapeOutputStream stream(&textStream); headerTemplate->render(&stream, &c); createOutputTab("Header", headerTemplate->error() ? headerTemplate->errorString() : output); if (headerTemplate->error()) return; output.clear(); Grantlee::Template implementationTemplate = m_engine->loadByName("implementation.cpp"); if (implementationTemplate->error()) { createOutputTab("Implementation", implementationTemplate->errorString()); return; } implementationTemplate->render(&stream, &c); createOutputTab("Implementation", implementationTemplate->error() ? implementationTemplate->errorString() : output); if (implementationTemplate->error()) return; output.clear(); if (c.lookup("pimpl").toBool()) { Grantlee::Template privateHeaderTemplate = m_engine->loadByName("private_header.h"); if (privateHeaderTemplate->error()) { createOutputTab("Private Header", privateHeaderTemplate->errorString()); return; } c.insert("className", Grantlee::getSafeString(c.lookup("className")) + QString("Private")); c.insert("baseClass", QVariant()); privateHeaderTemplate->render(&stream, &c); createOutputTab("Private Header", privateHeaderTemplate->error() ? privateHeaderTemplate->errorString() : output); if (privateHeaderTemplate->error()) return; } }