LoginSignup
4
3

More than 5 years have passed since last update.

Perl Sledge TemplateToolkit

Posted at

最近業務で Perl の Sledge というフレームワークと Template Tool Kit を使い始めたのですが、まぁ全然わからないコトわからないコト。
Sledgeはドキュメントが無いよって言われて、私はいったいどうすればいいんだ!?って感じで1ヶ月View書いて過ごしました。
viewで使う、TT2はもまた独特の表記で、あーーーRubyしたい、PHPしたいって思うばかりです。。。
毎日勉強ですねぇε=(ノ゚д゚)ノ

本日ずっと謎だけど放置していた内容がわかりました。
TT2の表記の問題。

[% aaaa %]
と
[%- aaaa -%]
などの違い。。。

案外簡単でした。
ていうか、フツーに公式ドキュメントに書いてあった。
(((( ´,,_ゝ`)))) ププッ プルプルッ

Chomping Whitespace
You can add - or + to the immediate start or end of a directive tag to control the whitespace chomping options. See the PRE_CHOMP and POST_CHOMP options for further details.
[% BLOCK foo -%] # remove trailing newline
This is block foo
[%- END %] # remove leading newline

(´Д`)ハァ…
なにをいうかお主、、、ということで、PRE_CHOMPとPOST_CHOMPを見てみる。

ちなみにChompとはコレ。
http://www.perlplus.jp/func/string/index10.html
(要するに改行)

抜粋

PRE_CHOMP, POST_CHOMP
Anything outside a directive tag is considered plain text and is generally passed through unaltered (but see the INTERPOLATE option). This includes all whitespace and newlines characters surrounding directive tags. Directives that don't generate any output will leave gaps in the output document.
Example:

Foo
[% a = 10 %]
Bar
Output:

Foo

Bar

The PRE_CHOMP and POST_CHOMP options can help to clean up some of this extraneous whitespace. Both are disabled by default.

With PRE_CHOMP set to 1, the newline and whitespace preceding a directive at the start of a line will be deleted. This has the effect of concatenating a line that starts with a directive onto the end of the previous line.

    Foo <----------.
                   |
,---(PRE_CHOMP)----'
|
`-- [% a = 10 %] --.
                   |
,---(POST_CHOMP)---'
|
`-> Bar

With POST_CHOMP set to 1, any whitespace after a directive up to and including the newline will be deleted. This has the effect of joining a line that ends with a directive onto the start of the next line.
If PRE_CHOMP or POST_CHOMP is set to 2, all whitespace including any number of newline will be removed and replaced with a single space. This is useful for HTML, where (usually) a contiguous block of whitespace is rendered the same as a single space.
With PRE_CHOMP or POST_CHOMP set to 3, all adjacent whitespace (including newlines) will be removed entirely.
These values are defined as CHOMP_NONE, CHOMP_ONE, CHOMP_COLLAPSE and CHOMP_GREEDY constants in the Template::Constants module. CHOMP_ALL is also defined as an alias for CHOMP_ONE to provide backwards compatability with earlier version of the Template Toolkit.
まぁ要するに、

これと、
[% FOREACH user IN userlist %][%- user -%][% END %]
これは一緒のことを意味している、ということ。
[% FOREACH user IN userlist %][% user %][% END %]

コード上の空白や改行を、読み込み時に無視させることが出来るという事ですね。

これらは結構、好きなように設定が可能なようです。

[%= if aaa =%]
とか
[%~ if aaa ~%]
Constant      Value   Tag Modifier
----------------------------------
CHOMP_NONE      0          +
CHOMP_ONE       1          -
CHOMP_COLLAPSE  2          =
CHOMP_GREEDY    3          ~

PRE_CHOMPとPOST_CHOMPを切り替えることも出来るようです。

my $template = Template->new({
   PRE_CHOMP  => '~',
   POST_CHOMP => '-',
});

To collapse all whitespace to a single space, use the '=' character.
[% FOREACH user IN userlist %][%= user =%][% END %]Here the template is processed as if written:
[% FOREACH user IN userlist %][% user %][% END %]
また、+を使って、Configでchomp設定しているものを、打ち消すことも出来るようです。

これが、
[% FOREACH user IN userlist %]
User: [% user +%][% END %]

これと同じ様になるという事ですね。
[% FOREACH user IN userlist %]User: [% user %][% END %]
打ち消し版です。
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3