Bonjour,
Sur une même vue(job/index.html.twig), je veux faire appel à 2 controllers. Est ce possible ?
Voici mon fichier job/index.html.twig et à l'interieur j'ai mis une include : {{ include('includes/features.html.twig') }}
{% extends 'base.html.twig' %}
{% block title %}Accueil{% endblock %}
{% block body %}
{{ include('includes/part1home.html.twig') }}
{{ include('includes/features.html.twig') }}
<section class="jobs">
<div class="container">
<div class="row heading">
<h2>Emplois récents</h2>
<p>Trouvez l'emploi que vous méritez</p>
</div>
<div class="companies">
{% for job in jobs %}
<div class="company-list">
<div class="row">
<div class="col-md-2 col-sm-2">
<div class="company-logo">
<img src="{{ asset('uploads/images/' ~ job.company.logo) }}" class="img-responsive" alt="" />
</div>
</div>
<div class="col-md-10 col-sm-10">
<div class="company-content">
<h3><a href="{{ path('job_show', {slug: job.slug}) }}">
{{ job.title }}
</a>
<span class="full-time">{{ job.contrat }}</span>
{# <span class="part-time">Temps partiel</span>
<span class="freelance">Freelance</span>
<span class="internship">Stage</span> #}
</h3>
<p>
<span class="date_pub"><i class="fa fa-calendar"></i> {{ job.publishedAt ? job.publishedAt|date('Y-m-d H:i:s') : '' }}</span>
<span class="company-name"><i class="fa fa-briefcase"></i>{{ job.company.name }}</span>
<span class="company-location"><i class="fa fa-map-marker"></i> {{ job.adresse }}</span>
<span class="package"><i class="fa fa-money"></i>{{ job.salary }}€</span>
</p>
</div>
</div>
</div>
</div>
{% else %}
<div class="company-list">
<div class="row">
<p>Pas d'emplois trouvés </p>
</div>
</div>
{% endfor %}
</div>
<div class="row">
<input type="button" class="btn brows-btn" value="Brows All Jobs" />
</div>
</div>
</section>
{{ include('includes/testimonials.html.twig') }}
{{ include('includes/members_cards.html.twig') }}
{{ include('includes/newsletter.html.twig') }}
{% endblock %}
Et voici mon fichier includes/features.html.twig :
<section class="features">
<div class="container">
{% for feature in features %}
<div class="col-md-4 col-sm-4">
<div class="features-content">
<span class="box1"><span aria-hidden="true" class="icon-dial"></span></span>
<h3>{{ feature.titre1 }}</h3>
<p>{{ feature.description1 }}</p>
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="features-content">
<span class="box1"><span aria-hidden="true" class="icon-search"></span></span>
<h3>{{ feature.titre2 }}</h3>
<p>{{ feature.description2 }}</p>
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="features-content">
<span class="box1"><span aria-hidden="true" class="icon-printer"></span></span>
<h3>{{ feature.titre3 }}</h3>
<p>{{ feature.description3 }}</p>
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="features-content">
<span class="box1"><span aria-hidden="true" class="icon-dial"></span></span>
<h3>{{ feature.titre4 }}</h3>
<p>{{ feature.description4 }}</p>
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="features-content">
<span class="box1"><span aria-hidden="true" class="icon-search"></span></span>
<h3>{{ feature.titre5 }}</h3>
<p>{{ feature.description5 }}</p>
</div>
</div>
<div class="col-md-4 col-sm-4">
<div class="features-content">
<span class="box1"><span aria-hidden="true" class="icon-printer"></span></span>
<h3>{{ feature.titre6 }}</h3>
<p>{{ feature.description6 }}</p>
</div>
</div>
{% endfor %}
</div>
</section>
Dans mon controller :
<?php
namespace App\Controller;
use App\Entity\Feature;
use App\Form\FeatureType;
use App\Repository\FeatureRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/feature")
*/
class FeatureController extends AbstractController
{
/**
* @Route("/", name="feature_index", methods={"GET"})
*/
public function index(FeatureRepository $featureRepository): Response
{
return $this->render('includes/features.html.twig', [
'features' => $featureRepository->findAll(),
]);
}
}
Et j'ai cette erreur dans templates/includes/features.html.twig (line 5) ==>{% for feature in features %}
L'erreur est: Variable "features" does not exist.
salut,
pour rendre un controller dans une vue Twig tu as 2 solutions:
1- Tu fais un rendu de ton controller directement dans ta vue:
<div id="sidebar">
{{ render(controller(
'App\Controller\ArticleController::feature_index'
)) }}
</div>
Cependant, cela ets à éviter car cela augmente le temps de génération de la vue et du coup l'affichage
2- Tu fais un rendu de ton controller avec plusieurs variables (Best practice):
dans ton controller "features"
tu renvois un tableau de valeur avec "job" et bien il suffit de déplacer ta logique:
au lieu de renvoyer ceci dans ton premier controller
return $this->render('includes/index.html.twig', [
'job' => $job
]);
il faut plutot faire ceci:
return $this->render('includes/index.html.twig', [
'job' => $job,
'features' => $featureRepository->findAll(),
]);
comme ca, dans ta vue index tu auras acces à 2 variables : "job" et "features", du coup l'inculde de ta seconde vue pour avoir accès a la variable "features" et s'afficher.
J'ai toujours la même erreur (Variable "features" does not exist.)
, j'ai fait ceci:
<?php
namespace App\Controller;
use App\Entity\Feature;
use App\Form\FeatureType;
use App\Repository\FeatureRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/feature")
*/
class FeatureController extends AbstractController
{
/**
* @Route("/", name="feature_index", methods={"GET"})
*/
public function index(FeatureRepository $featureRepository): Response
{
return $this->render('includes/features.html.twig', [
'jobs' => $job,
'features' => $featureRepository->findAll(),
]);
}
}
Ok avec {{ dump() }} j'ai ceci:
array:2 [▼
"jobs" => array:2 [▼
0 => Job^ {#799 ▼
-id: 1
-title: "Développeur Symfony 4"
-slug: "bbbbbbbbbbbbbbb"
-summary: "ccccccccccccccccc"
-content: "tttttttttttttttttttttttttt"
-publishedAt: DateTime @1388530800 {#797 ▶}
-salary: 2000
-adresse: "Paris"
-contrat: "CDI"
-company: Company^ {#841 ▶}
-user: null
}
1 => Job^ {#839 ▼
-id: 2
-title: "Chef de projet digital"
-slug: "bbbbbbbbbbbbbbb"
-summary: "ccccccccccccccccc"
-content: "tttttttttttttttttttttttttt"
-publishedAt: DateTime @1388530800 {#840 ▶}
-salary: 3000
-adresse: "Lyon"
-contrat: "Temps partiel"
-company: Company^ {#838 ▶}
-user: null
}
]
"app" => AppVariable^ {#671 ▼
-tokenStorage: TokenStorage^ {#45 ▶}
-requestStack: RequestStack^ {#143 ▶}
-environment: "dev"
-debug: true
}
]
alors il y a un problème avec ta requête features ou ton passage de variable car tu devrais avoir dans le débug la variable features aussi