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); }
QString TemplateLayout::generate() { int progress = 0; int totalWork = getTotalWork(PrintOptions); QString templateName; QString htmlContent; m_engine = new Grantlee::Engine(this); QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader()); m_templateLoader->setTemplateDirs(QStringList() << getSubsurfaceDataPath("printing_templates")); m_engine->addTemplateLoader(m_templateLoader); Grantlee::registerMetaType<Dive>(); Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<print_options>(); QVariantHash mapping; QVariantList diveList; struct dive *dive; int i; for_each_dive (i, dive) { //TODO check for exporting selected dives only if (!dive->selected && PrintOptions->print_selected) continue; Dive d(dive); diveList.append(QVariant::fromValue(d)); progress++; emit progressUpdated(progress * 100.0 / totalWork); } mapping.insert("dives", diveList); mapping.insert("template_options", QVariant::fromValue(*templateOptions)); mapping.insert("print_options", QVariant::fromValue(*PrintOptions)); Grantlee::Context c(mapping); if (PrintOptions->p_template == print_options::ONE_DIVE) { templateName = "one_dive.html"; } else if (PrintOptions->p_template == print_options::TWO_DIVE) { templateName = "two_dives.html"; } else if (PrintOptions->p_template == print_options::CUSTOM) { templateName = "custom.html"; } Grantlee::Template t = m_engine->loadByName(templateName); if (!t || t->error()) { qDebug() << "Can't load template"; return htmlContent; } htmlContent = t->render(&c); if (t->error()) { qDebug() << "Can't render template"; return htmlContent; } return htmlContent; }
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; } }
QString TemplateLayout::generate() { int progress = 0; int totalWork = getTotalWork(PrintOptions); QString htmlContent; delete m_engine; m_engine = new Grantlee::Engine(this); QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader()); m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser()); m_engine->addTemplateLoader(m_templateLoader); Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<print_options>(); QVariantList diveList; struct dive *dive; int i; for_each_dive (i, dive) { //TODO check for exporting selected dives only if (!dive->selected && PrintOptions->print_selected) continue; DiveObjectHelper *d = new DiveObjectHelper(dive); diveList.append(QVariant::fromValue(d)); progress++; emit progressUpdated(progress * 100.0 / totalWork); } Grantlee::Context c; c.insert("dives", diveList); c.insert("template_options", QVariant::fromValue(*templateOptions)); c.insert("print_options", QVariant::fromValue(*PrintOptions)); Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); if (!t || t->error()) { qDebug() << "Can't load template"; return htmlContent; } htmlContent = t->render(&c); if (t->error()) { qDebug() << "Can't render template"; } return htmlContent; }
QString TemplateLayout::generate() { int progress = 0; int totalWork = getTotalWork(PrintOptions); QString htmlContent; delete m_engine; m_engine = new Grantlee::Engine(this); Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<print_options>(); QVariantList diveList; struct dive *dive; int i; for_each_dive (i, dive) { //TODO check for exporting selected dives only if (!dive->selected && PrintOptions->print_selected) continue; DiveObjectHelper *d = new DiveObjectHelper(dive); diveList.append(QVariant::fromValue(d)); progress++; emit progressUpdated(lrint(progress * 100.0 / totalWork)); } Grantlee::Context c; c.insert("dives", diveList); c.insert("template_options", QVariant::fromValue(*templateOptions)); c.insert("print_options", QVariant::fromValue(*PrintOptions)); /* don't use the Grantlee loader API */ QString templateContents = readTemplate(PrintOptions->p_template); QString preprocessed = preprocessTemplate(templateContents); /* create the template from QString; is this thing allocating memory? */ Grantlee::Template t = m_engine->newTemplate(preprocessed, PrintOptions->p_template); if (!t || t->error()) { qDebug() << "Can't load template"; return htmlContent; } htmlContent = t->render(&c); if (t->error()) { qDebug() << "Can't render template"; } return htmlContent; }
QString TemplateLayout::generateStatistics() { QString htmlContent; delete m_engine; m_engine = new Grantlee::Engine(this); QSharedPointer<Grantlee::FileSystemTemplateLoader> m_templateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader()); m_templateLoader->setTemplateDirs(QStringList() << getPrintingTemplatePathUser() + QDir::separator() + QString("statistics")); m_engine->addTemplateLoader(m_templateLoader); Grantlee::registerMetaType<YearInfo>(); Grantlee::registerMetaType<template_options>(); Grantlee::registerMetaType<print_options>(); QVariantList years; int i = 0; while (stats_yearly != NULL && stats_yearly[i].period) { YearInfo year(stats_yearly[i]); years.append(QVariant::fromValue(year)); i++; } Grantlee::Context c; c.insert("years", years); c.insert("template_options", QVariant::fromValue(*templateOptions)); c.insert("print_options", QVariant::fromValue(*PrintOptions)); Grantlee::Template t = m_engine->loadByName(PrintOptions->p_template); if (!t || t->error()) { qDebug() << "Can't load template"; return htmlContent; } htmlContent = t->render(&c); if (t->error()) { qDebug() << "Can't render template"; return htmlContent; } emit progressUpdated(100); return htmlContent; }
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); }