All things to All people!

3 Embedded Block Syntax

Moto supports an additional, more compact syntax for embedding large 'blocks' of code into a page without worrying about whitespace treatment. This syntax may be used in addition to the single construct syntax described in the previous section.

3.1 Coding an Embedded Block

Embedded blocks begin with the escape sequence ${ and ends with }$. Inside and embedded block you can have multiple moto statements. Each statement must be terminated by a semicolon ';' .

   statement 1;
   statement 2;
   statement 3;

3.2 Embedded Comments

To comment out a block of text you want moto to ignore from within an ebmedded block surround that text with /* and */ .

/* comment */  

You can also comment out all text until the next end of line by preceeding the comment with //

print "hello"; // this statement prints "hello"  

3.3 Variable Declaration

Inside of an embedded block you can declare variables just as you can in C or java

variable type variable name ( = optional variable value )  

The embedded block syntax for declarations is much more compact than with $declare()

  Date d = new Date();
  String month = getValue("_month",str(d.getMonth()+1));
  String day = getValue("_day",str(d.getDayOfMonth()));
  String year = getValue("_year","2002");
  int i;
  String curtok;
  Tokenizer stok = new Tokenizer("Jan|Feb|Mar|Apr|Mar|Jun|Jul|Aug|Sep|Nov|Dec",'|');

3.4 Doing things in Embedded Land

Every statement inside an embedded block is essentially a $do() statement. Anything you would use a $do() construct for you can write as a statement inside an embedded block.

${String foo = "bar"; int l; l=foo.length() ; l++; foo=foo+foo; }$  

3.5 Embedded Output

${ expression;}$  

You can print something out from an embedded block statement by preceding your expression with the print keyword.

${print 7.00e+10 ;}$
${print 7.00E+10 ;}$
${print 7.00e-10 ;}$
${print 7.00E-10 ;}$
${print false; }$
${print 7.0F; }$
${print 7.0f; }$
${ print 7;}$
 print 7L;
 print "\n";
 print 7l;
${print null;}$
${print "string";}$

3.6 Embedded Conditionals

3.6.1 Embedded If construct

If statements in embedded blocks work just like they do in C or Java

        String qryQuestions;
        if (idTopic eq "0")
           qryQuestions =
              "SELECT q.*,iu.imageURL,p.uname FROM "+
              "tblQuestion q LEFT OUTER JOIN tblImageURL iu ON q.idImageURL=iu.idImageURL "+
               ", tblPrincipal p WHERE p.idPrincipal=q.idOwner";
        else if (idTopic eq "-1")
           qryQuestions =
              "SELECT q.*,iu.imageURL,p.uname FROM "+
              "tblQuestion q LEFT OUTER JOIN tblImageURL iu ON q.idImageURL=iu.idImageURL " +
              "LEFT OUTER JOIN tblTopicToQuestion ttq ON ttq.idQuestion = q.idQuestion "+
              ", tblPrincipal p WHERE p.idPrincipal=q.idOwner AND ttq.idTopic is null ";
           qryQuestions =
              "SELECT q.*,iu.imageURL,p.uname FROM "+
              "tblQuestion q LEFT OUTER JOIN tblImageURL iu ON q.idImageURL=iu.idImageURL " +
              ", tblTopicToQuestion ttq, tblPrincipal p WHERE ttq.idQuestion = q.idQuestion "+
              "AND ttq.idTopic = "+idTopic+" AND p.idPrincipal=q.idOwner";

There is no elseif keyword in embedded blocks, rather the else matches up with the most recent if

3.6.2 Embedded Switch construct

The switch statement works similar to switch in C or Java but like in the $switch() construct, cases do not fall through. Unlike the $switch() construct however, the embedded switch statement does make use of a default keyword. It must however follow all cases in the switch block.

    case "bar": print "baz";
    case "maka": print "ok";
    case "bling": print "bing";
    default: print "zip";

3.7 Embedded Loops

3.7.1 Embedded For loops

For loops in embedded blocks have the same syntax as they do in C

