Andreas Schwarz wrote:
Hmm, sanitizing doesn’t seem to work with -s:
$ echo ‘#{a}#’ | kwartz -s
print a, “\n”
Maybe you are using old version.
Could you confirm that you are using a new version?
Andreas Schwarz wrote:
Hmm, sanitizing doesn’t seem to work with -s:
$ echo ‘#{a}#’ | kwartz -s
print a, “\n”
Maybe you are using old version.
Could you confirm that you are using a new version?
Makoto Kuwata wrote:
Andreas Schwarz wrote:
Hmm, sanitizing doesn’t seem to work with -s:
$ echo ‘#{a}#’ | kwartz -s
print a, “\n”Maybe you are using old version.
Yes, sorry, I accidently put the new version into /usr/bin instead of
/usr/local/bin.
The first problem I found was that CGI::escapeHTML doesn’t work for
Numbers. I think it would make sense to change escapeHTML(x) to
escapeHTML((x).to_s).
I implemented a feature to include presentation data file.
Download and try it.
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-25_beta.tar.gz
New features:
New directive ‘include’ added.
It is to include presentation data file.
Presentation data:
New statement :load() and new directive ‘load’.
It is to load presentation logic file.
New command option ‘–include_path’ and ‘–load_path’.
These are used to specify path list for ‘include’ and ‘load’.
Andreas Schwarz wrote:
The first problem I found was that CGI::escapeHTML doesn’t work for
Numbers. I think it would make sense to change escapeHTML(x) to
escapeHTML((x).to_s).
Thanks. I adopted it.
–
regards,
kwa
Makoto Kuwata wrote:
I implemented a feature to include presentation data file.
Thanks for your update, everything seems to work perfectly.
Another suggestion: personally I don’t like it to let the template
access all the local variables. I would prefer something like this:
Ruby code:
data[:text] = “hello” # or use string as hash key, i don’t care
template = Kwartz::TemplateFile.new(‘template.html’)
puts template.expand(data)
Template file:
#{text}#
It has the advantage of a clear division between internal program data
and template data. Another possibility would be to compile the template
to a ruby class that can be used with:
require ‘my_template.rb’
puts MyCompiledTemplate::expand(data)
This would make erb/eruby unnecessary and would also probably be a bit
faster (although this doesn’t matter too much).
Andreas Schwarz
Thanks for your update, everything seems to work perfectly.
Another suggestion: personally I don’t like it to let the template
access all the local variables.
Yes, but it is difficult to change current mechanism.
I want Kwartz to be available not only in Ruby but also PHP, Java
or other programming languages.
Generating output script is the best solution I think to perform
the goal.
It has the advantage of a clear division between internal program data
and template data.
I can understand what you feel.
Kwartz can analyze template and report what variables are used.
Presentation data:
Hello World!
In Kwartz, ‘global variable’ means a variable what is passed from
main program to template, and ‘local variable’ means a variable
what is used only in template.
It is not what you want, but it can help you to know what is used
and not used.
regards,
kwa
Makoto Kuwata wrote:
Andreas Schwarz
Thanks for your update, everything seems to work perfectly.
Another suggestion: personally I don’t like it to let the template
access all the local variables.
Analyze:
$ kwartz -a analyze file.html
global variables: user list
local variables: item
That would make it possible to write a script that generates ruby code
like this:
module MyTemplate
def MyTemplate::expand(data)
user = data(:user)
list = data(:list)
# any other “global” variables needed by the template
# ruby code generated by kwartz
end
end
It’s certainly ugly, but I can’t think of anything else. Or is there a
way to set variables from the contents of a hash?
Andreas Schwarz wrote:
module MyTemplate
def MyTemplate::expand(data)
user = data(:user)
list = data(:list)any other “global” variables needed by the template
ruby code generated by kwartz
end
endIt’s certainly ugly, but I can’t think of anything else. Or is there a
way to set variables from the contents of a hash?
I created a utility script ‘mkmethod’.
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-26_beta.tar.gz
Example:
====================
bash$ ./mkmethod -h
Usage: mkmethod [-p file] [-l lang] [-M module] [-m method] [-s] file.html
Options:
-p file : presentation logic file
-l lang : ruby / eruby / erb (default ‘ruby’)
-M module : module name (default none)
-m method : method name (default ‘expand_’ + file)
-s : sanitizing (equals to ‘–escape=true’)
–name=value : options for kwartz
bash$ ./mkmethod -p hoge.plogic hoge.html
def expand_hoge(_args)
user = _args[:user]
list = _args[:list]
print “Hello "
print user
print “!\n”
for item in list do
print “
bash$ ./mkmethod -p hoge.plogic -M Hoge -l erb hoge.html
module Hoge
include ERB::Util
def self.expand_hoge(_args)
user = _args[:user]
list = _args[:list]
_erbout = ‘’; _erbout.concat "Hello “; _erbout.concat(( user ).to_s); _erbout.concat “!\n”
for item in list do
_erbout.concat “
====================
I hope you’d like it.
–
regards,
kwa
Makoto Kuwata wrote:
Andreas Schwarz wrote:
module MyTemplate
def MyTemplate::expand(data)
user = data(:user)
list = data(:list)any other “global” variables needed by the template
ruby code generated by kwartz
end
endIt’s certainly ugly, but I can’t think of anything else. Or is there a
way to set variables from the contents of a hash?I created a utility script ‘mkmethod’.
http://www.kuwata-lab.com/webtech/kwartz/kwartz_2004-03-26_beta.tar.gz
bash$ ./mkmethod -p hoge.plogic hoge.html
def expand_hoge(_args)
user = _args[:user]
list = _args[:list]
print "Hello "
Hello,
yes, that looks good, however I think it would be better to return the
expanded string instead of printing it.
Andreas
Andreas Schwarz wrote:
yes, that looks good, however I think it would be better to return the
expanded string instead of printing it.
It is not able to return string if you are using eruby.
(This is not due to Kwartz nor mkmethod script.)
You can do it only with ERB.
Compile:
bash$ ./mkmethod -p hoge.plogic -M Hoge -l erb hoge.html
module Hoge
include ERB::Util
def self.expand_hoge(_args)
user = _args[:user]
list = _args[:list]
_erbout = ‘’; _erbout.concat "Hello “; _erbout.concat(( user ).to_s); _erbout.concat “!\n”
for item in list do
_erbout.concat “\n”
\n”
_erbout.concat "- ”; _erbout.concat(( item ).to_s); _erbout.concat “
\n”
_erbout.concat “
end
_erbout
end
end
–
regards,
kwa
Makoto Kuwata wrote:
Andreas Schwarz wrote:
yes, that looks good, however I think it would be better to return the
expanded string instead of printing it.It is not able to return string if you are using eruby.
(This is not due to Kwartz nor mkmethod script.)
You can do it only with ERB.Compile:
bash$ ./mkmethod -p hoge.plogic -M Hoge -l erb hoge.html > hoge.rb
I get the following error with my template
(http://andreas-s.net/template.tar.gz):
andreas@213-203-244-47:templates$ mkmethod -p template.plogic -M Template -l erb template.html
/usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2654:in exec_expr': undefined method token’ for nil:NilClass (NoMethodError)
from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2679:in exec_expr' from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2665:in exec_expr’
from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2690:in exec_expr' from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2689:in exec_expr’
from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2765:in exec_stmt' from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:1069:in execute’
from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:904:in exec_nodelist' from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:903:in each’
… 51 levels…
from /usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2640:in analyze' from /usr/local/bin/mkmethod:128:in compile’
from /usr/local/bin/mkmethod:59:in `main’
from /usr/local/bin/mkmethod:155
Andreas Schwarz wrote:
I get the following error with my template
(http://andreas-s.net/template.tar.gz):andreas@213-203-244-47:templates$ mkmethod -p template.plogic -M Template -
l erb template.html
/usr/local/lib/ruby/site_ruby/1.8/kwartz.rb:2654:inexec_expr': undefined methodtoken’ for nil:NilClass (NoMethodError)
This is a bug about handling negative number in Kwratz::Analyzer#exec_expr().
Try this patch:
8< - - - - - - - 8< - - - - - - - 8< - - - - - - - 8< - - - -
— kwartz.rb 2004/03/26 10:59:00 0.90
+++ kwartz.rb 2004/04/03 05:13:45
@@ -2676,7 +2676,7 @@
when ‘+’, ‘-’, ‘*’, ‘/’, ‘%’ #, ‘^’
exec_expr(expr_node.left, arg1, arg2)
exec_expr(expr_node.right, arg1, arg2)
exec_expr(expr_node.right, arg1, arg2) if expr_node.right
when '!'
exec_expr(expr_node.left)
Thanks for your reporting.
–
regards,
kwa