関数
Clearsilverはいくつかの組み込み関数を持っています。
これらの関数は、引数のアクセスと操作が可能になります。
すべての関数は、文字列または数値を返します。
関数は、変数が使用される式でどこでも使用することができる。
Function | Arguments | Description |
---|---|---|
subcount(var) | An HDF variable | HDF変数の子ノードの数を返す |
name(local) | A local variable | ローカル変数の別名のHDF変数名を返す |
first(local) | A local variable | ループ最初のlocal変数の場合、trueを返す |
last(local) | A local variable | ループ最後のlocal変数の場合、trueを返す |
abs(expr) | A numeric expression | 数値式の絶対値を返す |
max(expr, expr) | Two numeric expressions | 二つの数値式の大きい方を返す |
min(expr, expr) | Two numeric expressions | 二つの数値式の小さい方を返す |
string.slice(expr, start, end) | A string expression, and two numeric expressions | start で指定された位置からendで指定された位置までのスライスした文字列を返す。Pythonのsliceに似ている |
string.find(string, substr) | Two string expressions | stringで指定した文字列からsubstrを探し、見つかればその位置を返し、見つからなければ-1を返す。Pythonのfindに似ている |
string.length(expr) | A string expression | 文字列数を返す |
_(expr) | A string expression | gettextを利用可能でコンパイルした場合、gettextが返す翻訳された文字列を返す |
テンプレートフィルター
Clearsilver APIは、ユーザーが組み込み関数に文字列操作関数を追加することができます。
これらの関数はただ1つの文字列引数を取り、文字列を返すことができます。
Clearsilver CGIキットはClearsilverに追加されたいくつかのWeb特有のフィルタを持っています。
これらのフィルタは、cgi_register_strfuncs()関数を経由してC言語で追加することができ、言語ラッパーのほとんどのCS層にデフォルトで含まれています。
url_escape | これはURLエスケープします。?と&と=をURLセーフな %hh 形式に変換します。 |
html_escape | これはHTMLをエスケープします。>と<と&をHTMLセーフな>、<、& に変換します。 |
js_escape | Javascriptの文字列に配置するためのJavaScriptエスケープします。 “と’と\を\”と\’と\\に変換します。 |
text_html | This pretty-formats normal text into an HTML fragment, attempting to detect paragraph boundaries and allowing it to wrap reasonably. |
html_strip | This removes all HTML tags and then converts any & based HTML escaped data into normal text. Combine this with html_escape() if you would like to strip the HTML tags from text and display the result in an HTML safe way. |
url_validate | Function to validate a URL for protecting against XSS. Ensures that the URL is a relative URL or an absolute url with a safe scheme (currently http, https, ftp or mailto). This is to avoid dangerous schemes like javascript. It then HTML escapes the URL. An unsafe URL is replaced by ‘#’. |
css_url_validate | Similar to url_validate except it escapes the URL for use in CSS. |
null_escape | This escape function just outputs the given string as is. The auto-escape system assumes that anything explicitly escaped is correctly escaped. |
これらのフィルタは、式の中でどこでも使用することができます。
これは、URLを構成するか、HTMLの安全のためにデータを強制するため、それらが極めて有効になります。
ここではいくつかの例を示します。
<?cs var:html_escape(Page.Title) ?> <?cs set:url = "http://www.google.com/q=" + url_escape(Query.q) ?> <IMG onclick="handleClick('<?cs var:js_escape(url)')" SRC="foo.gif"> <A HREF="/newurl?_done=<?cs var:url_escape(url) ?>">click here</A>