问题
I want change content of a block with a condition (isset($file)) but i saw an error Missing template name when i run my code
public function food_list()
{
$m_food=new M_food();
$arr=$m_food->food_list();
$smarty=new Smarty_restaurant();
$smarty->assign("title","New food");
//$smarty->assign("file","../../views/v_food_list.tpl");
//$smarty->assign("arr",$arr);
$smarty->display ("food_list.tpl");
}
And this is food_list.tpl file
{extends file="index.tpl"}
{block name="head"}{include file="head-food.tpl"}{/block}
{block name="slide"}{/block}
{if isset($file)}
{block name="content"}{include file=$file}{/block}
{else}
{block name="content"}{/block}
{/if}
I got an error Missing template name when i add // before $smarty->assign("file","../../views/v_food_list.tpl") I need a detail answer for my problem,please help me.Thank you so much
回答1:
The problem is that {block}
are compiled before the rest of Smarty file, so you should try to change your Smarty file into:
{extends file="index.tpl"}
{block name="head"}{include file="head-food.tpl"}{/block}
{block name="slide"}{/block}
{block name="content"}
{if isset($file)}
{include file=$file}
{/if}
{/block}
回答2:
The reason is the variable $file is unset or empty. You should make sure your variable $file is not empty and its value is valid template path.
来源:https://stackoverflow.com/questions/26045425/uncaught-exception-smartyexception-with-message-missing-template-name-in-smar