Templates 有没有办法为Smarty自定义函数使用变量名?

Templates 有没有办法为Smarty自定义函数使用变量名?,templates,smarty3,Templates,Smarty3,我们正在为Smarty使用自定义函数。由于我们使用单独的子模板来构建大模板,所以我们面临一个问题:有时一个子模板在一个页面中使用不止一次。因此,用户定义的smarty函数声明了两次 我们尝试为函数使用变量名,如 {function name = menu_{$object_key}} ... {/function} 但它不起作用。我们是做错了还是没有办法?有什么想法吗 提前谢谢 注:由于我们结构的复杂性,跟踪“如果函数在之前声明”是不可能的(或至少太难了)。如果您想声明一次函数,可能的解

我们正在为Smarty使用自定义函数。由于我们使用单独的子模板来构建大模板,所以我们面临一个问题:有时一个子模板在一个页面中使用不止一次。因此,用户定义的smarty函数声明了两次

我们尝试为函数使用变量名,如

{function name = menu_{$object_key}} 
... 
{/function} 
但它不起作用。我们是做错了还是没有办法?有什么想法吗

提前谢谢


注:由于我们结构的复杂性,跟踪“如果函数在之前声明”是不可能的(或至少太难了)。

如果您想声明一次函数,可能的解决方案是:

  • 使用内部使用的自定义函数制作单独的模板:
  • 功能。tpl

        {function name = menu} 
        ... 
        {/function} 
    
        {function name = another_func} 
        ... 
        {/function} 
    
        {include file='functions.tpl'}
    
        {menu data=$menu}
        {include file='sub_template.tpl'}
    
        {another_func}
    
  • 然后将其包含在主模板中:
  • main.tpl

        {function name = menu} 
        ... 
        {/function} 
    
        {function name = another_func} 
        ... 
        {/function} 
    
        {include file='functions.tpl'}
    
        {menu data=$menu}
        {include file='sub_template.tpl'}
    
        {another_func}
    
  • 在主模板和任何子模板中调用函数
  • main.tpl

        {function name = menu} 
        ... 
        {/function} 
    
        {function name = another_func} 
        ... 
        {/function} 
    
        {include file='functions.tpl'}
    
        {menu data=$menu}
        {include file='sub_template.tpl'}
    
        {another_func}
    
    sub_template.tpl

        {function name = menu} 
        ... 
        {/function} 
    
        {function name = another_func} 
        ... 
        {/function} 
    
        {include file='functions.tpl'}
    
        {menu data=$menu}
        {include file='sub_template.tpl'}
    
        {another_func}
    

    谢谢,但是我们必须在main.tpl中多次包含相同的.tpl(使用不同的变量),所以这对我们不起作用。这些次要的tpl是构建块,如横幅、菜单、内容框等,它们以不同的组合动态组合在一起,形成网页。