0

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');
2
  • did you already changed the templating engine to php ? Commented Jun 24, 2014 at 5:20
  • @Nextar Yes I have. My php code <?php echo $view['assets']->getUrl('images/logo.png'); // echoes ---> /assets/images/logo.png ?> works. Commented Jun 24, 2014 at 5:47

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I don't know how I missed the basic variable scope issue? I had realized it short time after asking but +1 for answering.
1

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

3 Comments

This is mostly my answer copied, anyway using global variables is a bad practise and should ever be avoided
Thanks. I don't know how I missed the basic variable scope issue? I had realized it short time after asking but +1 for answering.
@Nextar oh yeah, sorry about that. i should have placed a comment instead. And global variables is indeed bad practise and should have been mentioned :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.