I am new to symfony and I am trying to make some tests.
My first test is to make a page including header and footer.
I have created controllers files in bundle:
DefaultController.php
FooterController.php
SecurityController.php
for the login tests.
I have also created three twig files in
"Resources/views/Default"
footer.html.twig
head.html.twig
index.html.twig
Now for first, I have tried to include in index.html.twig the file footer.html.twig, with the code:
{% extends 'AcmeSecurityBundle:Default:footer.html.twig' %}
Now, if there is only text or tags, all works better. But if i declare in the footer controller some vars and put this code in the twig file:
{{ year }}
{{ lastyear }}
{{ copyright }}
Symfony gets me the 500 Internal Error, with
"Variable "year" does not exist in AcmeSecurityBundle:Default:footer.html.twig at line 7" message.
How can Ipass FooterController.php variables in a twig inclusion?
Related
is there an easy way to always show the twig vars {{dump()}} in the symfony web developer toolbar? I already have implemented a custom data collector but if I add the following lines in my collector templates:
<div class="sf-toolbar-info-piece">
<b>Twig vars</b> {{ dump() }}
</div>
only the vars given to the toolbar will be shown not the ones I added in to the render function in the controller.
Basically I want something like the output that you see if you add
{% dump %}
in your twig template.
I've overrided the default layout of FOSUserBundle by placing a layout.html.twig in app\Resources\FOSUserBundle\views.
I also have overrided some other templates (like login, register and resetting password) by placing new files in the corresponding directory:
app\Resources\FOSUserBundle\views\layout.html.twig
app\Resources\FOSUserBundle\views\Registration\register.html.twig
app\Resources\FOSUserBundle\views\Resetting\request.html.twig
app\Resources\FOSUserBundle\views\Security\login.html.twig
I would like to render the register-form, the login-form and the reset-password-form on the same page. For that I'm using twigs render controller method:
<div class="login" data-lbg="teal">
{{ render(controller('FOSUserBundle:Security:login')) }} // foo
{{ render(controller('FOSUserBundle:Registration:register')) }} // bar
{{ render(controller('FOSUserBundle:Resetting:request')) }} // baz
</div>
However, this just outputs the word "foo" and doesn't extend my layout.html.twig. So it just gives me a blank page with "foo" on it.
Is there something broken or am I doing something wrong?
Best
Christian
I created an specific controller for rendering the three forms, which seems to be working. In the corresponding view script:
{% extends 'FOSUserBundle::layout.html.twig' %}
{% block body %}
{{ render(controller('FOSUserBundle:Security:login')) }}
{{ render(controller('FOSUserBundle:Registration:register')) }}
{{ render(controller('FOSUserBundle:Resetting:request')) }}
{% endblock %}
Unfortunately, if there are validation errors, the user will be redirected to /login, /register and /resetting/request, which templates are not extending the login layout. At the moment, the user get a white page with the form on it. :/
I need to load a page, that will be "inserted" in a template - as I read it, Volt's Template Inheritance should do the trick and it does... kinda. Hardcoded values, as shown in the examples, work fine - the following example works:
<!-- Template -->
<div id="site_content">
{% block test %}
{% endblock %}
</div>
and the page, that inherits the template:
{% extends "../../templates/de/index.volt" %}
{% block test %}
{{ content() }} {# this is a registered volt function that outputs the generated content #}
{% endblock %}
However, the same page might need to inherit a different template and that must be decided on runtime, so the name of the template must be generated dynamically. Two options occurred to me:
Set the template name to a variable and use it when extending - the problem here is that I don't see a way to use it afterwards. That guy seems to have had the same problem, but there is neither an answer of how to do it, nor a confirmation that it isn't possible at all.
Register another function to generate the complete string (e.g. {% extends "../../templates/de/index.volt" %}) and then compile it, e.g.
$compiler->addFunction('get_template',
function ($resolvedArgs, $exprArgs) use ($volt) {
return $volt->getCompiler()
->compileString('{% extends "../../templates/de/index.volt" %}');
});
and then use that function in the page, e.g.
{{ get_template() }}
{% block test %}
{{ content() }}
{% endblock %}
However, using that approach does not parse the page content (e.g. the content returned by the registered content() function is not shown). I'm also open to other solutions (using Twig instead of Volt is only a last resort, for performance issues), advices of what I'm doing wrong or pointers of useful articles on the topic. Thanks in advance!
Try using partials as documented in the Phalcon doc: Using Partials
On my eZ publish 5 site I have all my templates in Twig, in the vendor/ezsystems/demobundle/EzSystems/DemoBundle/Resources/views/ subfolders. They are all being used throughout my whole site, no problems there. With one exception: 404 pages. If I go to mysite/nonexistingurl, it gives me a kernel (20) / Error page, with status 404. The template being used for this is the 20.tpl somewhere in eZ publish/symfony, I don't want that, I want to use my own Twig template for this.
How can I achieve this? I added a vendor/ezsystems/demobundle/EzSystems/DemoBundle/Resources/views/Exception/error.html.twig page, but this one is not being called
first add this configuration parameter
parameters:
ezpublish_legacy.default.module_default_layout: 'YourBundle::pagelayout_legacy.html.twig'
you may add it in the parameters.yml file located in path/to/yourezpublishinstall/ezpublish/config, the parameters.yml is usually imported in the config.yml located in the same folder
this would define the twig template located in path/to/yourbundle/Resources/views/pagelayout_legacy.html.twig as the parent template for legacy stack modules templates
inside the pagelayout_legacy.html.twig template, you may use this code
{% extends 'YourBundle::pagelayout.html.twig' %}
{% block content %}
{# module_result variable is received from the legacy controller. #}
{% if module_result.errorCode is defined %}
<h1>{{ module_result.errorMessage }} ({{ module_result.errorCode }})</h1>
{% else %}
{{ module_result.content|raw }}
{% endif %}
{% endblock %}
note in the code, the template extends the pagelayout.html.twig template, that should here define a block named content, the pagelayout.html.twig may usually be the main base layout for your ez publish 5 website
you may modify the pagelayout_legacy.html.twig template to your needs
reference:
http://share.ez.no/forums/developer/overriding-legacy-error-pages-templates
I used to work with CodeIgniter. Now I am starting to learn Symfony2. I was just wondering, in CodeIgniter I could load a view from another view. Like, I could load menu.php from index.php. This is how I used to do it -
//in index.php
<?php $this->load->view('menu.php'); ?>
Is it possible to do the same thing in Symfony2 and Twig?
There are a few different ways you can do it depending on what you're trying to accomplish.
If you want to render the response of a controller you can do this in your twig template.
{{ render(controller('AcmeArticleBundle:Article:recentArticles', {
'max': 3
})) }}
In the above example, the parameter passed max would be passed as an argument to your controller. Then the controller would be responsible for returning a response that will be inserted into the view where it was called from.
You can also use include to render just the twig template as an embedded view:
{% for article in articles %}
{{ include(
'AcmeArticleBundle:Article:articleDetails.html.twig',
{ 'article': article }
) }}
{% endfor %}
In the above example article would be passed into the context of the twig template articleDetails.html.twig but not to any controller. So this method is ideal for repetitious front-end code that is used in many places such as templates for tables, lists, sidebars, etc.
http://symfony.com/doc/current/book/templating.html#including-other-templates
http://twig.sensiolabs.org/doc/functions/include.html