Вопрос

I need to use PHP file_get_contents() in smarty tpl file. I can'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 using smarty plugin function to achieve that task. But I want to know whether is there any way I can use it in template file directly instead of parsing it from plugin file.

I've attached the plugin code which I'm using to achieve this function. Please anyone let me know how to use it in smarty tpl file directly.

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'];
}
}

I've used below code to call it in smarty template:

{getTitle id=$videoId}

Suggestions are welcome!

Это было полезно?

Решение

For those of you reading that didn't read the comments above, myself and OP are both aware that this is not how you use a template engine. He seems to have his reasons for wanting to do this directly in the template rather than a plugin or ahead of time in his code. So don't slag me for demonstrating how, please :)

Here is how you can do it in Smarty.

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

I did the first part all in one call but if you want to be careful you can split it into multiple calls with checks. But I tested this locally and it worked fine.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top