I would like to write a Smarty3 modifier that picks a gender dependent word. The gender is defined in the template.
I could use a function like this:
{genderize txt=['maleVersion','femaleVersion','unknownVersion']}
but this looks cumbersome in a textflow. I would prefer something like this:
{'maleVersion|femaleVersion|unknownVersion'|genderize}
I don't know however how to access the $smarty variable from a modifier, like I can from a function.
function smarty_function_gender($parameters = array(), &$smarty)
As far as I know there is no way to access template vars from within a modifier, but you can just pass the template variable in as a parameter to the modifier.
Template:
That's pretty janky though, you're depending on your input always being ordered a certain way.
There are a couple of more robust ways you can approach this. If the only language you need to support is English, the number of gendered words is fairly limited, you probably just need to worry about third person pronouns, and there are only five different variations of each of those, it's a matrix. You can write a simple function to return the correct entry in the table. Since you're already defining the gender in a variable, you only need to pass one parameter into the function to return the desired value. Here's what such a function would look like. You may want to use more terse keys in the table, I was verbose for clarity.
Then in your template you can do this:
If you need to support other/multiple languages, or if you want to be more flexible with the content you choose between, you could assign the different content versions to variables and write an even simpler plugin to display the appropriate one.
Then in your template: