Answer the question
In order to leave comments, you need to log in
How to display the output of a class method in Twig?
There are 2 databases: salon: id
name
city_id
city: id, name
{% for salonTables in salonTable %}
{ salonTables.name|escape }}
{{ salonTables.cityId|escape }}
{% endfor %}
{% for salonTables in salonTable %}
{ salonTables.name|escape }}
{{ \AppBundle\Entity\City::getNameFromId(salonTables.cityId) }}
{% endfor %}
Answer the question
In order to leave comments, you need to log in
As far as I know, you can't use static class methods in Twig.
I can offer 2 options:
1) Create your own extension for twig, and use it on a variable as a filter
2) Pass an instance of the City class to the template and use the method already from it.{{ city.nameFromId(salonTables.cityId) }}
You can run a static class method by calling it through an object of this class:
class Test {
private $x = 10;
public static function getSTatic() {
return 'static';
}
public function getNormal() {
return 'normal ' . $this->x;
}
}
{{ test.normal }}
{{ test.static }}
{{ city.nameFromId(salonTables.cityId)) }}
where city is an object of class City. class SalonController extends Controller
{
/**
* @Route("/salon/{id}")
* @Template
*/
public function salonAction($id)
{
$salons = $this->getDoctrine()->getRepository('AppBundle:City')->findBy(array('cityId' => $id));
return array('salons' => $salons);
}
}
{% for salon in salons %}
{ salon.name }}
{{ salon.cityId }}
{% endfor %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question