${for(i=0;i<10;i++) print "hello "+str(i)+"\n"; }$  

3.7.2 Embedded While loops

So do While loops

${i=0; while(i<10) print "hello "+str(i++)+"\n"; }$  

3.7.3 Embedded Break and Continue

Break and continue are used just like any other statement

Embedded while
     if (i<10)
        print "hello "+str(i++)+"\n";
  print "goodbye\n";
Embedded continue
  while(true) {
     if (i<10) {
        print "hello "+str(i++)+"\n";
  print "goodbye\n";

3.8 Embedded Scopes

Within embedded blocks you can create explicit variable scopes by using the '{' and '}' operators. Again this works just like it does in C or Java. Declarations inside an embedded scope only exist within that scope. For example the following piece of moto code

  int j = 1;
     int j = 1;
     print ++j; print "\n";
  print j;print "\n";

Would output


From the above example you can see also that redeclaration of an existing variable inside an embedded scope shadows the previous declaration.

You can also use moto scopes to escape from embedded blocks allowing you to intersperse static text or classic moto constructs with your embedded block structured code

${ i=0;
     if (i<10) {
hello $(i++)
     } else {
  print "goodbye\n";

3.9 Embedded Functions

They syntax for defining a function in an embedded block is the same as it is in C or Java

type name(arguments){
    return expression;

The to use the global type qualifier, just preceed the variable declaration with the keyword global

global int i=0;  

The following example defines two functions and declares three global variables

  global Vector debugQueries = new Vector();
  global Vector debugQueryTimes = new Vector();
  global Vector debugQueryResults = new Vector();

  MySQLResultSet doQuery(MySQLConnection conn, String qry){
     MySQLResultSet r;
     long t;

     r = conn.query(qry);

     debugQueryTimes.add(new Long(t));
     debugQueryResults.add(new Integer(r.size()));

     return r;

  void doDebugQueries(){
     int i;
        print "<p><pre>";
        print <String>debugQueries.get(i);
        print "</pre>\n";
        print "Results Returned: "+str((<Integer>debugQueryResults.get(i)).getValue()) + "<br>\n";
        print "Execution Time: "+str((<Long>debugQueryTimes.get(i)).getValue()) + "<br>\n";

3.10 Embedded Class Definitions

The syntax for defining a Class in an embedded block is the same as it is in Java

class class name {
    member variable declarations
    method definitions

The following example defines two classes


   class Question {
      int idQuestion;
      String qText;
      int idImageURL;
      int level;
      String explanation;
      int numanswers;
      int answers[];
      int canswer;
      int numtopics;
      int topics[];

   class QuestionManager {

      Question get(int idQuestion){
         int i;
         MySQLResultSet rset;
         Question q = new Question();
         rset = doQuery(conn,
            "SELECT q.* FROM tblQuestion q WHERE q.idQuestion = "+str(idQuestion));

            return null;
         q.idQuestion = idQuestion;   
         q.qText = rset.getString("qText");
         q.idImageURL = rset.getInt("idImageURL");
         q.level = rset.getInt("level");
         q.explanation = rset.getString("explanation");
           "SELECT a.idAnswer, qta.isCorrect " +
           "FROM tblQuestionToAnswer qta, tblAnswer a WHERE " +
           "   qta.idQuestion = "+str(idQuestion)+ " AND " +
           "   a.idAnswer = qta.idAnswer " +
           "ORDER BY a.aText"
        q.numanswers = rset.size();
        q.answers = new int[rset.size()];
           "SELECT ttq.idTopic " +
           "FROM tblTopicToQuestion ttq WHERE " +
           "   ttq.idQuestion = "+str(idQuestion)
          q.numtopics = rset.size();
          q.topics = new int[rset.size()];
        return q;


3.11 Embedded Try-Catch-Finally blocks

The syntax for try catch and finally is the same as it is in Java

try { ... }
catch (NullPointerException npe) { ... }
catch (Exception e) { ... }
finally { ... }

The syntax for throw is the same as well

throw new Exception();