Symfony2:如何在视图模板内的函数中获取资产的URL
As documented here I can read my image files in view by using following code.
<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>" alt="Symfony!" />
//// This outputs the image
or I can also do
<?php
echo $view['assets']->getUrl('images/logo.png');
// echoes ---> /assets/images/logo.png
?>
However, my views are much complex and I want to divide different sections of view in functions. So when I write the above code in a function, it doesn't work.
function one(){
echo $view['assets']->getUrl('images/logo.png');
}
one();
Notice: Undefined variable: view in ....\Resources\views\Section\splash.html.php on line 12
Fatal error: Call to a member function getUrl() on a non-object in ....\Resources\views\Section\splash.html.php on line 12
Can someone please guide me how can I get this to work?
This is my full view file.
<?php
echo $view['assets']->getUrl('images/logo.png') . "<br><br>";
function one(){
echo $view['assets']->getUrl('images/logo.png') . "<br><br>";
}
one();
?>
<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>" alt="no image" />
This is how I am calling the view from my controller
return $this->render('MySimpleBundle:Section:splash.html.php');
如记录所示这里我可以使用以下代码在视图中阅读我的图像文件。 p>
&lt; img src = “&lt;?php echo $ view ['assets'] - &gt; getUrl('images / logo.png')?&gt;” ALT = “Symfony的!” /&gt;
////这会输出图像
code> pre>
或者我也可以 strong> p>
&lt;?php
echo $ view ['assets'] - &gt; getUrl('images / logo.png');
//回声---&gt; /assets/images/logo.png
?>
nn但是,我的观点非常复杂,我想在功能中划分不同的视图部分。 因此,当我在函数中编写上述代码时,它不起作用。 p>
function one(){
echo $ view ['assets'] - &gt; getUrl ( '图像/ logo.png');
}
one();
Notice:未定义的变量:第12行的.... \ Resources \ views \ Section \ splash.html.php中的视图
Fatal错误:调用成员函数getUrl()on 第12行的\\ Resources \ views \ Section \ splash.html.php中的非对象
code> pre>
有人可以指导我如何获得 这是工作吗? p>
这是我的完整视图文件。 p>
&lt;?php
echo $ view ['assets'] - &gt; getUrl('images / logo.png')。 “&lt; br&gt;&lt; br&gt;”;
function one(){
echo $ view ['assets'] - &gt; getUrl('images / logo.png')。 “&lt; br&gt;&lt; br&gt;”;
}
one();
?&gt;
&lt; img src =“&lt;?php echo $ view ['assets'] - &gt; getUrl('images /logo.png')?&gt;“ alt =“no image”/&gt;
code> pre>
这是我从控制器调用视图的方式 p>
返回$ this-&gt; render('MySimpleBundle:Section:splash.html.php');
code> pre>
div>
The function "one()" has his own variable scope, so you have to pass the url into the function http://php.net/manual/en/language.variables.scope.php
function one($url)
{
echo $view['assets']->getUrl($url) . "<br><br>";
}
anyway, in my opinion you should use simply:
<img src="<?php echo $view['assets']->getUrl('images/logo.png') ?>" alt="Symfony!" />
to render the image, if your view gets to complex, it is may the right time to switch to a powerful templating engine like twig
Actually, it is the variable $view
as the notice says.
You should either pass $view
as a parameter or declare it global inside the function scope:
function one($view) {
echo $view['assets']->getUrl('images/logo.png');
}
or
function one() {
global $view;
echo $view['assets']->getUrl('images/logo.png');
}