bool WTemplate::_while(const std::vector<WString>& args, std::ostream& result) { if (args.size() < 2) return false; WString tblock = WString::tr(args[1].toUTF8()); while (conditionValue(args[0].toUTF8())) this->renderTemplateText(result, tblock); return true; }
void WTemplate::setCondition(const std::string& name, bool value) { if (conditionValue(name) != value) { if (value) conditions_.insert(name); else conditions_.erase(name); changed_ = true; repaint(RepaintInnerHtml); } }
void RegistrationWidget::update() { if (model_->passwordAuth()) bindString("password-description", tr("Wt.Auth.password-registration")); else bindEmpty("password-description"); updateView(model_); if (!created_) { WLineEdit *password = resolve<WLineEdit *> (RegistrationModel::ChoosePasswordField); WLineEdit *password2 = resolve<WLineEdit *> (RegistrationModel::RepeatPasswordField); WText *password2Info = resolve<WText *> (RegistrationModel::RepeatPasswordField + std::string("-info")); if (password && password2 && password2Info) model_->validatePasswordsMatchJS(password, password2, password2Info); } WAnchor *isYou = resolve<WAnchor *>("confirm-is-you"); if (!isYou) { isYou = new WAnchor(std::string("#"), tr("Wt.Auth.confirm-is-you")); isYou->hide(); bindWidget("confirm-is-you", isYou); } if (model_->isConfirmUserButtonVisible()) { if (!isYou->clicked().isConnected()) isYou->clicked().connect(this, &RegistrationWidget::confirmIsYou); isYou->show(); } else isYou->hide(); if (model_->isFederatedLoginVisible()) { if (!conditionValue("if:oauth")) { setCondition("if:oauth", true); if (model_->passwordAuth()) bindString("oauth-description", tr("Wt.Auth.or-oauth-registration")); else bindString("oauth-description", tr("Wt.Auth.oauth-registration")); WContainerWidget *icons = new WContainerWidget(); icons->addStyleClass("Wt-field"); for (unsigned i = 0; i < model_->oAuth().size(); ++i) { const OAuthService *service = model_->oAuth()[i]; WImage *w = new WImage("css/oauth-" + service->name() + ".png", icons); w->setToolTip(service->description()); w->setStyleClass("Wt-auth-icon"); w->setVerticalAlignment(AlignMiddle); OAuthProcess *const process = service->createProcess(service->authenticationScope()); w->clicked().connect(process, &OAuthProcess::startAuthenticate); process->authenticated().connect (boost::bind(&RegistrationWidget::oAuthDone, this, process, _1)); WObject::addChild(process); } bindWidget("icons", icons); } } else { setCondition("if:oauth", false); bindEmpty("icons"); } if (!created_) { WPushButton *okButton = new WPushButton(tr("Wt.Auth.register")); WPushButton *cancelButton = new WPushButton(tr("Wt.WMessageBox.Cancel")); bindWidget("ok-button", okButton); bindWidget("cancel-button", cancelButton); okButton->clicked().connect(this, &RegistrationWidget::doRegister); cancelButton->clicked().connect(this, &RegistrationWidget::close); created_ = true; } }
void WTemplate::renderTemplateText(std::ostream& result, const WString& templateText) { std::string text; WApplication *app = WApplication::instance(); if (app && (encodeInternalPaths_ || app->session()->hasSessionIdInUrl())) { WFlags<RefEncoderOption> options; if (encodeInternalPaths_) options |= EncodeInternalPaths; if (app->session()->hasSessionIdInUrl()) options |= EncodeRedirectTrampoline; WString t = templateText; EncodeRefs(t, options); text = t.toUTF8(); } else text = templateText.toUTF8(); std::size_t lastPos = 0; std::vector<WString> args; std::vector<std::string> conditions; int suppressing = 0; for (std::size_t pos = text.find('$'); pos != std::string::npos; pos = text.find('$', pos)) { if (!suppressing) result << text.substr(lastPos, pos - lastPos); lastPos = pos; if (pos + 1 < text.length()) { if (text[pos + 1] == '$') { // $$ -> $ if (!suppressing) result << '$'; lastPos += 2; } else if (text[pos + 1] == '{') { std::size_t startName = pos + 2; std::size_t endName = text.find_first_of(" \r\n\t}", startName); args.clear(); std::size_t endVar = parseArgs(text, endName, args); if (endVar == std::string::npos) { LOG_ERROR("variable syntax error near \"" << text.substr(pos) << "\""); return; } std::string name = text.substr(startName, endName - startName); std::size_t nl = name.length(); if (nl > 2 && name[0] == '<' && name[nl - 1] == '>') { if (name[1] != '/') { std::string cond = name.substr(1, nl - 2); conditions.push_back(cond); if (suppressing || !conditionValue(cond)) ++suppressing; } else { std::string cond = name.substr(2, nl - 3); if (conditions.back() != cond) { LOG_ERROR("mismatching condition block end: " << cond); return; } conditions.pop_back(); if (suppressing) --suppressing; } } else { if (!suppressing) { std::size_t colonPos = name.find(':'); bool handled = false; if (colonPos != std::string::npos) { std::string fname = name.substr(0, colonPos); std::string arg0 = name.substr(colonPos + 1); args.insert(args.begin(), WString::fromUTF8(arg0)); if (resolveFunction(fname, args, result)) handled = true; else args.erase(args.begin()); } if (!handled) resolveString(name, args, result); } } lastPos = endVar + 1; } else { if (!suppressing) result << '$'; // $. -> $. lastPos += 1; } } else { if (!suppressing) result << '$'; // $ at end of template -> $ lastPos += 1; } pos = lastPos; } result << text.substr(lastPos); }
bool WTemplate::renderTemplateText(std::ostream& result, const WString& templateText) { errorText_ = ""; std::string text; if (encodeTemplateText_) text = encode(templateText.toUTF8()); else text = templateText.toUTF8(); std::size_t lastPos = 0; std::vector<WString> args; std::vector<std::string> conditions; int suppressing = 0; for (std::size_t pos = text.find('$'); pos != std::string::npos; pos = text.find('$', pos)) { if (!suppressing) result << text.substr(lastPos, pos - lastPos); lastPos = pos; if (pos + 1 < text.length()) { if (text[pos + 1] == '$') { // $$ -> $ if (!suppressing) result << '$'; lastPos += 2; } else if (text[pos + 1] == '{') { std::size_t startName = pos + 2; std::size_t endName = text.find_first_of(" \r\n\t}", startName); args.clear(); std::size_t endVar = parseArgs(text, endName, args); if (endVar == std::string::npos) { std::stringstream errorStream; errorStream << "variable syntax error near \"" << text.substr(pos) << "\""; errorText_ = errorStream.str(); LOG_ERROR(errorText_); return false; } std::string name = text.substr(startName, endName - startName); std::size_t nl = name.length(); if (nl > 2 && name[0] == '<' && name[nl - 1] == '>') { if (name[1] != '/') { std::string cond = name.substr(1, nl - 2); conditions.push_back(cond); if (suppressing || !conditionValue(cond)) ++suppressing; } else { std::string cond = name.substr(2, nl - 3); if (conditions.empty() || conditions.back() != cond) { std::stringstream errorStream; errorStream << "mismatching condition block end: " << cond; errorText_ = errorStream.str(); LOG_ERROR(errorText_); return false; } conditions.pop_back(); if (suppressing) --suppressing; } } else { if (!suppressing) { std::size_t colonPos = name.find(':'); bool handled = false; if (colonPos != std::string::npos) { std::string fname = name.substr(0, colonPos); std::string arg0 = name.substr(colonPos + 1); args.insert(args.begin(), WString::fromUTF8(arg0)); if (resolveFunction(fname, args, result)) handled = true; else args.erase(args.begin()); } if (!handled) resolveString(name, args, result); } } lastPos = endVar + 1; } else { if (!suppressing) result << '$'; // $. -> $. lastPos += 1; } } else { if (!suppressing) result << '$'; // $ at end of template -> $ lastPos += 1; } pos = lastPos; } result << text.substr(lastPos); return true; }