A few simple examples of Knop code
Usage of the Knop custom types. Also see the online API reference.
// Example 1 - A Simple Form
// Create a new form object
var: 'form'=knop_form;
// Add text fields and a submit button
$form -> (addfield: -type='text', -name='firstname', -label='First name');
$form -> (addfield: -type='text', -name='lastname', -label='Last name');
$form -> (addfield: -type='textarea', -name='message', -label='Message');
$form -> (addfield: -type='submit', -name='button_send', -value='Send message');
// Show the form on the page
<form action="simple-form-response.lasso">
[$form -> renderform]
</form>
// Example 1 - A Simple Form, response page
// Create the same form object again
var: 'form'=(knop_form);
$form -> (addfield: -type='text', -name='firstname', -label='First name');
$form -> (addfield: -type='text', -name='lastname', -label='Last name');
$form -> (addfield: -type='textarea', -name='message', -label='Message');
$form -> (addfield: -type='submit', -name='button_send', -value='Send message');
// Load field values from the submission
$form -> loadfields;
// Look at the fielvalues
$form -> updatefields;
/* Use the form input in an inline, for example like this:
inline: -database=...,
$form -> updatefields,
-add;
/inline;
*/
// Create a database object
var: 'db'=(knop_database: -database='knopdemo', -table='customer', -keyfield='id', -username='knop', -password='knop');
// Create a form object
var: 'form'=(knop_form: -formaction='form-and-database-response.lasso', -database=$db);
// Add text fields and a submit button
$form -> (addfield: -type='text', -name='firstname', -label='First name');
$form -> (addfield: -type='text', -name='lastname', -label='Last name');
$form -> (addfield: -type='textarea', -name='message', -label='Message');
$form -> (addfield: -type='addbutton', -value='Send message');
// Example 2 - A form talks to a database
// Configure database and form objects in an include
include: 'form-and-database-config.lasso';
// Show the form on the page, this time the form object is a complete html form
$form -> renderform;
// Configure database and form objects in an include
include: 'form-and-database-config.lasso';
// Handle the form submission
$form -> process;
// Show the result
[$form -> (renderhtml: -excludetype='submit')] Adding record [$db -> error_msg]
//Example 3 - Knop_nav
// Create the parent nav object.
var: 'nav'=(knop_nav: -navmethod='param', -currentmarker=' »');
// Define the site structure
$nav -> (insert: -key='home', -label='Home Page');
// Create a child nav object
var: 'subnav'=knop_nav;
$subnav -> (insert: -key='latest', -label='Latest News');
$subnav -> (insert: -key='archive', -label='News Archive');
$nav -> (insert: -key='news', -label='News', -children=$subnav);
// Determine current location so the nav object knows where we are
$nav -> getlocation;
// Generate navigation menu
$nav -> renderhtml;
// Generate a breadcrumb
$nav -> renderbreadcrumb;
<h1>The current page is [$nav -> label]</h1> <p>The current framework path is [$nav -> path]</p>
// Example 4 - A Grid to show a record listing
// Configuration
// Create a database object
var: 'db'=(knop_database: -database='knopdemo', -table='customer', -keyfield='id', -username='knop', -password='knop');
// Create a grid object
var: 'grid'=(knop_grid: -database=$db);
$grid -> (addfield: -name='firstname', -label='First Name');
$grid -> (addfield: -name='lastname', -label='Last Name');
// Prepare page output
// Perform a search
$db -> (select: ($grid -> sortparams));
($grid -> sortparams);
// Generate the grid
$grid -> renderhtml;
// A Simple Form with validated input using compund expression
// define a few validations
var('namevalidation'={return(params -> size > 0)},
'passwordvalidation'={
local('result'=0); // assume input is ok
if(params -> size < 4);
#result = 'The password must be at least four characters';
else(string_findregexp(params, -find='\d') -> size == 0
|| string_findregexp(params, -find='\D') -> size == 0);
#result = 'The password should contain a mix of numbers and letters';
else(string_findregexp(params, -find='\s') -> size);
#result = 'The password should contain any spaces';
/if;
return(#result);
});
// Create a new form object
var: 'form'=knop_form;
// Add text fields and a submit button
$form -> (addfield: -type='text', -name='firstname', -label='First name', -validate=$namevalidation);
$form -> (addfield: -type='text', -name='lastname', -label='Last name', -validate=$namevalidation);
$form -> (addfield: -type='text', -name='password', -label='Password', -validate=$passwordvalidation);
$form -> (addfield: -type='textarea', -name='message', -label='Message', -rows=10);
$form -> (addfield: -type='submit', -name='button_send', -value='Send message');
// load field values from form submission
$form -> loadfields;
// perform validation
$form -> validate;
// Show the form on the page
<form action="validate-form.lasso">
[$form -> renderform]
</form>
if($form -> isvalid);
'The form input is OK';
else;
'<p style="color: red">The form has input errors<br>';
iterate($form -> errors, var('errorfield'));
if($errorfield -> isa('pair'));
$form -> getlabel($errorfield -> name) + ': ' + $errorfield -> value;
else;
$form -> getlabel($errorfield) + ': The field must not be empty';
/if;
'<br>';
/iterate;
'</p>';
/if;
Author: Johan Sölve
Created: 12 Oct 2009
Last Modified: 16 Mar 2011
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft