multisite eZ Platform installation - php

I'm building a multisite eZ Platforme installation and I need to specify a main layout for my templates.
Right now I have a template article.html.twig :
{% extends "main_layout.html.twig" %}
{% block content %}
<h1>{{ ez_render_field(content, 'body') }}</h1>
{% endblock %}
what I want to do is something like this :
{% if(siteaccess = "site1"){
extends "site1_main_layout.html.twig"
}
else if(siteaccess = "site2"){
extends "site1_main_layout.html.twig"
}
%}
Please help me!

You can just configure the layout in the config:
ezpublish:
system:
site1:
pagelayout: "tpl1.html.twig"
site2:
pagelayout: "tpl2.html.twig"
After that, you can just use the following in your full view:
{% extends pagelayout %}
{% block content %}
...
{% endblock %}
pagelayout is a variable prepopulated by eZ Platform from the above config based on current siteaccess. It requires eZ Platform 1.2 at the least, I believe.
It should also be noted that pagelayout variable is available only in full view templates. Other templates wishing to use the configured pagelayout must use the following:
{% extends ezpublish.configResolver.parameter('pagelayout') %}

Correct me if I misunderstood your goal, but do you recon it could be sorted by checking the domains? (I am assuming they would be different so could serve as a separator):
{% if app.request.baseUrl == 'site1' %}
...
{% else %}
...
{% endif %}
If I am not wrong, I also belive you can create a default Twig Controller Loader to decide this beforehand instead of leaving the logic to your views :)

Have a look here https://doc.ezplatform.com/en/latest/guide/design_engine/
you can use the Design engine where you can set up different themes with fallback.
Define one base theme an add per siteaccess one extra theme where you can override any template.

Related

Difference between customizing form theme and referencing block in Symfony?

what is the difference between referencing a widget block and customizing it : the docs say :
So far, to override a particular form block, the best method is to
copy the default block from form_div_layout.html.twig, paste it into a
different template, and then customize it. In many cases, you can
avoid doing this by referencing the base block when customizing it
But to me it looks the same :
{# app/Resources/views/Form/fields.html.twig #}
{% extends 'form_div_layout.html.twig' %}
{% block integer_widget %}
<div class="integer_widget">
{{ parent() }}
</div>
{% endblock %}
{# app/Resources/views/form/fields.html.twig #}
{% block integer_widget %}
<div class="integer_widget">
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
</div>
{% endblock %}
What is the difference?
The first example in the documentation that you are referencing shows how to override the entire widget that displays your form element.
The second example in the documentation that you are referencing shows how you can employ code re-use so that you are not rewriting form templating sections that you are not modifying. So, instead of having to declare
{% set type = type|default('number') %}
{{ block('form_widget_simple') }}
all over again in your overriding widget, you can instead reference the base block that already has this. If you are referencing base blocks from an external template, you can call the parent block via {{ parent() }}, and if you are referencing blocks from inside the same template as the form, you can call the base block via {{ block('base_integer_widget') }}
If you look at it from a PHP/Symfony point of view with inheritance that can help explain it as well. Say you have one PHP class that extends another and you want to override a function named doSomething() - you might rewrite the entire function as you need it. But, say that doSomething() has a block of common code that you always want to run, then you might perform your actions and call parent::doSomething() at the end of it. Or, if you're accessing a different Symfony service you might call $this->get('some.service')->doSomething() instead.
That's the same concept here, you can either override the entire widget or you can override parts of it - perhaps putting a surrounding <div></div> but calling {{ parent() }} from within that since you're changing nothing else about the widget.
I do have one example where I overrode standard button behavior in Symfony and used both cases. I have a separate template file in `app/Resources/views/Form/navigationButton.html.twig' with the following code:
{% use 'form_div_layout.html.twig' %}
{% block button_widget -%}
{% set attr = attr|merge({class: (attr.class|default(''))|trim}) %}
{{- parent() -}}
{%- endblock %}
{% block button_row -%}
{{- form_widget(form) -}}
{%- endblock button_row %}
I am overriding the button widget by allowing additional classes to be passed as attributes and then calling the parent widget to produce it as normal. I then override the button row widget to not put surrounding <div></div> tags since I didn't want that in my template (see the originals here and here).
Then to use in one of my templates I simply do:
{% form_theme form ':Form:navigationButton.html.twig' %}

Inherit dynamic template in Phalcon Volt

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

Import content between Twig files

How can I have a Twig file which imports some of it's content from a second Twig file in the same or a sub directory?
I am developing a project where multiple Twig files have content in common and I'm trying to avoid copying and pasting content between Twig files.
So, I want to have a subdirectory containing the shared markup and simply "import" into the relevant sections of the main Twig files.
With import.list.html.twig in the same directory as the main Twig file, I tried the following:
{% extends "::base.html.twig" %}
{% block title %}StockBundle:StockController:index{% endblock %}
{% block body %}
<h1>Welcome to the StockController:List page</h1>
{% for stock in stocks %}
<div class='list'>
<div class='each stock'>
<span class='name'>{{ stock.name }}</span>
<span class='desc'>{{ stock.description }}</span>
<span class='balc'>{{ stock.balance }}</span>
<span class='edit'>
edit
</span>
</div>
</div>
{% endfor %}
{% include 'import.list.html.twig' %}
{% endblock %}
... but I got the following error:
Unable to find template "import.list.html.twig" in ::base.html.twig at line 10.
When you include it needs to know the namespace of where it is located. When you do ::, as in {% extends "::base.html.twig" %}, it comes from the app/Resources/views directory of your application.
See: Template Naming and Locations Symfony documentation
If your import.list.html.twig is in a bundle, you'll have to define that properly. For instance, if you have StockBundle and a Resources/views directory in that bundle with its own base.html.twig template, you would have
{% include 'StockBundle::base.html.twig' %}
If you had, say, a Stock folder inside that bundle (attached to your StockController) and an import.list.html.twig template, you would have
{% include 'StockBundle:Stock:import.list.html.twig' %}
Note via Registered Namespaced Twig Paths that you can also use a namespaced path instead. They're actually faster as well. So the above would instead be
{% include '#Stock/base.html.twig' %}
{% include '#Stock/Stock/import.list.html.twig' %}
Here's another good reference for Symfony template best practices
NOTE
Since Symfony 2.2, you can use the include() function, and this is the preferred way to include templates:
{{ include('#Stock/base.html.twig') }}
{{ include('#Stock/Stock/import.list.html.twig') }}
Try this:
{% include 'StockBundle:Stock:import.list.html.twig' %}
instead of:
{% include 'import.list.html.twig' %}
http://symfony.com/doc/current/book/templating.html#including-templates

ezpublish/symfony renders 404 in Smarty, everything else in Twig

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

FOSUserBundle , dynamically change twig to render

In order to override a template for FOSUserBundle, I should create a twig file with the same name conserving the hierarchy. That's fine and it works perfectly.
My problem is that the twig file is not static ( in the desktop version I will render a twig file and in the mobile version we will render another one). I decide which twig to render on the controller by testing on a session variable.
Is there a solution to dynamically change the twig to render in FOSUserBundle without overriding all controllers?
You can test your session variable in twig also. Simply include another template in your twig :
{% if app.session.isMobile %}
{% include '::mobile.html.twig' %}
{% else %}
{% include '::desktop.html.twig' %}
{% endif %}

Categories