Many IntelliSurvey tags support a syntax known as a here document (heredoc), common in several programming languages. This allows survey code to span multiple lines instead of being constrained to a single line.
1. When did you last use your credit card?
type: radio
1. Within the past 6 months
2. 6-12 months ago
3. 1-2 years ago
4. More than 2 years ago
USER. User Type
type: coded single select
selectby: calculate
cvalue: <<END
if (anyChecked($Q1,2..4)) {1}
elsif (anyChecked($Q1,1)) {2}
END
1. Lapsed User
2. Current User
Details
- Heredoc syntax uses the insertion operator
<<
followed by a word (also called a delimiter) chosen by the programmer. - After the code block, the delimiter is repeated to indicate the end of the heredoc. Use a distinct, all-caps word like "END" for the delimiter to make it stand out from the rest of the code.
- Some code must span several lines to work properly, for example, blocks and inline custom ranks on ranking questions.
- Programmers may choose to use heredoc with other tags and widgets for clarity and readability. For example:
- The
rows
,columns
,rowsfrom
, andcolsfrom
tags support heredoc formatting if desired. - Heredoc can also be used on the tags
text
,html
, andmarkdown
.
- The
Additional examples
Blocks
Blocks use heredoc syntax to loop through sections of a survey several times.
set list: PRODUCTS
1. soap
2. dog food
3. clothing
block: <<ENDBLOCK
3X%%ID%%. Have you purchased %%TEXT%% in the past 6 months?
type: radio
1. Yes
2. No
ENDBLOCK
list: PRODUCTS
The 'markdown' tag and text widgets
Text widgets such as set text
and show text
often use heredoc to incorporate HTML or Markdown syntax. The example below uses heredoc to accommodate the line breaks necessary for markdown.
Syntax | Survey page |
show text: MY_MARKDOWN markdown: <<END * This is an *un-ordered* list * This is an **un-ordered** list item * Another list item 1. *Numbered* sub-list item 2. Another **numbered** sub-list item END |
Inline custom ranks
To specify an inline list of alternative ranks on a ranking question, list the ranks within a heredoc. The present as: pulldown
tag must also be included.
set list: REASONS
randomize: y
1. Quality of public schools
2. Neighborhood safety
3. Town amenities (public parks, greenways, etc.)
4. Property values
5. Proximity to work
6. Proximity to friends/family
97. Other {autoother: y; placeholder: specify} {anchor: y}
10. What are the <b>three most important</b> reasons you chose to move to this area?
type: rank
ranks:<<END
1. Most Important
2. Second Most Important
3. Third Most Important
END
optsfrom: REASONS
rank: 3
present as: pulldown
Validations
Validations can be written in a single line or in multiple lines. Multi-line validations differ slightly based on whether they are applied to a question or a table.
For questions, if the validations are written in multiple lines and are attached to the question widget, heredoc syntax is required to permit the line breaks.
set list: FRUIT
1. Bananas
2. Grapes
3. Apples
4. Watermelon
1. What fruits do you like?
type: checkbox
optsfrom: FRUIT
validations: <<END
validate: anyChecked($Q1,1)
message: Everyone likes bananas.
validate: anyChecked($Q1,2)
message: Everyone likes grapes.
END
For tables, the multi-line approach permits the use of heredoc syntax in the condition, allowing line breaks within the condition itself, which helps with code readability. The example below uses a single row validation condition
tag with heredoc to list multiple validations with a single tag.
set list: GROCERYSTORES 1. Trader Joes 2. Costco 3. Publix start table: 13 intro: For each store, indicate if you shop there. If you do, please specify the reason for shopping. If you don't, specify the reason you aren't a customer. row validation condition:<<ENDVAL
(anyChecked($Q13R[id]CA,1) and $Q13R[id]CB ne '') or
(anyChecked($Q13R[id]CA,2) and $Q13R[id]CC ne '')
ENDVAL
row validation message: Please specify the reason for shopping or not shopping.
rowsfrom: GROCERYSTORES -- column id: A type: radio 1. Do shop there 2. Never shop there required: y -- column id: B type: text size: 15 required: n heading: Why do you shop there? -- column id: C type: text size: 15 required: n heading: Why don't you shop there? end table
Using heredoc at a list call
Heredoc can be used in a list call to improve readability of long conditional statements. The list name appears on a separate line after the insertion operator <<
, and multiple decorators can be included. Once all logic is written, end the heredoc on the last line.
RESTAURANTS. Variable to select max 3 restaurants
type: checkbox
selectby: weight, counts
lock: y
maxgroups: 3
optsfrom: <<OPTS
RESTAURANT_LIST
{if
(noneChecked($QREST,[id]) and
anyChecked($QEVEREATEN,[id])) or
(noneChecked($QREST,[id]) and ([priority1])) or
anyChecked($QMAIN,[id])
}
{weight:
anyChecked($QMAIN,[id]) * 1000 +
anyChecked($QEVEREATEN,[id]) * 100
}
OPTS
Comments
0 comments
Please sign in to leave a comment.