Class CKComponent |
|
When you define your component, its class must inherit CKComponent in code file. The component dynamically renders HTML with template and binding file.
A component has 3 files in a directory whose name is the same as the component's name. These three files composite Model-View-Controller architecture.
Template(View): | A HTML file includes CGIKit tags ( <cgikit> ). One CGIKit tag corresponds to one dynamic element. |
Binding(Controller): | A definition file for elements is used by a component. The suffix, ckd, is abbreviation of "CGIKit Definition". The definition file connects Template to Code. |
Code(Model): | A Ruby source where a component's class is defined. It must inherit CKComponent. The name of the component's class must be the same as the component name. |
ex) An application with a MainPage component
/cgi-bin/cgikit-app cgikit-app.cgi /MainPage MainPage.html MainPage.ckd MainPage.rb
In this case, CGIKit application is cgikit-app.cgi and one component, "MainPage".
Template is an ordinary HTML file, except CGIKit's own tag. CGIKit tag has only one attribute, "name". Each <cgikit> tag in template corresponds to one dynamic element.
(MainPage.html)
<html> <body> <cgikit name="HelloWorld"/> </body> </html>
In a binding file, a type and attributes of the elements in the template are defined. Note that the format differs form Ruby.
(MainPage.ckd)
HelloWorld : CKString { value = sayHello; }
A name and type of the element must be separated with colon. In the example above, the element name is HelloWorld and the element type is CKString. Note that HelloWorld is used in the template as "name" attribute of the CGIKit tag.
The blanket, "{", means beginning of the definition of the element attributes.
In attributes defintion, you write some pairs of an element's attribute and its value. The value is method name or literal. At the end of the pair, you must locate a semicolon. If you write string literal, you must enclose the string with single or double quotation marks.
ex)
method: | attribute = method_name; |
string: | attribute = "string"; or 'string' |
figure(integer only): | attribute = 12345; |
true/false: | attribute = true; |
Close the definition with a close-blanket.
A class of a component must be named the same name as the component name, and inherit CKComponent. The class has the methods specified in the ckd file.
(MainPage.rb)
class MainPage < CKComponent def sayHello "Hello World!" end end
Hello World!
In the HelloWorld example, the component is parsed in this order.
Some elements, for example, CKForm, CKSubmit and CKHyperlink, can call a method of a component when they receive some events. You can specify the called method by their "action" attribute.
For example, if you bind Echo#echo method to a CKHyperlink object and the link created by the CKHyperlink object is clicked, Echo#echo is invoked. Please try running examples in samples/Examples by yourself.
Now, you can hook some events. Then, what can we do in the hooked event? When a submit button or hyperlink is clicked, a method bound to the "action" attribute is called. If the result of the method is a CKComponent object, the returned object is displayed as web page. If the returned object is not a CKComponent object, the current component is displayed.
CGIKit provides the easy way to create component objects. A method that creates an instance of component is page(). It returns a object of the component specified by the name. You can link to other components by using this method.
See example:
def other_page # Create an instance of a component other_page = page( 'OtherComponentName' ) # Control the object if needed other_page.foo = true
# Return the component object other_page end
This example displays a component, "OhterComponentName", when a user clicks the element to which the method, "other_page", is bound. In this example, a OtherComponentName's object is created by page(). After that, true is assigned to the "foo" attribute of the created object.
Web page, the component object, can be controlled before returning to clients in this procedure.
Normally, CGI scripts process form data by themselves. But, CGIKit applications don't have to do it.
For example, when a web page is created from a template which has CKText, the "name" attribute of the input tag ( textarea ) is determined by the CKText. The value of the "name" attribute includes the ID. If a user clicks the submit button, the data of the input tag is assinged to the component by calling the method which the ID specifies. The method which the ID specifies is bound the CKText element to.
Note: IDs are not unique. Its are created by joining names of components and elements that defined in the components. Don't use a defined element over and over.
You can use one component in another component like elements. It means that it is possible to nest components.
You can write a component which converts itself to a part of a web page. In some cases, a web page is composed of these components. These components are called parts component. Parts componente is recommended to include CKPartsMaker. A component which includes CKPartsMaker isn't displayed even if its name is set to the CKApplication#target. See also CKPartsMaker section.
This is an example.
<cgikit name="String"/>
String : CKString { value = string; }
class NestedComponent < CKComponent include CKPartsMaker attr_accessor :string end
<html> ... <cgikit name=Location> ... </html>
Location : NestedComponent { string = "This is the nested component."; }
<html> ... This is the nested component. ... </html>
As you can see, the nested component is bound to the method of the parent component.
Methods |
Attributes |
:action | [RW] | |
:path | [RW] |
Classes and Modules |
Public Class methods |
new( app, parent, name, body ) |
Public Instance methods |
request() |
response() |
create_response() |
html_string() |
definitions() |
page( page ) |
Returns an instance of the new component.
run() |
Invokes the action after binding component's variables to attributes for definitions, values from request to component's variables.
to_s() |