在Smarty中有两条不同的路径


Two different paths in Smarty?

我在我的php项目中使用smarty

|->app
    |view
        |path_returned_by_handler.tpl
|->lib
    |abstract
        |controller.php
    |view
        |path_to_be_used_by_controller.tpl

有一些处理程序返回路径到一些sub_template,但在显示我想使用/lib/view/path_to_be_used_by_controller.tpl在这个文件中,我将使用handler返回的路径{include file="$path_returned_by_handler" title="sub_temp"}

我该怎么做?1)一个解决方案可能是我使用绝对路径,而不是使用setTemplateDir在所有或设置到顶级父目录,并使用相对路径从那里。有更好的解决方案吗?

如果你想使用相对路径,你需要在include

的开头使用。/

结构:

| index.tpl
  |test
      test.tpl
      test3.tpl
      |test2
           test2.tpl

index.tpl

{include file="test/test.tpl"}

test.tpl

I'm test<br />
{include "./test3.tpl"}
{include "./test2/test2.tpl"}

test2.tpl

I'm test 2<br />

test3.tpl

I'm test 3<br />

显示索引的结果。tpl是:

I'm test
I'm test 3
I'm test 2

所以模板中的相对路径工作得很好。在最新的Smarty 3.1.18中进行了测试