wpart_include

wpart_include injects the content of a external file into a file.

Attributes

file

Specifies the path of the included file.

as

If present - inserts the content of the file into the as dictionary key.

format

If present - a format to apply to the value before printing it out.

Example

We will insert contents of the file priv/include/license.txt into the view:

license.html

  ...
    <h3>Here is our license: </h3>
    <pre>
      <wpart:include file="priv/include/license.txt" />
    </pre>
    <wpart:include file="priv/include/date.txt" format="YYYY-MM-DD" />
  ...

Here is the example, how to use as attribute:

copyright.html

  ...
    <wpart:include file="priv/include/copyright.sig" as="copyright" />
    This file is <wpart:lookup key="copyright" />.
  ...
    And here is another place where we are using wpart:lookup:
    <wpart:lookup key="copyright" />
  ...

Program Tutorial

wpart:include

For the following examples it is assumed you have erlang-web installed and have permissions setup. And a few reminders to verify before you actually can start following the sample.

You will need to know the path to the html program. In this example, I have a separate path from the template directory to a subdirectory called mw. I place my html files in the mw subdirectory. The directory structure and the filenames:

home
 erl
  erlangweb-1.2.1
    templates
      mw
        content1.html
        content2.html
        content3.html
        footer.html
        index.html
        menu.html
        page2.html
        page3.html
        title1.html
        title2.html
        title3.html

Either create an mw directory or change the following sample to your directory name.

And you will need to change the config file (dispatch.conf) to give each page permission to run. The dispatch.conf can be found in:

home
 erl
  erlangweb-1.2.1
    config
      dispatch.conf

In the dispatch.conf file, after the line, {static, "mw/.*", enoent}, you will need to add

{static, "content1", "mw/content1.html"}.
{static, "content2", "mw/content2.html"}.
{static, "content3", "mw/content3.html"}.
{static, "footer", "mw/footer.html"}.
{static, "index", "mw/index.html"}.
{static, "menu", "mw/menu.html"}.
{static, "page2", "mw/page2.html"}.
{static, "page3", "mw/page3.html"}.
{static, "title1", "mw/title1.html"}.
{static, "title2", "mw/title2.html"}.
{static, "title3", "mw/title3.html"}.

After modifying dispatch.conf it will need to be loaded, you will need to either restart erlangweb or run e_dispatcher:reinstall()

Once you have the above complete, you are ready to start. To use wpart:include, you will need to identify the sections of code that you will be putting into external files.

NOTE: A quick comment about xmerl, which parses xml. It is a part of Erlang/OTP and is used extensively in Erlang Web. Quite often though, it has problems with external languages such as CSS or Javascript. In order to prevent issues, use <link rel="stylesheet" ... /> instead of embedding the CSS file in the document using wpart:include.

Looking at the example code below, the common code is:

title
menu
content
footer

Here is the html for two pages before modification:

home.html:

<html>
 <head>
  <title>Home</title>
 </head>
 <body>
  <p>Menu Item 1</p>
  <p>Menu Item 2</p>
  <p>Menu Item 3</p>
  <br />
  <p>Welcome to the home page for erlang-web.</p>
  <a href="index.html">Home</a> - 
  <a href="page2.html">Page2</a> 
 </body>
</html>

And for page2.html:

<html>
 <head>
  <title>Page 2</title>
 </head>
 <body>
  <p>Menu Item 1</p>
  <p>Menu Item 2</p>
  <p>Menu Item 3</p>
  <br />
  <p>Welcome to Page 2 for erlang-web.</p>
  <a href="index.html">Home</a> - 
  <a href="page2.html">Page2</a> 
 </body>
</html>

Looking at the above samples, the title, menu, content and footer are quite easy to spot. Now simply cut the respective pieces from the program, insert the wpart:include command in their places and paste the cut code into their own files:

home.html:

<html>
 <head>
  <wpart:include file="templates/mw/title1.html" format="html"/>
 </head>
 <body>
  <wpart:include file="templates/mw/menu.html" format="html" type="cdata"/>
  <wpart:include file="templates/mw/content1.html" format="html type="cdata""/>
  <wpart:include file="templates/mw/footer.html" format="html" type="cdata"/>
 </body>
</html>

And for page2.html:

<html>
 <head>
  <wpart:include file="templates/mw/title2.html" format="html" type="cdata"/>
 </head>
 <body>
  <wpart:include file="templates/mw/menu.html" format="html" type="cdata"/>
  <wpart:include file="templates/mw/content2.html" format="html" type="cdata"/>
  <wpart:include file="templates/mw/footer.html" format="html" type="cdata"/>
 </body>
</html>

For the home page title1.html:

  <title>Home</title>

The contents for title2.html

  <title>Page 2</title>

For the menu.html file:

  <p>Menu Item 1</p>
  <p>Menu Item 2</p>
  <p>Menu Item 3</p>
  <br />

For the content1.html file:

  <p>Welcome to the home page for erlang-web template sample.</p>

For the content2.html file:

  <p>This is Page 2 for erlang-web template sample.</p>

And for the footer.html file:

  <a href="index.html">Home</a> - 
  <a href="page2.html">Page2</a>

As you can see in the above examples, each page is much smaller and you can rapidly add new pages by copying the home.html code and merely changing the content part of the program. The rest of the program won't change. As you add pages, the footer would be changed, but by merely adding the new html elements to footer.html, all of the pages will be updated with the change.

For the external include files, DO NOT add any other html tags. Simply cut the code from the existing page and place it into its own file as shown in the above samples. You will not need to change the config file permissions for the new html files as they will be part of the existing pages and not used separately.

To add a third page, copy the home.html code and change the wpart:include names for the title and the content:

page3.html

<html>
 <head>
  <wpart:include file="templates/mw/title3.html" format="html"/>
 </head>
 <body>
  <wpart:include file="templates/mw/menu.html" format="html"/>
  <wpart:include file="templates/mw/content3.html" format="html"/>
  <wpart:include file="templates/mw/footer.html" format="html"/>
 </body>
</html>

And add two new files, title3 and content3:

title3.html

  <title>Page 3</title>

content3.html

  <p>Page 3 for erlang-web template sample. Hey, this is snazzy.</p>

And finally, you will need to add the new page to footer.html

  <a href="index.html">Home</a> - 
  <a href="page2.html">Page2</a> - 
  <a href="page3.html">Page3</a>

The new page is now ready and the footer will be updated on all existing pages.

If you have trouble with your program, you can check the report.log file. The log is descriptive and can help you pinpoint the cause of the error. Typically by looking at the last 50 lines of messages (tail -50 report.log) you will find what you need.

home
 erl
  erlangweb-1.2.1
    log
      report.log

After you have finished running the sample code and no longer need it, for a secure system, remember to remove the sample pages from the dispatch.conf file. And last, either restart erlangweb or run e_dispatcher:reinstall() to reload the dispatch.conf file.

Wparts/WpartInclude (last edited 2009-07-21 03:56:40 by mwillson)