Tutorial 1 - Referenced Modules

This example is the most basic page that can be created with ModPages. It simply displays "Welcome to ModPro!" on the page. The header is defined in an inline module and the body of the page is defined in a referenced module. It references mpml.xsl using the <?xml-stylesheet?> processing instruction.

The entire example can be viewed at the bottom of this page. See tutorial/ tutorial1/ tutorial1.xml to view the entire file with comments.

The first thing you must do is add the processing instructions. ModPro allows you to specify any number of xml-stylesheet instructions that point to the xsl files that will be used to process the file. Usually you will have at least a reference to the base ModPro XSL file, mpml.xsl that contains all the basic processing templates.

<?xml-stylesheet type="text/xsl" href="../xsl/mpml.xsl"?>
      

Next comes the <mpml> element. The root element of a ModPro file must always be <mpml>. It should define the MPML namespace. Usually the namespace is aliased as "mp". From this point forward all MPML elements are prefixed with "mp:", for exampe <mp:mpml> This allows the compiler to know which elements are MPML elements. All other elements are assumed to be HTML elements and are output as-is.

<mp:mpml xmlns:mp="http://www.worldtreesoftware.com/mpml">
      

Now it's time to define your HTML and modules. The <html> element is the entry point that ModPro starts processing the document from. From there it moves through the document writing out HTML elements and replacing modules with the module's content.

The first module it hits is an inline module named "headerContent". Using an inline module in this context is not very useful, but it is used for custom modules quite a bit so we should learn it here. The module is defined with the <mp:module> element. The name attribute is not required on an inline module but does help to identify it. A module can contain HTML markup and references to other modules.

<mp:module name="headerContent">
  <title>Tutorial 1</title>
</mp:module>
      

Inside the <body> element we see a module reference. A module reference acts like a placeholder for a module defined somewhere else. The ref attribute tells it which module to reference. This reference will be replaced with the contents of the module it references.

<mp:module ref="bodyContent"/>
      

Now we will look at the definition of the "bodyContent" module. Referenced modules are defined outside of the <html> element. They are defined just like an inline module except that the name attribute is required. You can define as many modules as you want and modules may be defined in external files, which we will see in the next tutorial.

<mp:module name="bodyContent">
  <h1>Welcome to ModPro!</h1>
</mp:module>
      

Here is what the entire example tooks like.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<mp:mpml xmlns:mp="http://www.worldtreesoftware.com/mpml">

  <html>
    <head>
      <mp:module name="headerContent">
        <title>Tutorial 1</title>
      </mp:module>
    </head>
    <body>
      <mp:module ref="bodyContent"/>
    </body>
  </html>
  
  <mp:module name="bodyContent">
    <h1>Welcome to ModPro!</h1>
  </mp:module>

</mp:mpml>
      

Now we need to compile the file. To do this open a command prompt and navigate to the directory that contains tutorial1.xml. I can recommend a good (free) tool in Windows to open a command prompt anywhere from Explorer called Open Command Window Here. Start the compiler by running the following command.

..\..\mpc -out:*.html *.xml
      

This starts the compiler by running mpc.bat in the ModPro directory. If you don't want to type the path all the time you can add the directory that mpc.bat is in to your path environment variable.

The -out:*.html option tells the compiler to save the compiled HTML files to the current directory with the name of the source file and an extension of html. The *.xml tells it which source files to compile. In this case it will compile all files with the xml etension in the current directory.

You will also find a file called make.bat in all of the tutorial directories. It contains the above command to run the compiler so you don't have to type it by hand every time. I suggest you create a similar batch file for all of your ModPro projects then you won't have to do so much typing every time you compile. Just type "make" on the command line or execute it from a folder and it's all done for you.

After compiling you should see tutorial1.html created.

Next Tutorial>