Text Substitution:
The Sage_TextSubstitution node is designed to facilitate dynamic text manipulation by allowing you to substitute placeholders within a given text template with specified values. This node is particularly useful for generating customized text outputs based on variable inputs, making it a powerful tool for creating dynamic and personalized content. By leveraging a flexible placeholder system, you can easily replace parts of a text with corresponding values from a dictionary, ensuring that your text outputs are tailored to specific needs or contexts. The node also provides options to handle missing placeholders gracefully and to add prefixes or suffixes to the final output, enhancing its versatility and adaptability in various text processing scenarios.
Text Substitution Input Parameters:
text
This parameter represents the template text containing placeholders that you want to substitute with actual values. The placeholders are defined using a specific delimiter, and the text serves as the base structure for the substitution process. There is no explicit minimum or maximum value for this parameter, but it should be a valid string containing the placeholders you intend to replace.
delimiter
The delimiter is a character or string used to identify placeholders within the template text. By default, the delimiter is set to $, but you can customize it to suit your needs. This parameter is crucial as it defines how placeholders are recognized and replaced within the text.
prefix
The prefix parameter allows you to add a specific string at the beginning of the final substituted text. This can be useful for adding consistent introductory text or formatting to your output. If not specified, the default value is an empty string.
suffix
Similar to the prefix, the suffix parameter lets you append a specific string to the end of the final substituted text. This is helpful for adding closing remarks or formatting to your output. The default value is an empty string if not provided.
strings
This parameter is a dictionary containing key-value pairs where the keys correspond to the placeholders in the template text, and the values are the actual strings to substitute. The keys should start with str_ to be recognized as valid inputs for substitution. This parameter is essential for defining the dynamic content that will replace the placeholders in the template.
Text Substitution Output Parameters:
result
The result parameter is the final output string after all specified substitutions have been made. It reflects the template text with placeholders replaced by their corresponding values from the strings dictionary, along with any specified prefix or suffix. This output is crucial as it represents the customized and dynamically generated text based on the input parameters.
Text Substitution Usage Tips:
- Ensure that your template text is well-structured with clearly defined placeholders using the chosen delimiter to avoid substitution errors.
- Use the
stringsparameter effectively by providing a comprehensive dictionary of key-value pairs to achieve the desired text customization. - Experiment with different delimiters if you encounter conflicts with existing characters in your template text to ensure smooth substitution.
Text Substitution Common Errors and Solutions:
ValueError
- Explanation: This error may occur if there are invalid placeholders in the template text, possibly due to delimiter conflicts.
- Solution: Verify that the placeholders in your template text are correctly formatted and match the specified delimiter. Consider changing the delimiter if conflicts persist.
KeyError
- Explanation: This error arises when a placeholder in the template text does not have a corresponding key in the
stringsdictionary. - Solution: Ensure that all placeholders in the template text have matching keys in the
stringsdictionary. Use thesafe_substitutemethod to leave unmatched placeholders as-is if necessary.
