[Klone-users] More complex examples.

Radek radek.hnilica at gmail.com
Mon Dec 17 05:45:17 EST 2007


Hi,

A few words to templates.  I make template and split it into two
files. component/pre_content.kl1 and component/post_content.kl1.  Yes
I have components in it's own directory under the root of the
application. This is because if left in webapp/www directory the KLone
tryies to compile them like pages which rises a bunch of errors.
Components in this way are legal C code only in context of some page.
The page which use template is like

<%! /* -*- mode:c;coding:utf-8;-*- */
#include <project.h>
#define TITLE="Main PAge"
%><%
  /* Set the page variables before including pre_content. */
  var="Value";
%>
<%@include ../../components/pre_content.kl1%>
  <p><tt>CONTENT-BEGIN</tt></p>
    <p>This is a Test Page.</p>
  <p><tt>CONTENT-END</tt></p>
<%@include ../../components/post_content.kl1%>

The first part of the template is in file pre_content:
<%! /* -*- mode:c;coding:utf-8; -*- */
#include <time.h>
%><%
  time_t now;
  now = time(0);
%><html>
  <head>
    <title>TITLE</title>
    <link rel="stylesheet" href="kl.css" type="text/css" />
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  </head>
  <body>
    <h1>TITLE</h1>
    <p>Server: <%=hostname%>, time=<%=ctime(&now)%></p>
    <% render_menu(out, SCRIPT_NAME); %>
    <hr/>

the post_content is similar but rendered after the page content.

This way of doing templates is not the right way.  But it works and is
usable.  Splitting template to pre_content and post_content brings a
more complexity than necessary.  The better way is let the content
render in context of template.  Something like
file mytemplate:
 <html>
   <head>...</head>
  <body>
    <h1></h1>
    .....
    <%= render_page_content %>
    <hr/>
    <p>....</p>
  </body>
</html>


and let the page be
use teplate "mytemplate"
....

But I'm not sure if there is a way how to do such a templating in KLone.



On Dec 16, 2007 10:28 PM, thomas fossati <tho at koanlogic.com> wrote:
>
> In a way a .kl1 is already a web component, pretty much like a java
> servlet ... what do you mean exactly ?
>

Page component is like a mini page embedded in page.  It has it's own
html a its own code.  For example think about component table_editor
which enables display and editing features of table in SQL db.  Then I
can use that component in page like:
  <p>...</p>
  <%@ use component table_editor (table_name, options%>
  <p>,,,</p>
  <%@ use component another_component(component arguments)%>
  ....



Components can be used not only in page, but also as bricks when
creating more complex components.  Thus ruling the complexity of
bigger a applications.  Some of this can by done with the <%@include
filename%> directive so I'll explore it.  But.  Can I pass some
arguments into that component?  Today I do it by the global variables
and page variables like TITLE in template example.  But this have
disadvantages.  All components share these not only with the page but
also with other components.  The second problem is, that components
are included into page, not instantiated.  So using the some component
twice or more times in page is not so simple if possible.

-- Radek



More information about the Klone-users mailing list