首页 文章

在smarty模板中替换php file_get_contents?

提问于
浏览
1

我需要在smarty tpl文件中使用PHP file_get_contents() . 我可以't use it in PHP and assign it to smarty template. Because the URL is generated dynamically through loop inside smarty template file. So I' m使用smarty插件功能来完成该任务 . 但我想知道是否有任何方法可以直接在模板文件中使用它而不是从插件文件中解析它 .

我附上了我用来实现这个功能的插件代码 . 请任何人让我知道如何直接在smarty tpl文件中使用它 .

function smarty_function_getTitle($params)
{
if ($params['id']) {
    $content = file_get_contents("http://youtube.com/get_video_info?video_id=".$params['id']);
    parse_str($content, $ytarr);
    return $ytarr['title'];
}
}

我用下面的代码在smarty模板中调用它:

{getTitle id=$videoId}

欢迎提出建议!

1 回答

  • 3

    对于那些没有阅读上述评论的读者,我和OP都知道这不是你使用模板引擎的方式 . 他似乎有理由想要在模板中直接执行此操作而不是插件或提前在他的代码中执行此操作 . 所以,请不要因为如何展示如何,请:)

    以下是如何在Smarty中完成的 .

    {"http://youtube.com/get_video_info?video_id=`$videoId`"|file_get_contents|parse_str:$result}
    {$result.title}
    

    我在一次通话中完成了第一部分,但是如果你想要小心,你可以将它分成带有支票的多个通话 . 但我在本地进行了测试,它运行良好 .

相关问题