Lasso™ Professional 5 Developer’s Guide
Duncan Cameron
Wordware Publishing, Inc.
Library of Congress Cataloging-in-Publication Data Cameron, Duncan (Duncan James), 1970-. Lasso Professional 5 developer's guide / by Duncan Cameron. p. cm. ISBN 1-55622-961-5 1. Lasso Professional (Document markup langugage). 2. Internet programming. I. Title. QA76.76.H94 C358 005.2'76--dc21
2002 2002004617 CIP
© 2002, Wordware Publishing, Inc. All Rights Reserved 2320 Los Rios Boulevard Plano, Texas 75074 No part of this book may be reproduced in any form or by any means without permission in writing from Wordware Publishing, Inc. Printed in the United States of America
ISBN 1-55622-961-5 10 9 8 7 6 5 4 3 2 1 0205 Lasso is a trademark of Blue World Communications, Inc. Other products mentioned are used for identification purposes only and may be trademarks of their respective companies. Appendix A is used with the permission of Blue World Communications, Inc.
All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address. Telephone inquiries may be made by calling: (972) 423-0090
This book is dedicated to the late James and Dorothy Cameron and my patient, loving wife, Antonia.
Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Chapter 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Testing Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Sending Text to the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Sending Data to the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Sending HTML to the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Using White Space in Lasso (and HTML) . . . . . . . . . . . . . . . . . . . . . . 8 Adding Comments to Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . 10 Using JavaScript with Lasso. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Chapter 2 Variables . . . . . . . . . . . What are Variables? . . . . . . . . . Variable Syntax . . . . . . . . . . . Types of Variables . . . . . . . . . . Global Variables . . . . . . . . . Local Variables. . . . . . . . . . Server Application Variables . . Assigning Values to Variables . . . .
. . . . . . . .
. . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . .
. 17 . 17 . 17 . 18 . 18 . 18 . 19 . 19
Chapter 3 HTML Forms and Lasso . . . . Creating a Simple Form . . . . . . . . Using GET or POST . . . . . . . . . . Receiving Data from a Form in Lasso. Using Hidden Fields . . . . . . . . . .
. . . . .
. . . .
. . . . . . . . . .
. . . . .
. . . . .
. . . .
. . . . . . . . . .
. . . . .
. . . . .
. . . .
. . . . . . . . . .
. . . . .
. . . . .
. . . .
. . . . . . . . . .
. . . . .
. . . . .
. . . .
. 25 . 25 . 28 . 29 . 35
Chapter 4 Control Structures . . About Control Structures . The [If]...[/If] Conditional . More Operators . . . . . . Using [Else] . . . . . . . . Using [While]...[/While] . . Using [Loop]...[/Loop]. . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. 39 . 39 . 39 . 42 . 44 . 49 . 52
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . . .
. . . . . .
. . . . . . .
. . . . . . .
. . . . . .
Chapter 5 Using Strings . . . . . . . . . Trimming Strings . . . . . . . . . . Connecting Strings (Concatenation). Encoding and Decoding Strings . . .
. . . .
. . . .
. . . . . . . . . . . . . . .
. . . .
. . . . . . . . . . . . . . .
. . . .
. . . . . . . . . . . . . . .
. . . .
. . . . . . . . . . . . . . .
. . . .
. 59 . . 59 . . 60 . . 65
. . . . . .
v
Contents Encrypting and Decrypting Strings . . . . . . . . . . . . . . . . . . . . . . . . . 72 Exploiting Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Chapter 6 Using Numbers . . . . . . . . . . . . Adding, Subtracting, Multiplying, and Dividing Incrementing and Decrementing a Number . . . Using Multiple Operators . . . . . . . . . . . . Using Built-in Mathematical Functions . . . . . Formatting Numbers . . . . . . . . . . . . . . .
. . . . . .
. . . . .
. . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . .
. . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . .
. . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . .
. 81 . 81 . 86 . 87 . 94 . 96
Chapter 7 Using Arrays and Maps . . . . . . . . . . . . . . . . . . . . . . 97 Creating an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Adding and Removing Items in an Array . . . . . . . . . . . . . . . . . . . . . . 99 Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Sorting Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Creating an Array from a Form . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Using Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Using Arrays and Maps Together . . . . . . . . . . . . . . . . . . . . . . . . . 116 Chapter 8 Files and Directories . . . . . . . . . File Permissions . . . . . . . . . . . . . . . . Creating and Writing to Files. . . . . . . . . . Reading from Files . . . . . . . . . . . . . . . Browsing Directories . . . . . . . . . . . . . . Uploading Files . . . . . . . . . . . . . . . . . Renaming and Deleting Files and Directories .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. 127 . . 127 . . 133 . . 141 . . 150 . . 162 . . 169
Chapter 9 Databases . . . . . . . . . . . Databases and Lasso . . . . . . . . . . Connecting to a Database. . . . . . . . Lasso MySQL — Creating a Database. Lasso MySQL — Creating a Table . . Lasso MySQL — Creating a Field . . . Using -Add . . . . . . . . . . . . . . . Using -Findall and -Search . . . . . . . Using -Update . . . . . . . . . . . . . Using -Delete . . . . . . . . . . . . . . Using -SQL . . . . . . . . . . . . . . . Using Joins . . . . . . . . . . . . . Chapter 10 Sessions and Cookies. . Creating Sessions . . . . . . . Sessions with Forms . . . . . Adding Variables to a Session Deleting a Session . . . . . . Using Sessions with Cookies.
vi
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. . . . . . . . . . . .
. 173 . . 173 . . 175 . . 176 . . 179 . . 182 . . 191 . . 194 . . 200 . . 202 . . 204 . . 213
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. 215 . . 215 . . 218 . . 222 . . 223 . . 232
Contents Chapter 11 Creating Web Applications . Using Include and Library . . . . . Determining Date and Time . . . . Using HTTP Headers . . . . . . . . Sending Email . . . . . . . . . . . Required Parameters . . . . . . Optional Parameters . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. 237 . . 237 . . 239 . . 253 . . 257 . . 258 . . 258
Chapter 12 Debugging . . . . . . Common Errors . . . . . . . Error Reporting . . . . . . . Debugging Detective Work. Using Logs . . . . . . . . .
. . . . . . . . . . . . . .
. . . . .
. . . . .
. . . . . . . . . . . . . .
. . . . .
. . . . .
. . . . . . . . . . . . . .
. . . . .
. . . . .
. . . . . . . . . . . . . .
. . . . .
. . . . .
. . . . . . . . . . . . . .
. . . . .
. . . . .
. 267 . . 267 . . 273 . . 280 . . 281
Chapter 13 Advanced Topics . . . . . . Regular Expressions . . . . . . . . Creating and Using Custom Tags . Creating and Using LassoApps . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. 285 . . 285 . . 292 . . 298
Chapter 14 Lasso Administration . . . . . Setting up the Administration Panel . Datasources . . . . . . . . . . . . . . Group Permissions . . . . . . . . . . File Permissions . . . . . . . . . . . General Permissions . . . . . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . . . . . . . . . . . . .
. . . . . .
. . . . . .
. 303 . . 303 . . 305 . . 310 . . 316 . . 317
Appendix A Lasso Tag Syntax Overview . . . . . . Action Tags . . . . . Administration Tags Array Tags . . . . . Client Tags . . . . . Conditional Tags . . Custom Tags . . . . Data Type Tags . . . Database Tags. . . . Date Tags . . . . . . Delimiter Tags . . . Encoding Tags . . . Encryption Tags. . . Error Tags . . . . . . File Tags . . . . . . Include Tags . . . . Link Tags . . . . . . Math Tags . . . . . . Operator Tags . . . . Output Tags . . . . . Response Tags . . . Results Tags. . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. 319 . . 319 . . 320 . . 323 . . 326 . . 330 . . 333 . . 335 . . 338 . . 339 . . 360 . . 365 . . 368 . . 370 . . 371 . . 377 . . 382 . . 384 . . 387 . . 396 . . 399 . . 400 . . 404
. . . . .
Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
vii
Contents Session Tags . String Tags . . Symbol Tags . Technical Tags Utility Tags . . Variable Tags .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
Appendix B Error Code Reference . Lasso Error Codes . . . . . Lasso MySQL Error Codes. FileMaker Pro Error Codes.
. . . .
. . . . . . . . . . . . . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. . . . . . . . . . .
. . . .
. . . .
. 443 . . 443 . . 445 . . 448
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. . . . . . . . . . . . . .
. 451 . . 451 . . 451 . . 452 . . 452 . . 452 . . 453 . . 453 . . 454 . . 454 . . 454 . . 455 . . 456 . . 457
Appendix C Quick Table Reference . . . Data Types . . . . . . . . . . . . . String Expression Symbols . . . . . Math Expression Symbols . . . . . Conditional Expression Symbols. . Logical Expression Symbols . . . . LDML Delimiters. . . . . . . . . . Array Member Tags . . . . . . . . Map Member Tags . . . . . . . . . Pair Member Tags . . . . . . . . . File Tags . . . . . . . . . . . . . . Error Tags . . . . . . . . . . . . . . Encoding Keywords . . . . . . . . HTML/HTTP Delimiters . . . . . .
. . . . . . . . . . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
410 412 421 430 435 441
Appendix D Example Application MyResume . . . . . . . . . . . . . . . . . 459 Application Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 readme.lasso . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459 Appendix E Lasso Resources. . . General Lasso Resources . Vendor Related . . . . ISP Related . . . . . . Database Related . . . General Lasso Sites. . Web Server Related. .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. . . . . . . . . . . . . . . . . . . .
. . . . . . .
. . . . . . .
. 465 . . 465 . . 465 . . 465 . . 466 . . 466 . . 467
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
viii
Foreword Since its inception in late 1996, the Lasso product line has provided tools that help Web developers build and serve data-driven Web sites with enhanced ease of use, security, performance, and flexibility. The reason tens of thousands of developers have used Lasso is that it allows very complex data-driven Web sites to be constructed with minimum effort. Lasso has allowed non-programmers to become Web database developers, something many Web designers have thought too formidable of a challenge to overcome. Lasso has also allowed advanced programmers to create and distribute solutions in a fraction of the time and with more flexibility than that afforded by other technologies. In short, Lasso products have helped beginning to advanced Web designers and developers in their quest to retain more control and capture enhanced productivity when building and serving data-driven Web sites. While Lasso products are renowned for their ease of use—and while the latest version of Lasso products contains thorough documentation (over 1,800 pages) which has received accolades from many developers—there has been a long-standing need for a dedicated book on the subject of Lasso. This book delivers on that need. In this book, Duncan Cameron excels in conveying the essence of Lasso through well-organized and well-explained concepts and examples. Beginning Web developers to advanced programmers alike should benefit from this book and from the huge advancements made to the Lasso product line in Lasso Professional 5. We thank you for your interest in Lasso products and we look forward to serving you long into the future. Bill Doerrfeld Founder and CEO of Blue World Communications, Inc. Publishers of the Lasso product line
ix
Introduction About This Book This book was written for two simple reasons. The first was a personal need for a book that I could take with me to use as a reference guide when away from my own computer, and the second was because there were no books available for Lasso on the commercial market. I have hopefully achieved my goal of providing a tool for the Lasso developer’s laptop case with this book. I have not written extensive textual content in this book but have concentrated on examples. The reason for this is that in my own experience I always wanted an example of code to show me how it worked. If you are looking for reams and reams of text and minimal examples, then this book is probably not for you. The book is designed for beginner and intermediate developers and is broken down into the most common areas that I believe are going to be your development needs when using Lasso. It will take you to a level of feeling competent in writing Lasso-based programs.
What is Lasso? To me, Lasso is an application that allows building, constructing, developing, and/or deploying large data-driven solutions for my clients. When I first found this solution, the name “Lasso” initially made me think that it wasn’t what I was looking for, but after downloading an evaluation version of the software, I found that it was exactly what I wanted for my clients’ needs. Since those early days, Lasso has evolved into a true enterprise middleware application, and, in my view, it can handle any development task I put in front of it. If you are looking to build an Internet/intranet or extranet application, I would seriously consider Lasso. Even if you are an experienced programmer in other middleware, Lasso has something for you.
xi
Introduction
What You Will Need To use this book to its best advantage, I recommend that you obtain each of the following before settling down. However, the book is also designed as a reference library for developers on the road. You will need: n
A text editor like SimpleText or Notepad.
n
An Internet browser such as Internet Explorer or Netscape.
n
Access to a Lasso Professional 5 web server.
n
An FTP client if you are uploading files to your server.
About the Author Duncan Cameron, originally from Falkirk in Scotland, is an enthusiastic, charismatic professional and a fun character who enjoys working as a freelance application developer specializing in Lasso. After spending several years in the Royal Navy and traveling the world, he now enjoys life with his family in their home in the English countryside. Some would say his passion for Lasso and what it can achieve is evangelistic, but Duncan is always one for ensuring that the proof of the pudding is in the eating. Duncan is also the founder of the popular Lasso Community web site LassoDevelopment.com and is the founder of the UK Lasso Association.
xii
Getting Started
1
Chapter 1
Getting Started Basic Syntax All programming languages have some basic rules to follow when writing applications or scripts. We call these rules syntax. Syntax needs to be interpreted by Lasso through the web server so that Lasso knows which code is Lasso’s. Just as you need the correct punctuation, grammar, and spelling to get high marks on an English essay, with Lasso you need to use the correct syntax to have a smooth running web site with no errors flashing on the screen and the web server telling you that something is wrong. Lasso comes with a choice of style in programming. Yes, you have a choice! There is [] Square Bracket syntax that surrounds LDML (Lasso Dynamic Markup Language) with square brackets ([]). [Output: 'your text string']
For the more traditional programmer there is , which uses a script-based syntax.
You will notice that [] Square Bracket syntax starts and ends with square brackets and syntax ends with a semicolon. Once received, these tell Lasso to execute the given tag line. Leaving off a closing ] or ; will result in a syntax error. Another basic rule is what file extension you give your files. For the examples in this book I am using .lasso, as this will ensure that all Lasso code is parsed by your web server running Lasso. If you follow these basic syntax rules, you shouldn’t have many problems. In order to create your first Lasso page, you will start off exactly as you would if you were creating an HTML document.
2
Chapter 1
v
Note You will find that as you progress with Lasso, both syntax options will be valuable in their own way.
Testing Your Scripts Unlike HTML, which can be tested on your own computer using a web browser, Lasso pages need to be viewed via a Lasso-enabled web server. If you are working directly on a Lasso-enabled web server, the script will be ready to work with once you’ve saved it. If you are creating your script using your home computer and using a third party’s Lasso-enabled web server, you will need to use FTP to place it onto the server for testing. Your ISP or web host should provide you with FTP access. You will also need an FTP application such as Fetch (for the Macintosh) or WS_FTP (for Windows) or WebDAV. Instructions To upload your script to the server: 1. Open your FTP application. 2. Connect to the server, using the address, username, and password provided to you by your ISP or web host. 3. Find the proper directory for your HTML pages (e.g., www/ or htdocs/ or wwwroot/). 4. Upload your script, for example test.lasso, to the server. To test your script in the browser: 1. Open your favorite web browser. 2. Enter the URL of the site where your script has been saved. 3. Add the filename /test.lasso to the URL. 4. Press Return. The page should load in your browser window. Some text editors, such as BBEdit, have built-in FTP capability, allowing you to save your scripts directly to the server.
Getting Started
3
Sending Text to the Browser In order for the user to read text on your web site, you need to show the user a text string. Text strings in Lasso are identified by surrounding the text with single quotes and using the [Output] LDML tag. [Output: 'Hello World']
[Output] tells Lasso to display the value that is contained within the tag area between the colon (:) and the end of the tag. In this case it is a square bracket. If your text string contains a single quote: I haven't heard from you.
you will need to use what is called an escape character (\ — backwards slash). This would now look like: [Output: 'I haven\'t heard from you']
This ensures that Lasso doesn’t think that the single quote is the end of the string you wish to output. Another issue is working with numbers (integers or decimals). If you want Lasso to know that the value of the output is a number and not text, you would not use any single quotes. [Output: 3]
This is important to remember when you want Lasso to do calculations for you. You will notice [] Square Bracket syntax closes with a square bracket (]) to tell Lasso that this is the end of the tag line. In the syntax is slightly different, as you use a semicolon (;) to indicate the end of the tag line.
Instructions Using what we have covered, I will show you how to create your first Lasso page in both [] Square Bracket and syntax. Please follow these steps to test your first Lasso page. 1. Open a new document in your text editor. 2. Then save the blank file as syntax.lasso in a folder on your hard drive that you wish to use for storing these examples.
4
Chapter 1
3. Type the [] Square Bracket or example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used [Output] Square Bracket Example 01. 02. 03. 04. 05. 06. 07. 08.
<TITLE>Basic Syntax [Output: 'Hello World']
LassoScript Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10.
<TITLE>Basic Syntax
Getting Started
5
Sending Data to the Browser Now that you have created your first Lasso page, it’s time to make it actually do something. Lasso uses LDML to tell the server what data to send to the user’s web browser. All the basic principles that we have covered set the rule for showing data to the user. Lasso has a lot of built-in LDML tags used for specific purposes to assist in the development of solutions. The following examples show you how easy it is to use Lasso to indicate what browser the current visitor is using. Instructions 1. Open a new document in your text editor. 2. Then save the blank file as browser.lasso in a folder on your hard drive that you wish to use for storing these examples. 3. Type the [] Square Bracket or example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used [Output]
[Client_Type]
Square Bracket Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11.
<TITLE>Browser [Output: 'Hello, your browser is: '] [Output: (Client_Type)]
LassoScript Example 01. 02. 03. 04. 05. 06. 07. 08.
<TITLE>Browser
6
Chapter 1
09. ?> 10. 11. 12. 13. 14.
v
Note We use the [Output] tag to show that we are outputting this data to the browser.
Sending HTML to the Browser When looking at web sites, nobody thinks twice about all the different colors and fonts used in order for the text to be displayed in an appealing way. Lasso allows developers to integrate HTML with LDML to ensure that they can format the output as they wish, using fonts, colors, tables, and more. To do this we need to tell Lasso how to output the integrated HTML code along with the text to the browser to interpret it to the user. Page
ð Lasso ð Browser ð User
This is called encoding. We tell Lasso what type of encoding policy to use in order for the character to be displayed properly to the user. Encoding is covered in more detail later in the book in Chapter 5, “Using Strings.” To show the user a word that is bold, we would use: [Output: '
Hello', -EncodeNONE]
If we didn’t use -EncodeNONE, it would not display correctly to the user.
Getting Started
7
Notice we used a comma to separate the string information from the type of encoding we want the [Output] tag to use. This tells Lasso that there is another command to follow in addition to outputting the string. Instructions 1. Open a new document in your text editor. 2. Then save the blank file as html.lasso in a folder on your hard drive that you wish to use for storing these examples. 3. Type the [] Square Bracket or example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used [Output]
[Client_Type]
Square Bracket Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10.
HTML [Output: '
Hello, your browser is:
', -EncodeNONE] [Output: (Client_Type)]
LassoScript Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14.
<TITLE>HTML Hello, your browser is:
', -EncodeNONE; Output: (Client_Type); ?>
8
Chapter 1
Using White Space in Lasso (and HTML) As those who hand code HTML understand, white space (blank lines, tabs, and other extra spaces) in your code can help maintain an uncluttered programming environment while not affecting what the viewer sees in the browser. By inserting blank lines between sections of code, placing nested elements one tab-stop in from their predecessor, and by spacing out code, a script will look more organized, making it easier to initially program and later debug. Thus, judicious use of white space in your work is highly encouraged and can be utilized in both your LDML and the resulting HTML. Hello, your browser is:
', -EncodeNONE; Output: (Client_Type); ?>
When programming your LDML, you should understand that white space is generally (but not universally) ignored. Any blank line (whether it be just one or several in a row) is completely irrelevant to the end result. Likewise, tabs and spaces are normally inconsequential to LDML.
Getting Started
9
Instructions 1. Open a new document in your text editor. 2. Then save the blank file as whitespace.lasso in a folder on your hard drive that you wish to use for storing these examples. 3. Type the [] Square Bracket or example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used -EncodeNONE [Output]
[Client_Type]
Square Bracket Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13.
<TITLE>White Space [Output: '
Hello, your browser is:
', -EncodeNONE] [Output: (Client_Type)]
LassoScript Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16.
<TITLE>White Space Hello, your browser is:
', -EncodeNONE; Output: (Client_Type); ?>
10
Chapter 1
v
Note There is a school of thought that recommends condensing your HTML code into the tightest possible package by eliminating all superfluous spaces. The thinking behind this is that you will increase the download speed of the page once you are no longer transmitting extra spaces. While the notion is not without merit, doing so is not conducive to practicing and learning.
Adding Comments to Your Scripts Adding comments to your scripts is very important; I can’t stress this enough. This is because when you have been working on a project for a few months, you become familiar with your structure and layouts. However, when you have to come back to the same project after a space of a few months to make changes, you will find it difficult to remember what you did unless you added some form of commenting. This also applies to developers who take over development projects and need to know what the particular script does and where what happens, in order to quickly make the required changes. Lasso offers three types of commenting. offers the far superior format using two forward slashes (//), [] Square Brackets offers the [Output_None] ...[/Output_None] option, and finally HTML comments use [HTML_Comment]... [/HTML_Comment].
Getting Started
11
[Output_None]
will be processed by Lasso but not output to the browser. [/Output_None]
hides a portion of the page from being output, but processes the LDML tags within. The double slash // will not do anything if used within square bracketed syntax. However, you will notice throughout the book that I do this, as it is a good habit to get into and you will be able to identify the comments you have made for later viewing. Instructions 1. Open a new document in your text editor. 2. Then save the blank file as comments.lasso in a folder on your hard drive that you wish to use for storing these examples. 3. Type the [] Square Bracket or example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used // (LassoScript comment) -EncodeNONE [Output_None]...[/Output_None]
[Output] [Client_Type]
Square Bracket Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18.
<TITLE>Comments [Output_none] // Displays the type of browser the user is using. [/Output_none] [Output: '
Hello, your browser is:
', -EncodeNONE] [Output: (Client_Type)]
12
Chapter 1
LassoScript Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18.
v
<TITLE>Comments Hello, your browser is:
', -EncodeNONE; Output: (Client_Type); ?>
Note This is one major advantage of using over [] Square Bracket syntax as you can apply // lineage comments without having to use the tag [Output_none].
Getting Started
13
Adding comments is a good habit to get into, and you will receive respect from fellow developers for doing so.
Using JavaScript with Lasso There are times when you wish to hide comments in the browser from the user’s viewing area. This is done by using the HTML comments: . These were originally developed for JavaScript developers who wanted to hide their scripts from the user but allow the browser to read them to run the script. When working with HTML comments and Lasso, you need to follow one rule: Replace the with [HTML_Comment] and [/HTML_Comment]. This allows you to place LDML tags within your JavaScript code. Otherwise, any Lasso code inside will not be processed. A short example is: [HTML_Comment] Your browser is [Client_Browser] [/HTML_Comment]
This outputs to the browser:
Nothing is seen by the users, unless they view the source code. Instructions 1. Open a new document in your text editor. 2. Then save the blank file as javascript.lasso in a folder on your hard drive that you wish to use for storing these examples. 3. Type the [] Square Bracket example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used [HTML_Comment]...[/HTML_Comment] Square Bracket Example 01. 02.
[Client_Browser]
14
Chapter 1
03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35.
v
<TITLE>JavaScript Example <SCRIPT LANGUAGE="javascript"> [HTML_Comment]
// Begin the Javascript
// Ask Lasso for the browser type var Browser = "[Client_Browser]"; // End the JavaScript [/HTML_Comment] <SCRIPT LANGUAGE="javascript"> // Alert the user alert("Hello, your browser is " + Browser);
You just acknowledged that your browser is
[Client_Browser]
Using JavaScript and Lasso.
Note If you are not placing LDML tags within the JavaScript, you do not need to use the tags.
When using JavaScript, I recommend using only the [] Square Bracket syntax as this allows you to see where you have Lasso and where you have JavaScript.
Getting Started
15
Variables
17
Chapter 2
Variables What are Variables? Writing scripts, having data passed from one page to another, and bringing in new data for the current page can be like juggling 15 colored balls in the air and trying to remember to catch one before it hits the ground. Variables are here to save you from worrying about the balls in the air. They are like a safety net that holds onto them until you need to use one of them. Variables temporarily contain the values under a unique name given by you, the developer, so you can use the value on the current page however many times you wish.
Variable Syntax To create a variable you need to give it two things: n
Name
n
Value
Here are a few rules on naming variables: n
Variable names are not case sensitive.
n
Variable names should not contain white space (Tabs or Returns).
n
Variables should not contain punctuation marks, except underscores ( _ ).
A few suggestions on naming variables: n
Don’t use spaces within a variable name like “First Name.” Keep the words together or separated with an underscore and the first letter of each word in uppercase, like “FirstName” or “First_Name.” This will make it easier for you to see the variable and automatically know what it contains.
n
Don’t use abbreviations like “FN,” as it will be difficult to remember what the variable was used for after a few months working on another project.
18
Chapter 2
n
Keep variable names simple and clear. Some projects I have developed in the past have contained over 500 unique variables, and having a good naming system in place will save you headaches and time.
It is important to understand that the way you set a variable may affect its value. When setting a string value to a variable, you would surround the string with single quotes: [Variable: 'FirstName' = 'Joshua']
When setting an integer or decimal number against a variable, you would not use single quotes: [Variable: 'MyNumber' = 302.3]
To set a variable against a substitute tag like [Field] or [Action_param] or even [Var], the following would apply: [Variable: 'DatabaseField' = (Field: 'AnyField')] [Variable: 'VisitorEmail' = (Action_Param: 'VisitorEmail')] [Variable: 'ValueThree' = (Var: 'ValueOne')]
v
Note When setting numbers as a variable be sure you do not surround the number with quotes, as this will turn the number into a string value.
Types of Variables In programming, you will hear the term “variable” used quite often. You may also hear global variable, local variable, and server application variable. They all offer a similar feature in containing a value, and each has its place in development. Global Variables The most common variable you will use while working with Lasso is the global variable, which is defined for the page you are using and does not progress to any further pages once the user has moved onto the next page of the web site or application. Local Variables As you progress with Lasso and become more advanced, you will be writing your own LDML tags to be used within your application. The local variable is designed for use with writing your own LDML tags and means that the variable name will not be used outside of the LDML tag. So if you have a local variable called Test, you will still be able to have a global variable called Test that won’t be affected by the local variable.
Variables
19
Server Application Variables Larger applications usually use a web server just to handle one Internet, intranet, or extranet application. This means that the developers can create customized variables that are always defined when the server is started. We call these server application variables or server wide variables. Generally, these are variables that are used on every page, like having a mini database of definitions and values loaded at the time the server is loaded, so the developer can just declare a variable and the value will pop out. I will not be covering server application variables in this book.
Assigning Values to Variables A variable is created and set using the [Variable] or [Var] tags. The following [] Square Bracket example sets a variable named FirstName to the string value Joshua. [Variable: 'FirstName' = 'Joshua']
You may also see the above written as: [Var: 'FirstName' = 'Joshua']
Using the abbreviation [Var] instead of [Variable] is okay and is often used. The variable is retrieved using the [Variable] or [Var] tag, although this time the tag is simply given the name of the variable used. This means if you wanted to display Joshua, which was set in the variable FirstName, you would use the following tag to retrieve the value: [Var: 'FirstName']
This outputs: Joshua
Once a [Var] contains data it will remain there for the duration of the page, unless you then assign another value to the same [Var]. For example, you have declared the variable Greeting with the value of Hello: [Var: 'Greeting' = 'Hello'] [Var: 'Hello']
This outputs: Hello
Once a variable has been set and it has been assigned a value, you can use the $ symbol (global variable) to retrieve the value instead of writing out [Variable] or [Var]. In this book, I will use the $ symbol after I have shown the variable has been declared. [Var: 'Greeting' = 'Hello']
20
Chapter 2
[Output: $Greeting]
Outputs: Hello
When using the syntax, it is correct to do either of the following:
Or: [Var: 'Greeting' = 'Hello']
Both of these output: Hello
If we now assign another value to the variable Greeting (for example, Goodbye) further down the page, the variable output Hello would be superceded by the new value Goodbye. Because the variable has already been defined, you can use the $ global variable symbol: [Var: 'Greeting' = 'Hello'] [$Greeting = 'Goodbye'] [Output: $Greeting]
This outputs: Goodbye
If you use the $ symbol to retrieve a global variable value, you need to ensure the variable has been defined. Otherwise, it will return an error, as the $ symbol is designed for use only with declared variables. Unlike using [Variable] or [Var], which do not show an error if you have not defined the variable, the $ symbol will return an error when used. To always ensure that a variable has been defined, you can use the following LDML tag that has been designed for this: [Var_Defined: 'Greeting']
This outputs FALSE if no variable has been defined against the name Greeting. If we define our variable first, then we would get a response of TRUE. [Var: 'Greeting' = 'Hello'] [Var_Defined: 'Greeting']
This outputs TRUE and ensures that the variable is safe to use with the $ symbol.
Variables
21
Lasso also allows the developer to define multiple variables at one time. For example: [Var: 'FirstName' = 'Ari', 'LastName' = 'Denison']
This saves you from having to type in [Var] each time, although you cannot set a variable against a variable that has been created within the same instruction. Right: [Var: 'One' = 'StringOne', 'Two' = 'StringTwo'] [Var: 'Three' = $One]
Wrong: [Var: 'One' = 'StringOne', 'Two' = 'StringTwo', 'Three' = $One]
This is incorrect because variable $One has not been defined until it has been loaded by Lasso and the last square bracket processed. Once processed, it can then be used. Instructions The following examples will show you how you can use variables on your page layouts. This example will set the title of the page. 1. Open a new document in your text editor. 2. Then save the blank file as variable.lasso in a folder on your hard drive that you wish to use for storing these examples. 3. Type the [] Square Bracket or example onto your document. 4. Save the document again. 5. Upload the document onto the server that is running Lasso into a folder you are using for testing the examples. Tags Used \ (Escape character) // (LassoScript comment) -EncodeNONE [Var]
[Client_Type] [Output] [Output_None]...[/Output_None]
Square Bracket Example 01. [Output_none] 02. 03. // This example shows how you can use variables on your page layout 04.
22
Chapter 2
05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25.
// and displays the type of browser the user is using. // Set variables for example (notice escape is used again to hide the single quote.) [Var: 'PageTitle' = 'Client\'s Browser'] [/Output_none]
<TITLE>[Output: $PageTitle] [Output:'
Hello, your browser is:
', -EncodeNONE] [Output:(Client_Type)]
LassoScript Example 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28.
Var: 'PageTitle' = 'Client\'s Browser'; ?> <TITLE> Hello, your browser is:
', -EncodeNONE; Output:(Client_Type); ?>