ultimatepp/uppsrc/Skylark/StdLib.icpp
cxl bf417f02cc cosmetics (thanks SenderGhost)
git-svn-id: svn://ultimatepp.org/upp/trunk@8889 f0d560ea-af0d-0410-9eb7-867de7ffcac7
2015-09-04 06:56:08 +00:00

95 lines
2.6 KiB
Text

#include "Skylark.h"
namespace Upp {
Value Cycle(const Vector<Value>& 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<Value>& arg, const Renderer *)
{
return arg.GetCount() && IsValueArray(arg[0]) ? ValueArray(arg[0]).GetCount() : 0;
}
Value RawFn(const Vector<Value>& 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<Http *>(dynamic_cast<const Http *>(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<Value>&, const Renderer *r)
{
return Raw("<input type=\"hidden\" name=\"__post_identity__\" value=\"" + GetIdentity(r) + "\">");
}
Value JsIdentity(const Vector<Value>&, const Renderer *r)
{
return Raw("<script type=\"text/javascript\">var __js_identity__ = \"" + GetIdentity(r) + "\"</script>");
}
Value VariablesSet(const Vector<Value>&, const Renderer *r)
{
String html;
if(r) {
const VectorMap<String, Value>& set = r->Variables();
html << "<table border='1'><tr><th>ID</th><th>VALUE</th></tr>\n";
for(int i = 0; i < set.GetCount(); i++)
html << "<tr><td>"
<< EscapeHtml(set.GetKey(i))
<< "</td><td>"
<< EscapeHtml(AsString(set[i]))
<< "</td></tr>"
;
html << "</table>";
}
return Raw(html);
}
Value Render(const Vector<Value>& 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);
};
};