Hi folks,
I have released new version of Kwartz.
Kwartz is a template system for Ruby, PHP and Java.
It has the following features:
- Separate presentation logic from HTML file.
- Runs Very fast.
- Doesn’t break HTML design at all.
- Auto-sanitizing.
Download:
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-19.tar.gz
Homepage:
http://www.kuwata-lab.com/webtech/kwartz/
Current release has several significant enhancements.
-
Id attribute has now ability equal to kd attribute.
You can use id attribute instead of kd attribute.
In addition, you can use both id and kd attribute in a tag.
-
id=“name” is now recognized as id=“mark:name”.
It means that id=“name” is recognized as a marking directive.
-
New directive notation supported.
id=“attr:name=value” / id="foreach:item=list"
are able to be described as
id=“attr:name:value” / id="foreach:item:list"
which are more suitable for HTML specification.
-
New keyword ‘empty’ supported in interemdiate language.
:if (str == empty) … / :if (str != empty) …
are equal to
:if (str==null || str==’’) … / :if (str!=null && str!=’’) …
-
New command action ‘analyze’ supported.
Action ‘analyze’ analyzes template and report global/local variables.
···
=====================
$ kwartz -a analyze -p file.plogic file.html
global variables: username item_list
local variables: item item_ctr
=====================
-
New directive ‘replace’ supported.
…
is replaced by other element which is marked as ‘foo’.
-
New command opton ‘–enable_eruby=true’ supported,
which enables you to write native codes of several languages
in a template:
--------------------
### presentation logic
% if lang == 'ruby’
s = ‘%03d’ % i
% elsif lang == ‘php’
<?php $s = sprintf('%03d', $i); ?>
% else
:set(s = (i >= 10 ? ‘0’ : ‘00’) .+ i)
% end
:print(s)
--------------------
See Kwartz User’s Guide for more detail.
–
regards,
kwa
Hi,
I would like to use Kwartz for my webforum, and I have a few
questions/suggestions:
-
#{username}#
results in
if logged_in then
print username, "\n"
end
But according to
http://www.kuwata-lab.com/webtech/kwartz/users-guide.en.03.html#directive-if
the result should look like this:
if logged_in then
print "<span id=“if:logged_in”>\n"
print username, "\n"
print "\n"
end
-
IMO something like this would be much cleaner than eval()'ing the code
generated by kwarts, because the ruby file has to be parsed only once,
and it isn’t always wanted to send the output to STDOUT.
template = Kwartz.new(File.read(‘template.html’))
string = template.expand(data)
-
ruby output could also support sanitizing (CGI::escapeHTML).
Thanks
Andreas
Andreas,
Thanks for your suggestions.
Andreas wrote:
#{username}#
results in
if logged_in then
print username, “\n”
end
But according to
http://www.kuwata-lab.com/webtech/kwartz/users-guide.en.03.html#directive-if
the result should look like this:
if logged_in then
print “<span id="if:logged_in">\n”
print username, “\n”
print “\n”
end
Tag or
are deleted if they have only directives.
If you don’t want to delete them, use
or
,
or add other attributes like
.
See
http://www.kuwata-lab.com/webtech/kwartz/users-guide.en.03.html#directive-notes
IMO something like this would be much cleaner than eval()'ing the code
generated by kwarts, because the ruby file has to be parsed only once,
and it isn’t always wanted to send the output to STDOUT.
template = Kwartz.new(File.read(‘template.html’))
string = template.expand(data)
You should use ERB if you want to get result as string.
···
s = File.open(‘file.eruby’) { |f| f.read() }
require ‘erb’
erb = ERB.new(s) # or ERB.new(s, $SAFE, ‘%’)
str = erb.result(binding())
And executing the output script is enough fast.
See
http://www.io.com/~egabriel/bench/results.html
The benchmark shows that eruby (= ERuby::import()) is faster than
any other template system in Ruby.
(ERB works almost as fast as eruby.)
It is one of the reasons why I adopted the current way in Kwartz.
If you want to get more speed, try erbscan.
http://raa.ruby-lang.org/list.rhtml?name=erbscan
I have not tried it yet, but I heared that erbscan makes ERB very very fast.
ruby output could also support sanitizing (CGI::escapeHTML).
Yes, you are right.
I do that in the future release.
–
regards,
kwa
Makoto Kuwata wrote:
Tag or
are deleted if they have only directives.
If you don’t want to delete them, use or ,
or add other attributes like
.
OK, this makes sense.
Another question:
I have some if/elsif code at the top of my plogic file that sets a few
boolean variables to determine which parts of the template have to be
displayed. But the problem is that this code is moved to the bottom of
the resulting erb file, where it has no effect.
I call kwartz with:
kwartz -l erb -p template.plogic template.html > template.erb
Makoto Kuwata wrote:
Tag or
are deleted if they have only directives.
If you don’t want to delete them, use or ,
or add other attributes like
.
It seems that
gets removed, too, even if there
isn’t an id attribute. And IMO it is questionable to remove any
s
at all, as they affect the layout off the page. At least removing tags
should be disabled by default, if you want to follow the “principle of
least surprise”.
Andreas
Andreas Schwarz wrote:
It seems that
gets removed, too, even if there
isn’t an id attribute.
It seems to be a bug of Kwartz.
I can’t reproduce it. Could you give me a sample of the bug?
And IMO it is questionable to remove any
s
at all, as they affect the layout off the page. At least removing tags
should be disabled by default, if you want to follow the “principle of
least surprise”.
I’ll examine your suggestion. Thank you.
···
–
regards,
kwa
I have just released a new version of Kwartz.
.* Ruby sanitizing supported
.* Special macro BEGIN and END supported.
They are equivalent to BEGIN and END of awk.
Download:
http://www.kuwata-lab.com/webtech/kwartz/
···
–
regards,
kwa
Andreas,
Another question:
I have some if/elsif code at the top of my plogic file that sets a few
boolean variables to determine which parts of the template have to be
displayed. But the problem is that this code is moved to the bottom of
the resulting erb file, where it has no effect.
I call kwartz with:
kwartz -l erb -p template.plogic template.html > template.erb
How about this?
template.html:
....
--------------------
template.plogic:
:elem(document)
:set(var = condition ? value1 : value2) ## or if/else statement
@stag
@cont
@etag
:end
It is supposed that only :elem() or :macro statments should be
appeared in presentation logic file in Kwartz.
I may add special macros something like BEGIN/END of awk.
–
regards,
kwa
Makoto Kuwata wrote:
Andreas Schwarz wrote:
It seems that
gets removed, too, even if there
isn’t an id attribute.
It seems to be a bug of Kwartz.
I can’t reproduce it. Could you give me a sample of the bug?
kwartz: 0.19 (2004-03-19 10:34:03+09),
kwartz.rb: 0.76 (2004-03-19 10:24:33+09)
Makoto Kuwata wrote:
I have just released a new version of Kwartz.
- Ruby sanitizing supported
A question about sanitizing in general: is it possible to disable
sanitizing for certain variables in the template?
Andreas Schwarz wrote:
kwartz: 0.19 (2004-03-19 10:34:03+09),
kwartz.rb: 0.76 (2004-03-19 10:24:33+09)
Thanks. I reproduced. (It is a bug around nested tags.)
And I just fixed it.
Please download the new version.
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-21.tar.gz
···
–
regards,
kwa
Andreas Schwarz wrote:
A question about sanitizing in general: is it possible to disable
sanitizing for certain variables in the template?
No. Kwartz doen’t have such feature.
I want to implement it (because I also want it), but it is a bit difficult,
because information about whether sanitizing or not sanitizing is lost
when converting HTML file into intermediate code.
For example, suppose that ‘@{…}@’ is a notation to disable sanitizing.
Presentation data:
···
Hello #{user}# ! # print with sanitizing
Hello @{user}@ ! # print without sanitizing
Intermediate code:
:print('Hello ‘, user, ’ !\n’) # sanitizing or not sanitizing?
:print('Hello ‘, user, ’ !\n’) # sanitizing or not sanitizing?
To get the request, I must implement another print statement
such as :print2() in intermediate language.
And I must implement id=“Value:…” and id=“Attr:…” which are
converted into :print2() instead of :print().
I may imprement :print2() statement in the future.
And IMO it is questionable to remove any
s
at all, as they affect the layout off the page. At least removing tags
should be disabled by default, if you want to follow the “principle of
least surprise”.
I examined your suggestion and decided to leave as it is.
Current behavior may surprise you, but I believe it is natural.
Reasons:
- or
containing only Kwartz directives are dummy tags
to use Kwartz directives.
They doesn’t affect HTML design at all, and should be deleted.
- Kwartz is designed to be available for any type of textfile,
as well as available for HTML/XML.
It is very natural to delete or when Kwartz is used
for LaTeX file or PostScript file.
- You can use or
not to delete automatically.
–
regards,
kwa
Makoto Kuwata wrote:
Andreas Schwarz wrote:
A question about sanitizing in general: is it possible to disable
sanitizing for certain variables in the template?
No. Kwartz doen’t have such feature.
I want to implement it (because I also want it), but it is a bit difficult,
because information about whether sanitizing or not sanitizing is lost
when converting HTML file into intermediate code.
I may imprement :print2() statement in the future.
Any idea about when this will be? 
I looked at your code and noticed that you are using mixed tabs and
spaces for indentation; this makes it look strange for someone who isn’t
using the same tab width as you.
- or
containing only Kwartz directives are dummy tags
to use Kwartz directives.
They doesn’t affect HTML design at all, and should be deleted.
See Thomas’ post.
And thanks for the bugfix!
Andreas
Thomas Fini Hansen wrote:
That’s not right,
does, as it’s a block element. It’s effect is
not unlike
. That’s why Amrita and other remove ‘unused’ s
but not
s: it would change how a browser lays out the page.
Really? Oh, I have not known it…
Thanks for your great information.
I fixed it and released a new version.
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-22.tar.gz
Andreas Schwarz wrote:
I may imprement :print2() statement in the future.
Any idea about when this will be? 
Sorry, I can’t promise or specify a plan.
···
–
kwa
Makoto Kuwata wrote:
Thomas Fini Hansen wrote:
That’s not right,
does, as it’s a block element. It’s effect is
not unlike
. That’s why Amrita and other remove ‘unused’ s
but not
s: it would change how a browser lays out the page.
I fixed it and released a new version.
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-22.tar.gz
Thanks!
Another feature request: it would be very nice to include other template
files like this:
Andreas Schwarz wrote:
I may imprement :print2() statement in the future.
Any idea about when this will be? 
Sorry, I can’t promise or specify a plan.

Andreas Schwarz wrote:
Another feature request: it would be very nice to include other template
files like this:
That’s an interesting suggestion.
There are some choices.
-
which to include, presentation data or presentation logic?
-
included file should be also parsed, or not?
for example, Veocity has both (‘#parse()’ and ‘#include()’).
-
is argument a string, variable or name?
(id=“include:‘file.html’” , id=“include:filename” or id=“include:file.html”)
-
how to include, insert before the element or replace the element?
sub.html:
···
foo
--------------------
main.html
foo bar baz
--------------------
choice1: insert before the element
foo
foo bar baz
--------------------
choice2: replace the element
foo
--------------------
I must examine and determine which is more suitable to Kwartz.
Maybe I have to add :include() statment in intermediate language.
–
kwa
Makoto Kuwata wrote:
Andreas Schwarz wrote:
Another feature request: it would be very nice to include other template
files like this:
That’s an interesting suggestion.
There are some choices.
- which to include, presentation data or presentation logic?
Presentation data (maybe presentation logic, too?).
- included file should be also parsed, or not?
for example, Veocity has both (‘#parse()’ and ‘#include()’).
Should be parsed, so that it’s possible to split a large template into
a few smaller files.
- is argument a string, variable or name?
(id=“include:‘file.html’” , id=“include:filename” or id=“include:file.html”)
It think a constant string would be sufficient, and probably it would be
a bit difficult to compile all the files seperately and decide which to
include at runtime.
- how to include, insert before the element or replace the element?
I would insert it inside the element, and remove the tag if it’s a span
tag and doesn’t have any more attributes. IMO this offers the most
possibilities and the “least surprise” 
Andreas
Andreas Schwarz wrote:
A question about sanitizing in general: is it possible to disable
sanitizing for certain variables in the template?
I implemented partial sanitizing into Kwartz.
Could you try it?
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-24_beta.tar.gz
(This is experimental version. Documents are not updated.)
New features:
-
new function E() and X() are implemented.
E() means always sanitizing, and X() means never sanitizing.
Presentation data (HTML file):
···
#{expr}#
#{E(expr)}#
#{X(expr)}#
Intermediate code:
:print(expr)
:print(E(expr))
:print(X(expr))
Output script:
Ruby
print expr, “\n”
print CGI.escapeHTML(expr), “\n”
print expr, “\n”
eRuby
<%= expr %>
<%= CGI.escapeHTML(expr) %>
<%= expr %>
Output script (with command option ‘-s’):
Ruby
print CGI.escapeHTML(expr), “\n”
print CGI.escapeHTML(expr), “\n”
print expr, “\n”
eRuby
<%= CGI.escapeHTML(expr) %>
<%= CGI.escapeHTML(expr) %>
<%= expr %>
-
New directives ‘Value’, ‘VALUE’, ‘Attr’ and ‘ATTR’ are implemented.
‘Value’ and ‘Attr’ mean always sanitizing.
‘VALUE’ and ‘ATTR’ mean never sanitizing.
For example, id=“Value:expr” is equal to id=“value:E(expr)”,
and id=“ATTR:name:value” is equal to id=“attr:name:X(value)”.
Presentation data (HTML file):
foo
foo
foo
--------------------
Intermediate code:
:print(‘’, expr, ‘\n’)
:print(‘’, E(expr), ‘\n’)
:print(‘’, X(expr), ‘\n’)
I wonder whether this design is nice.
If you have any suggestions, please tell me.
–
regards,
kwa
Makoto Kuwata wrote:
Andreas Schwarz wrote:
A question about sanitizing in general: is it possible to disable
sanitizing for certain variables in the template?
I implemented partial sanitizing into Kwartz.
Could you try it?
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-24_beta.tar.gz
(This is experimental version. Documents are not updated.)
Output script (with command option ‘-s’):
Hmm, sanitizing doesn’t seem to work with -s:
$ echo ‘#{a}#’ | kwartz -s
print a, “\n”
I wonder whether this design is nice.
The E() and X() thing is nice, but I don’t think I will use the other
way because I like to keep the “id=” stuff as short as possible.
Andreas