如何在智能修饰符插件中访问模板变量的值?
我想编写一个Smarty3修饰符,该修饰符选择一个依赖性别的单词。性别在模板中定义。
我可以使用这样的函数:
{genderize txt=['maleVersion','femaleVersion','unknownVersion']}
但是这在文本流中看起来很麻烦。我更喜欢这样的东西:
{'maleVersion|femaleVersion|unknownVersion'|genderize}
但是我不知道如何从修饰符中访问$ smarty变量,就像我可以从功能中访问。
function smarty_function_gender($parameters = array(), &$smarty)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,无法从修饰符内访问模板vars,但是您只需将模板变量作为参数传递给修饰符即可。
模板:
不过,这很笨拙,您依赖于输入总是以某种方式订购。
您可以通过几种强大的方法来解决此问题。如果您需要支持的唯一语言是英语,则性别单词的数量相当有限,您可能只需要担心第三人称代词,每个代词只有五个不同的变体,这是一个矩阵。您可以编写一个简单的功能以返回表中的正确条目。由于您已经在变量中定义性别,因此您只需要将一个参数传递到该函数中即可返回所需的值。这就是这样的功能的样子。您可能想在桌子上使用更多的简短键,为了清楚,我是冗长的。
然后,在模板中可以执行此操作:
如果您需要支持其他/多种语言,或者如果您想更灵活地使用您选择的内容,则可以将不同的内容版本分配给变量,并将更简单的插件写入显示合适的一个。
然后在您的模板中:
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: