Answer the question
In order to leave comments, you need to log in
Symfony 4 how to write menu middleware?
Hello everyone, tell me how to solve the following problem, I have twig templates that look like this
├── base.html.twig
├── email
│ └── index.html.twig
├── footer.html.twig
├── header.html.twig
├── index
│ ├── index.html.twig
│ ├── modal_load_file.html.twig
│ └── modal_load_link.html.twig
└── output
└── index.html.twig
/**
* @Route("/", name="index", methods={"GET","HEAD"})
*/
public function index()
{
$menu = $this->getMenu();
// some code
return $this->render('index/index.html.twig', [
// some vars
'menu' => $menu
]);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="stylesheet" href="{{ asset('css/bootstrap.min.css') }}"/>
<link rel="stylesheet" href="{{ asset('css/datepicker.min.css') }}"/>
<link rel="stylesheet" href="{{ asset('css/main.css') }}"/>
<script src="{{ asset('js/jquery-3.4.1.min.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/popper.min.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/bootstrap.min.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/datepicker.min.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/validator.form.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/download.files.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/download.link.js') }}" type="text/javascript"></script>
<script src="{{ asset('js/main.js') }}" type="text/javascript"></script>
{% block stylesheets %}{% endblock %}
</head>
<body>
{{ include('header.html.twig') }}
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
{{ include('footer.html.twig') }}
<script>
</script>
</html>
└── output
└── index.html.twig
Answer the question
In order to leave comments, you need to log in
Twig Extension
Create your extension by passing your services to it (repository for example)
In the getFunctions() method, register your twig function
Register it with a tag twig.extension
Call it in your templates
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question