I feel like this is very stupid problem, but can't solve it.
Installed Twig via composer, version 1.24.2
Trying basic functionality, started from extending templates:
Initialization
$loader = new Twig_Loader_Filesystem('template');
$twig = new Twig_Environment($loader, array(
'cache' => 'c_cache',
'debug' => true,
'auto_reload' => true,
));
Rendering
echo $twig->render('layout.twig', array(
'data' => array(
'title' => 'Page title!',
) ));
layout.twig
...
<body>
{% block header %}{% endblock %}
</body>
...
_header.twig
{% extends 'layout.twig' %}
{% block header %}
Hello?
{% endblock %}
Update
I misunderstood concept of extend, I should use include in this situation.
Your render should be from _header.twig and not from layout.twig. If you render layout, the block header is empty.
Try to render from _header.twig and you will get the expected result.
Related
I have a logic that wouldn't be easy to implement in EasyAdmin so I decided that I implement it in Symfony 6 then integrate it into EA. The integration worked like a charm but I can't figure out which form_theme should I use to look like the other EA forms.
I have created a form type which doesn't belong to any entity since multiple entities will be generated after the validation based on the input data.
This is the controller
<?php
namespace App\Controller\Admin;
use App\Form\Type\NewTextType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class TextController extends AbstractController
{
#[Route('/admin/text/new', name: 'new_text')]
public function new(): Response
{
$defaultData = [];
$form = $this->createForm(NewTextType::class, $defaultData);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
// process the data and persist them as different entities
// redirect to the empty form and do it again
}
return $this->renderForm('admin/text/new.html.twig', [
'form' => $form,
]);
}
}
and the template
{% extends '#EasyAdmin/page/content.html.twig' %}
{% form_theme form 'foundation_5_layout.html.twig' %}
{% block content_title %}
<h1 class="title">Add new Text</h1>
{% endblock %}
{% block main %}
{{ form(form) }}
{% endblock %}
Unfortunately it looks like a crap.
It looks better if I replace foundation_5_layout with {% form_theme form 'bootstrap_5_layout.html.twig' %} but then the appearance setting is not applied even though it is presented in the BODY tag:
data-ea-dark-scheme-is-enabled="true"
What do I miss here?
I use Symfony 6.1.2 and EasyAdmin 4.3.2
Finally I have found the right template which supports the Light/Dark appearance:
{% form_theme form '#EasyAdmin/crud/form_theme.html.twig' %}
The whole template
{% extends '#EasyAdmin/page/content.html.twig' %}
{% form_theme form '#EasyAdmin/crud/form_theme.html.twig' %}
{% block content_title %}
<h1 class="title">Add new Text</h1>
{% endblock %}
{% block main %}
{{ form(form) }}
{% endblock %}
I have Shopware 6.5.3. I was trying to extend "sw-users-permissions-user-detail" like this :
import template from './sw-users-permissions-user-detail.html.twig';
Shopware.Component.override('sw-users-permissions-user-detail', {
template
});
And file 'sw-users-permissions-user-detail.html.twig'
{% block sw_settings_user_detail %}
{% parent %}
{% block test %}
<p>Blabla</p>
{% endblock %}
{% endblock %}
It's not working at all, and I don't know why.
Any help ?
NB : It's working when I'm overriding other templates :
Component.override('sw-dashboard-index', {
template
});
If you want to put the original contents to the block, that you are overriding, you should use the 'parent' statement like this:
{{ parent() }}
I am new to Timber and Twig andnow I encountered problem I cannot solve. I use ACF in page templates and everything works fine but they do not display on front page. {{dump.(post.meta)}} is null
my front-page.php:
<?php
$context = Timber::context();
$context['post'] = new Timber\PostQuery();
$templates = array( 'pages/front-page.twig');
Timber::render( $templates, $context );
my front-page.twig:
{% extends "base.twig" %}
{% block content %}
{{dump(post.meta('header'))}}
<h1>{{post.meta('header')}}</h1>
<div class="hero__image__container">
<img src={{ Image(post.meta('img')).src }}/>
</div>
{% include 'parts/banner.twig' %}
{% include 'parts/services.twig' with {'items': services.get_items} %}#}
{% endblock %}
What am I doing wrong?
When you want to get a single post in Timber, you would use Timber::get_post().
<?php
$context = Timber::context();
$context['post'] = Timber::get_post();
$templates = array( 'pages/front-page.twig' );
Timber::render( $templates, $context );
When you use Timber\PostQuery, you get a collection of posts in return, which behaves like an array. Before you could use {{ post.meta() }}, you’d first have to access the first post in that array.
5 days have gone by, still no success!
I dumped the results of my card variable and it returns results.
But when I try to pass it trough varibale on my twig tamplate it throws:
Variable "card" does not exist.
It's defined in SonataAdmin.
protected function configureShowFields(ShowMapper $showMapper)
{
$card = $this->getCardTransactions(); // on dump(), it works
$showMapper->tab('Cards')
->add('Data', 'string', array(
'template' => "#AdminTemplates/sonata/details.html.twig",
'card' => $card
))
->end()
->end();
}
and in my twig;
{% for c in card %}
{{ c.id }}
{% endfor %}
I think it has to do with SonataAdmin and how it handles this type of calls but I have read the documentation and searched online but still no luck.
You have to use the field_description.options object in your template to access your variable.
protected function configureShowFields(ShowMapper $showMapper)
{
$showMapper
->tab('Cards')
->add('Data', 'string', [
'template' => "#AdminTemplates/sonata/details.html.twig",
'card' => $this->getCardTransactions(),
])
->end();
}
{# #AdminTemplates/sonata/details.html.twig #}
{% extends '#SonataAdmin/CRUD/base_show_field.html.twig' %}
{%- block field -%}
{% spaceless %}
{% for card in field_description.options.cards %}
{{ card.id }}
{% else %}
<p>No card</p>
{% endfor %}
{% endspaceless %}
{%- endblock -%}
Im including a link.twig into block.twig, and block.twig into page.twig. in my set options is there a way I can change the link objects name to something like heroLink?
I need to set options within page.twig. link.twig is included into other templates so I dont want to change it (eg changing link.url to heroLink.url).
In my page:
{% set options = {
title: 'my title',
link: {
text: 'Search',
url: "www.google.com"
}
}
%}
{% include "block.twig" with options %}
In block.twig:
<div class="something">
<h2>{{ title }}</h2>
<div class="hero">
{% include "link.twig" with {'style': 'primary'} %}
</div>
</div>
In link.twig:
{{ link.text }}
The reason for this is that block.twig actually has other links. link.twig may be imported multiple times. As the mock object needs to be created in page.twig something like heroLink makes a lot more sense in this context.
In my page:
{% set options = {
action: {
text: 'Action',
url: "action.com"
}
}
%}
{% include "component.twig" with options %}
In component.twig:
{% import "link.twig" as mainLink %}
{{ mainLink.link(action.url, action.text) }}
In link.twig
{% macro link(url, text) %}
{{ text }}
{% endmacro %}