User:Ganqqwerty/test page for newtocru
Parser function #ask
The basic way of writing an inline query is to use the parser function #ask. The query string (See selecting pages for syntax) and any printout statements are directly given as parameter, like in the following example:
Example: Using #ask function
{{#ask: [[Category:City]] [[located in::Germany]]
| ?population
| ?area#km² = Size in km²
}}
Here we query for all cities located in Germany, and two additional printout statements are used (a simple one and one with some extra settings). This displays the following result on a page:
| Population | Size in km² | |
|---|---|---|
| Berlin | 3,391,409 | 891.85 km²344.343 sqmi |
| Frankfurt | 679,664 | 248.31 km²95.872 sqmi |
| Munich | 1,353,186 | 310.43 km²119.857 sqmi |
| Stuttgart | 606,588 | 207.36 km²80.062 sqmi |
It is common to put the query as the first parameter behind #ask:. All other parameters are separated by |, just like for other parser functions. The exact formatting of the inline query is not essential, but it is good to use line breaks to make it more readable to other editors. As with all templates, one line per parameter, starting with the | is most accepted in practice.
Note that all the arguments to the #ask: function are ignored by the page parsing, hence the above example does not add a category or a «located in» property annotation to this page. A few more things to note are:
- The pipe '|' symbol is used to separate the conditions from the property to display.
- The conditions for display are a single argument to the #ask function, so there are no '|' symbols between them.
- White space and line breaks can be used within the #ask function, SMW is fairly flexible there.
- The format of the results display changes when you request display of additional properties. SMW picks an appropriate default format for query results, but you also have detailed control of the appearance of query results.
Knowing the basics of query string and printout statements therefore is enough to write many kinds of queries. But there are many cases where the standard table output of a query may not be the best choice, or where further settings are desired (like the maximum number of results that should be displayed). For this purpose, inline queries have a number of other possible parameters that one can use to control their appearance in detail. The general syntax for #ask therefore is the following:
{{#ask: argument 1 | argument 2 | … }}
Most of this page explains the various arguments one may use in inline queries.