#include "Skylark.h" namespace Upp { Value Cycle(const Vector& arg, const Renderer *) { if(arg.GetCount() < 3 && !IsNumber(arg[0])) return String(); return arg[1 + int(arg[0]) % (arg.GetCount() - 1)]; } Value CountFn(const Vector& arg, const Renderer *) { return arg.GetCount() && IsValueArray(arg[0]) ? ValueArray(arg[0]).GetCount() : 0; } Value RawFn(const Vector& arg, const Renderer *) { RawHtmlText r; for(int i = 0; i < arg.GetCount(); i++) r.text.Cat(AsString(arg[i])); return RawToValue(r); } String GetIdentity(const Renderer *r) { // This ugly hack expects that __identity__ is always present in r->var Http *http = const_cast(dynamic_cast(r)); if(!http) throw Exc("invalid POST identity call"); String s = http->var[0]; if(s.GetCount()) return s; s = AsString(Uuid::Create()); http->SessionSet0("__identity__", s); http->var[0] = s; return s; } Value PostIdentity(const Vector&, const Renderer *r) { return Raw(""); } Value JsIdentity(const Vector&, const Renderer *r) { return Raw(""); } Value VariablesSet(const Vector&, const Renderer *r) { String html; if(r) { const VectorMap& set = r->Variables(); html << "\n"; for(int i = 0; i < set.GetCount(); i++) html << "" ; html << "
IDVALUE
" << EscapeHtml(set.GetKey(i)) << "" << EscapeHtml(AsString(set[i])) << "
"; } return Raw(html); } Value Render(const Vector& arg, const Renderer *r) { if (arg.GetCount() < 1) return ""; // create new Renderer, as we can not modify the one currently used Renderer rr; // copy variables so they are available in the rendered template too for(int i = 1; i < r->Variables().GetCount(); i++) { rr(r->Variables().GetKey(i), r->Variables()[i]); } // add arguments as variables for(int i = 1; i < arg.GetCount(); i++) { rr("_"+IntStr(i), arg[i]); } // render the template passed in first argument return rr.Render(AsString(arg[0])); } INITBLOCK { Compiler::Register("cycle", Cycle); Compiler::Register("raw", RawFn); Compiler::Register("count", CountFn); Compiler::Register("post_identity", PostIdentity); Compiler::Register("js_identity", JsIdentity); Compiler::Register("set", VariablesSet); Compiler::Register("render", Render); }; };