Help:Parser Functions
- Help:Parser Functions
Parser functions are a powerful feature of MediaWiki that allow you to perform calculations, manipulate strings, and control the output of your wiki pages dynamically. They are essentially functions that are processed *within* the wiki text before the page is rendered, allowing for complex logic and content generation. This guide provides a comprehensive introduction to parser functions for beginners, covering their syntax, common functions, and practical examples.
== What are Parser Functions?
Unlike extensions which require server-side scripting and modification, Parser Functions are built directly into MediaWiki and operate solely on the text of a wiki page. This makes them easy to use and deploy – no administrator intervention is necessary. They are used to:
- Perform calculations (arithmetic, dates, etc.).
- Manipulate text strings (extracting parts, replacing characters, etc.).
- Conditionally display content based on certain criteria.
- Create loops and iterate over data (to a limited extent).
- Format data in specific ways.
- Improve the reusability of content through templates.
Parser functions are invoked using the `#` symbol followed by the function name and any necessary parameters, all enclosed within double curly braces `Template:...`. For example: `{{#if:condition|then|else}}`. The result of the function is then substituted into the page content.
== Basic Syntax
The general syntax for a parser function is:
`{{#function_name: parameter1 | parameter2 | ...}}`
- `#`: Indicates that this is a parser function.
- `function_name`: The name of the function you want to use (e.g., `if`, `expr`, `time`).
- `parameter1`, `parameter2`, etc.: The inputs to the function. Parameters are separated by vertical bars `|`. These parameters can be literal values, variables, or even other parser functions.
Note the following important points:
- Parser functions are *case-sensitive*. `#IF` is not the same as `#if`.
- Spaces around the `|` separators are generally ignored, but it’s good practice to include them for readability.
- Some functions accept a variable number of parameters, while others require a specific number.
- The output of a parser function is always a string. If you need a numerical value for calculations, you may need to use the `#expr` function to ensure it's treated as a number.
== Common Parser Functions
Here's a breakdown of some of the most commonly used parser functions:
- 1. `#if` – Conditional Statements
The `#if` function tests if an expression evaluates to a non-empty string. If it does, it returns the "then" part; otherwise, it returns the "else" part.
Syntax: `{{#if: expression | then | else }}`
Example:
`{{#if: | This message is visible. | This message is hidden.}}`
This will display "This message is visible." if the parameter `show_message` is set to any non-empty value. Otherwise, it will display "This message is hidden.". This is heavily used in Template:Infobox.
- 2. `#ifeq` – Equality Check
The `#ifeq` function checks if two values are equal.
Syntax: `{{#ifeq: value1 | value2 | then | else }}`
Example:
`{{#ifeq: | active | This account is active. | This account is inactive.}}`
This will display "This account is active." if the `status` parameter is equal to "active".
- 3. `#switch` – Multi-way Conditional
The `#switch` function allows you to test a value against multiple possible cases.
Syntax: `{{#switch: expression | case1 | result1 | case2 | result2 | ... | default }}`
Example:
`{{#switch: | red | The color is red. | green | The color is green. | blue | The color is blue. | The color is unspecified.}}`
This will display a message based on the value of the `color` parameter. If the `color` parameter is not one of the specified cases, it will display "The color is unspecified.". This is often used in Help:Categorization for determining article classification.
- 4. `#expr` – Arithmetic Expressions
The `#expr` function evaluates a mathematical expression. It supports basic arithmetic operations (+, -, *, /, %), comparison operators (>, <, >=, <=, ==, !=), and parentheses for grouping.
Syntax: `{{#expr: expression }}`
Example:
`{{#expr: 10 + 5 * 2}}` will return `20`.
`{{#expr: (10 + 5) * 2}}` will return `30`.
This is particularly useful for Technical analysis calculations like moving averages or RSI values. Consider a simple calculation of percentage change: `{{#expr: ((value - previous_value) / previous_value) * 100}}`.
- 5. `#time` – Date and Time Formatting
The `#time` function formats a timestamp according to a specified format string.
Syntax: `{{#time: format | timestamp }}`
If no timestamp is provided, the current time is used. The format string uses date and time codes (e.g., `Y` for year, `m` for month, `d` for day, `H` for hour, `i` for minute, `s` for second). See the MediaWiki documentation for a complete list of format codes.
Example:
`{{#time: Y-m-d H:i:s}}` will display the current date and time in the format "2023-10-27 10:30:00".
This is helpful for displaying the last modified date of a page or for creating time-sensitive content. It can also be integrated with Candlestick pattern analysis to show the time of specific formations.
- 6. `#formatnum` – Number Formatting
The `#formatnum` function formats a number with separators and a specified number of decimal places.
Syntax: `{{#formatnum: number | decimal_places | separator }}`
Example:
`{{#formatnum: 1234567.89 | 2 | ,}}` will return "1,234,567.89".
This is useful for displaying large numbers in a more readable format. It's often used in Financial modeling to present monetary values.
- 7. `#titleparts` – Extracting Parts of a Title
The `#titleparts` function extracts specific parts of a page title.
Syntax: `{{#titleparts: title | part_number | separator }}`
Example:
`{{#titleparts: My Page Title | 2 | /}}` will return "Page".
This is helpful for creating dynamic links or extracting information from page titles.
- 8. `#property` – Accessing Property Data
The `#property` function retrieves the value of a specified property from a linked data source. This requires the appropriate extensions to be enabled on the wiki.
Syntax: `{{#property: entity | property }}`
Example: (assuming the appropriate extensions are set up)
`{{#property: Q76 | population}}` might return the population of France (Q76). This relates to Data analysis and external data sources.
- 9. `#ask` – Querying Semantic Data
The `#ask` function queries a wiki with semantic data (using Semantic MediaWiki). It returns a formatted list of results based on specified criteria.
Syntax: `{{#ask: query }}`
Example: (assuming Semantic MediaWiki is installed and data is defined)
`{{#ask: | [[Price>100]] | list}}` might return a list of stocks in the "Stocks" category with a price greater than 100. Crucial for Trend analysis and filtering data.
- 10. `#categorytree` – Displaying Category Hierarchies
The `#categorytree` function displays a hierarchical tree of a specified category and its subcategories.
Syntax: `{{#categorytree: category | depth | hideleafs }}`
Example:
`{{#categorytree: Stocks | 2 | false}}` will display a tree of the "Stocks" category and its first two levels of subcategories, including leaf categories. Useful for Market segmentation and overview of investment areas.
== Combining Parser Functions
The real power of parser functions comes from their ability to be combined. You can nest functions within each other to create complex logic.
Example:
`{{#if: | {{#expr: {{{value1}}} + 0 }} | Value1 is missing.}}`
This example first checks if the `value1` parameter is set. If it is, it adds `value1` and `value2` (defaulting to 0 if `value2` is not set). If `value1` is not set, it displays the message "Value1 is missing.".
This demonstrates how to create more robust and flexible templates. This is fundamental to developing Trading strategies that adapt to changing conditions.
== Limitations
While powerful, parser functions have some limitations:
- **Performance:** Complex parser functions can slow down page rendering, especially on high-traffic pages.
- **Recursion:** Parser functions do not support recursion. This means you cannot call a function from within itself.
- **Error Handling:** Error handling is limited. If a function encounters an error, it usually returns an empty string or a generic error message.
- **Complexity:** Deeply nested functions can become difficult to read and maintain.
- **Looping:** True looping constructs are limited. While you can simulate looping with templates and recursive templates (which have performance implications), it's not ideal.
Consider using extensions like Lua scripting for more complex logic if performance or functionality becomes a concern.
== Best Practices
- **Keep it simple:** Avoid overly complex expressions. Break down complex tasks into smaller, more manageable functions.
- **Use descriptive names:** Choose meaningful names for your parameters and variables.
- **Test thoroughly:** Test your parser functions with different inputs to ensure they work as expected.
- **Document your code:** Add comments to explain what your functions do.
- **Consider performance:** Be mindful of the performance implications of complex functions.
- **Use templates:** Encapsulate complex logic in templates to improve reusability and maintainability. Templates are essential for Backtesting and simulating trading scenarios.
- **Understand the limits:** Be aware of the limitations of parser functions and consider using extensions if necessary.
== Resources
- Help:Templates
- Help:Categories
- Help:Semantic MediaWiki
- MediaWiki Parser Functions Documentation: [1](https://www.mediawiki.org/wiki/Manual:Parser_functions)
- Parser Functions Cheat Sheet: [2](https://en.wikipedia.org/wiki/MediaWiki_parser_function_cheatsheet)
Understanding and utilizing Parser Functions can significantly enhance the functionality and dynamic capabilities of your MediaWiki wiki. Mastering these tools is key to building a powerful and informative resource for your community. Applying these principles can improve the accuracy of Elliott Wave Theory analysis, refine Fibonacci retracement calculations, and provide a framework for Bollinger Bands based signals. Furthermore, it facilitates the creation of tools for MACD crossover detection and the implementation of Relative Strength Index alerts. Successful Day trading often requires flexible tools built using such functions.
Start Trading Now
Sign up at IQ Option (Minimum deposit $10) Open an account at Pocket Option (Minimum deposit $5)
Join Our Community
Subscribe to our Telegram channel @strategybin to receive: ✓ Daily trading signals ✓ Exclusive strategy analysis ✓ Market trend alerts ✓ Educational materials for beginners