Reference Manual for Gracelets - 2.0.0.RC2SourceForge.net Logo
2.20 - Tutorial : Including Script Code
There may be times when it is practical or necesary to "include" a gracelet script. There are two options to accomplish this.

One option is to use the "renderscript" special tag. This will interpret the script as render-only code. That is, it wraps the script inside a JSF Component and then runs the script when the component is rendered. There is a variable called "xml" automatically available to your script which will allow you to easily interact with the response writer to render a dynamic response. Also a variable called "cmp" is also automatically available to these included scripts so you can reference the component that wraps your script. This is a good option when the inclusion of the script changes frequently and is not intended to be a permanent part of the view.

To do this, you pass a single closure to the special 'renderscript' tag, as in the following example (following are the results of the script):

Toggle Line Numbers
1 def s = "(0..10).each { num -> xml.td( \"\$num\" ) }" 
2 xh.table (width: "95%", border: 1) { 
3     caption("Script from outside used for rendering") 
4     tr { renderscript { s } } 
5 } 
http://somesite/somepage.jsf - Mozilla Firefox
http://somesite/somepage.jsf
http://somesite/somepage.jsf
Script from outside used for rendering
012345678910
Above we assign to the variable s some script that will be run at render time. Then we include that script underneath the tr() tag. This shows how easy it is to include render time code which could be retreived from a database or from anywhere else you may want to include scripting from.

Another option is to include true gracelet code, that means everything you can put into a normal view script, you could include. Since view scripts are only ran once per change this should only be used when the include will be from a source that does not change often. Since view scripts are application level components, they cannot be expected to handle session, conversation or any other scope of code below the application level.

Even so, you may find it useful to use this option. Maybe some part of your system is fairly static, and you want to include the source of the gracelet view from somewhere else. You can do this by using the 'include' (not to be confused with the u.include() tag which requires a src attribute) tag that takes a char sequence, input stream or file as a parameter. You can then pass that to the include tag and it will process the resulting text as a gracelet script included at the point where you use this tag.

An example below shows how to use the tag:

Toggle Line Numbers
1 xh.table (width: "95%", border: 1) { 
2     caption("Gracelet code included at runtime") 
3     tr { include("(0..10).each { num -> xh.td( \"\$num\" ) }") } 
4 } 
http://somesite/somepage.jsf - Mozilla Firefox
http://somesite/somepage.jsf
http://somesite/somepage.jsf
Gracelet code included at runtime
012345678910
As can be seen above, we practically do the same thing as in the first example above. The difference is that it uses the xh tag which is a facelet tag library, since this is true gracelet code, it gets compiled and ran when the parent script runs and becomes a "part" of the parent view.