The parameters intro and outro indicate mediawiki text to be shown if some result exists (befor and after the result).
The parameter default indicates mediawiki text to be shown if NO result exists.
This obviously works as expected, but strangely enought if you set a property (or a variable with the variable extension). The property value gets set even it the related text should not be processed.
{{#ask:[[XXX]]
|intro=Here are the results: {{#set:Result1=INTRO}}
|outro=These were the results! {{#set:Result1=OUTRO}}
|default=There are NO results! {{#set:Result1=DEFAULT}}
}}
The first query only displays "There are NO results!" and the value "DEFAULT" gets set for the property "Result1". This is desired behaviour.
However, the property Result1 also gets the values "INTRO" and "OUTRO" event though these should be omitted.
This can't be expected behaviour.
This can't be expected behaviour.
It is the __expected__ behaviour because when MediaWiki "sees" the raw text it splits the text into "tokens" that are encapsulate by a parser function such as {{ ... }} and processes the content of those functions independently (or in some sort of order).
It means #ask doesn't know nothing about #set that may be part of the intro, outro, or default parameter text and therefore __cannot__ influence the behaviour to make it appear as "Hey, #set you are part of a #ask invocation and we don't have any results, so don't you dare to begin to annotate any values!".
Maybe you could try sending some messages via the parser and find out whether #set is part of a #ask parse or not but all this will __not__ work in case #set is processed before (and my guess it is) the #ask parser function and the "when" is decided by the MediaWiki parser (SMW has no influence over the invocation or the text processing).
Hmm, I would expect that all values are set for intro and outro since both should process what one added to them unconditionally. I put the "question" lable to this in the light of James' reply, however I think that default may not work as expected since a values is set for the second example even though default should not be triggered because the query yields results. In this case it this would be and enhancement for default. Personally I would not like to change the behaviour of intro and outro as it can currently be observed.
As James pointed out, unless MW changes its way of parsing the wikitext there's nothing SMW can do here. MW is a templating system, not a programming environment (*). The effect is that _all_ parameters to _all_ parser functions are _always_ evaluated. This is by the way the same issue that regularly trips up users of #if.
This does not work as expected, because it will set both values:
{{#if: some condition | {{#set: prop=Yay.}} | {{#set: prop=Nope.}} }}
The correct way to do it is
{{#set: prop={{#if: some condition | Yay. | Nope.}} }}
(*) Lua though...
Ah, yeah you are right. The second solution is what I actually do in several places all over wikis. Senior moments I guess. :)