Include / Require

The “include” and “require” command lets you embed one script inside another script. This helps make your projects more manageable and speeds up development time as it lets you reuse your code. You can also include LiveCode stacks.

  • include: Includes the given script each time it is called. Each time it is called it will reset any variables that are set in the included file.
    <?lc include "my_settings.lc" ?>
  • require: Only includes the given script if it has not already been included or required
    <?lc require "database_library.lc" ?>

Example 1. The difference between include and require

  • settings.lc contains the following:
    <?lc
    put "red" into myVar
    ?>
  • Using include
    include "settings.lc"
    put myVar // outputs "red" as its set in the included file
    put "blue" into myVar
    put myVar // outputs "blue"
    include "lc/settings.lc"
    put myVar // outputs "red" as the myVar value
  • Using require
    require "settings.lc"
    put myVar // outputs "red" as its set in the required file
    put "blue" into myVar
    put myVar // 
    require "lc/settings.lc"
    put myVar // outputs "blue" as the myVar value

Example 2. Embedding a menu script

Create a file called menu.lc

<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>

The above menu can now be embedded in a page by using the inlcude command:

<?lc include "database_library.lc" ?>

Leave a Reply