Inserting a block

You can use <insert>, <expand>, or <call> to make an individual <block> available.

<insert>, <expand>, and <call> all import a block, but differ in their ability to access the encapsulating block's variables.
  • <insert> - place a block of code into the current query, as well as access the encapsulating block's variables.
  • <expand> - place a block of code into the current query, as well as access and modify the encapsulating block's variables.
  • <call> - place a block of code into the current query, but allow no access to the encapsulating block's variables.

Notice that every <block> is given a name. This name is how you tell <insert> (or <expand> or <call>) which <block> to make available in your current query.

Here's what a sample looks like:

<insert block="storeSelection" storechoice="3"/>

Using the <insert> element makes this block available in your code. Even better, if you combine this operation with the <import> operation, the <block> doesn't even need to physically appear in your query code. Here's a quick example:

<import path="pub.demo.retail.selectionlib"/>
<insert block="storeSelection" storechoice="3"/>

The two lines in the example above perform the exact same action as:

<block name="storeSelection" storechoice="3">
  <sel value="store={@storechoice}"/>
</block>

At the same time, the above code expands to:

<sel value="store=3"/>

While you could retype the line above a hundred times in a hundred different queries, you would need to change each and every one any time something about your analysis changed. Blocks and libraries give you tools to reuse your code, and to update every instance where it's in use by changing it in a single location. As you continue to explore these concepts, look for places where you reuse code and ask yourself if a <library> of <block> statements would be a better solution.