Twig

Mayd provides Twig integrations for the frontend, as well as the backend of Mayd applications.

Functions

A list of all Twig functions, provided by Mayd. Please refer to their documentation about details of their usage.

file_url()

Provided by mayd/files, mainly usable in frontend.

Generates a download URL to a file.

{{ file_url(file, true) }}
parameters
nametypedefaultdescription
fileFileEntry|FileData|int|stringThe id / entity / VO of the file to generate the link to.
embedboolfalseWhether the download should try to embed the file or always download it.
return value
typedescription
string|nullThe URL, or null if the file was not found.

Embedding a download depends on browser support. Nearly all browser support embedding a PDF for example, that means the file is not downloaded, but instead opened inside the browser tab.

homepage_url()

Provided by mayd/pages, usable in frontend & backend.

{{ homepage_url(website) }}
parameters
nametypedescription
websiteWebsiteThe website to generate the homepage url to.
return value
typedescription
stringThe absolute URL to the homepage.

image_tag()

Provided by mayd/files, mainly usable in frontend.

Similar to image_url(), but directly renders an <img> tag. Can be passed additional parameters.

{{ image_url(image, "content-slider") }}
parameters
nametypedefaultdescription
imageFileEntry|FileData|int|stringThe image to get the URL for.
sizestringThe image size to fetch.
attributesarray[]Additional HTML attributes to attach to the file.
languageLanguagenullThe language to automatically add the alt texts for. If null, the language from MaydView is used.
return value
typedescription
string|nullThe HTML for the image tag, or null if the file was not found.

This function automatically marks all images as loading="lazy" for performance reasons.

In the future, this function will directly embed the width / height of the image, to improve loading.

image_url()

Provided by mayd/files, mainly usable in frontend.

Generates a URL to an image, that can be used inside an <img> tag or as background image URL.

{{ image_url(image, "content-slider") }}
parameters
nametypedescription
imageFileEntry|FileData|int|stringThe image to get the URL for.
sizestringThe image size to fetch.
return value
typedescription
string|nullThe URL, or null if the file was not found.

Instead of <img src="{{ image_url() }}"> you should always use image_tag().

mayd_crud()

Provided by mayd/crud, should only be used in the backend.

Embeds a CRUD editor in the backend.

{{ mayd_crud("some.key", "locations.crud.headline", )
parameters
nametypedefaultdescription
keystringThe CRUD manager key
headlinestringThe headline of the CRUD manager.
contextOrDataarray|CrudContext[]The context of the crud manager
optionsarray[]Additional options
options.emptyMessagestring"..."The label to show if the result is empty.
options.addLabelstring"..."The label to show for "add a new entry".
options.headlineTranslationParamsstring[]Additional translation params to translate the headline.
options.headlineIsPageTitlebooltrueWhether the CRUD headline acts as page title.
options.searchPlaceholderstring"..."The input placeholder for the search field
return value
typedescription
stringThe HTML embed code, that will automatically be picked up by the JSX CRUD component.

Some comments about the options:

  • If you have a page title before the CRUD manager, you should set options.headlineIsPageTitle to false.
  • Every label / message in the options, as well as the headline will automatically be translated in the backend domain.

mayd_feature()

Provided by mayd/core, usable in frontend & backend.

Returns whether a Mayd feature flag is enabled.

{% if mayd_feature("some-feature") %}
parameters
nametypedescription
featurestringThe key of the feature.
return value
typedescription
boolWhether the feature is enabled.

All unknown features will always be disabled.

This checks for Mayd feature flags, not project feature flags. Use project_feature() for that.

mayd_form()

Provided by mayd/forms, mainly usable in frontend.

Renders a Mayd form in the frontend.

{{ mayd_form(context, frontendForm) }}
parameters
nametypedefaultdescription
contextWebsiteRequestContextInterfaceThe website request context.
formFrontendFormThe frontend form to render
additionalDataarray[]Additional data to add to the form (see the form docs for details about that parameter).
renderOptionsarray[]Options for manipulating the rendering.
renderOptions.formThemestring"..."The form theme to use when rendering the form.
return value
typedescription
stringThe rendered HTML form.

If you need a custom form theme, you should always extend the base @MaydForms/frontend/theme.html.twig form theme.

mayd_form_themes()

Provided by mayd/core, should only be used in the backend.

Returns the list of all registered form themes. This includes the form themes by Mayd bundles, as well as the project’s own form theme, set in the Kernel.

{%- form_theme form with mayd_form_themes() -%}
return value
typedescription
string[]The list of all registered form themes.

You must set this flag in every backend form, to have proper form rendering.

mayd_form_value()

Provided by mayd/forms, should only be used in the backend.

The Mayd forms component normalizes input values before storing them in the database. This function renders such normalized values.

{{ mayd_form_value(value) }}
parameters
nametypedescription
valuemixedThe value to render.
return value
typedescription
stringThe rendered value (as HTML).

mayd_logout_url()

Provided by mayd/core, usable in frontend & backend.

Generates the URL to the logout. You can pass an optional relog key, to directly redirect to the login form of the frontend authenticator with the same key.

{{ mayd_logout_url("mayd") }}
parameters
nametypedefaultdescription
relogstring|nullnull
return value
typedescription
stringThe logout URL.

mayd_menu()

Provided by mayd/pages, mainly usable in frontend.

Renders a menu, at the given location, in the given website.

parameters
nametypedefaultdescription
contextRequestContextInterface|nullThe request context to render in.
locationstring|nullnullThe menu location to render, or null if you want to render the page tree menu.
renderOptionsarray[]Additional render options.
return value
typedescription
stringThe rendered menu (as HTML).

If the request context is either null or doesn’t have a website, nothing will be rendered. The renderOptions are the options accepted by the MenuBundle.

mayd_menu_tree()

Provided by mayd/pages, mainly usable in frontend.

Returns the menu tree, to manipulate or render manually.

{% set menu = mayd_menu_tree() %}
{# do sth with the menu here #}
parameters
nametypedefaultdescription
contextRequestContextInterface|nullThe request context to render in.
locationstring|nullnullThe menu location to render, or null if you want to render the page tree menu.
renderOptionsarray[]Additional render options.
return value
typedescription
MenuItem|nullThe resolved menu tree, null if the request context was empty.

If the request context is either null or doesn’t have a website, null will be returned.

You still have to pass render options, as these are also used for resolving the menu tree. The renderOptions are the options accepted by the MenuBundle.

mayd_slot_admin()

Provided by mayd/pages, should only be used in the backend.

Renders the data container to embed a slot admin.

{{ mayd_slot_admin(slot) }}
parameters
nametypedescription
slotSlot|nullThe slot to embed the slot admin for.
return value
typedescription
stringThe HTML embed code, that will automatically be picked up by the slot admin JS component.

mayd_support_enabled()

Provided by mayd/core, usable in frontend & backend.

Returns whether the user has support mode enabled.

{% if mayd_support_enabled() %}
return value
typedescription
bool

mayd_toasts_init()

Provided by mayd/core, usable in frontend & backend.

Initializes the (Symfony-based) toasts for the frontend.

{{ mayd_toasts_init("_app-toasts-data") }}
parameters
nametypedescription
dataContainerClassstringThe HTML class for the data container.
return value
typedescription
stringThe rendered data container, that will be picked up by the JS component (as HTML).

page_url()

Provided by mayd/pages, usable in frontend & backend.

Returns the absolute path to the given page.

{{ page_url(page, "sub.route", {language: "de"}) }}
parameters
nametypedefaultdescription
pagePageTreeNode|TreeNodeData|nullThe page to generate the link to.
routestring"index"The sub route to generate to.
routeParametersarray[]The route parameters for the sub route.
return value
typedescription
string|nullThe absolute path URL to the given page + sub route.

The page router automatically performs context switches: so it recognizes if the domain is different and will automatically generate an absolute URL, if needed.

page_url_absolute()

Provided by mayd/pages, mainly usable in frontend.

Returns the absolute URL to the given page.

{{ page_url_absolute(page, "sub.route", {language: "de"}) }}
parameters
nametypedefaultdescription
pagePageTreeNode|TreeNodeData|nullThe page to generate the link to.
routestring"index"The sub route to generate to.
routeParametersarray[]The route parameters for the sub route.
return value
typedescription
string|nullThe absolute URL to the given page + sub route.

project_feature()

Provided by mayd/core, usable in frontend & backend.

Returns whether a project feature flag is enabled.

{% if project_feature("some-feature") %}
parameters
nametypedescription
featurestringThe key of the feature.
return value
typedescription
boolWhether the feature is enabled.

All unknown features will always be disabled.

This checks for project feature flags, not Mayd feature flags. Use mayd_feature() for that.

render_slot()

Provided by mayd/pages, mainly usable in frontend.

Renders the slot contents for the frontend.

{{ render_slot(slot, requestContext) }}
parameters
nametypedescription
slotSlot|nullThe slot to render.
contextRequestContextInterfaceThe request context, to render the slot in.
return value
typedescription
stringThe rendered slot contents (as HTML).

rich_text()

Provided by mayd/rich-text, mainly usable in frontend.

Renders a rich text for the frontend. Uses the central rich text renderer, to apply some transformations to the HTML.

{{ rich_text(text) }}
parameters
nametypedescription
textstring|nullThe text to render.
return value
typedescription
string|nullThe rendered text (as HTML).

If you want to render a plain text and just have proper line breaks, you can use Twig’s native |nl2br filter on any variable for that.

Important Blocks

This section lists important blocks, that you can override in your template.

Backend

Block Name

Usage

Description

body

Global body.

(internal, should not be used)

breadcrumb

The breadcrumb entries.

Should directly render to <li> elements and must call parent().

content

The main content.

(empty)

javascripts

The assets in the footer.

Must call parent()

site_content_class

Additional (modifier) classes for .site-content

(empty) should just return plain string.

stylesheets

The assets in the head.

Must call parent()

title

The meta title

You should never override this block directly, but instead use the metaTitle template variable.

Frontend

Most of the frontend blocks come from your own layout.html.twig template, but there are some blocks in the base layout.

Your frontend layout.html.twig should always

{% extends "@MaydCore/frontend.html.twig" %}

The base frontend blocks are:

Block Name

Usage

Description

assets_footer

The assets in the footer.

Must call parent()

assets_head

The assets in the head.

Must call parent()

body

The main content.

(empty)

meta

The meta tags

(empty)

title

The meta title

(empty)

tracking

Any tracking scripts.

Must call parent()

Globals

Variable Name

Type

Description

mayd

MaydView

The global mayd view.

Global Template Variables

These are variables that you should (or could) pass to your templates. These variables differ between frontend and backend, so they are listed separately.

Backend

Variable Name

Required?

Type

Description

currentLocale

no

Locale

Used for generating

viewFilters

no

ViewFiltersSelection

If this variable is available, the corresponding view filter selectors will be rendered.

metaTitle

no

MetaTitle

The meta title instance to display. Normally generated via BackendController::metaTitle().

Frontend

Variable Name

Required?

Type

Description

context

no

RequestContextInterface

The request context. Technically not required, but should

Request Context

One very important variable is the request context. It (together with possibly other things) is most easily passed to the template by just using the mergeParameters() function from it in your action:

use Mayd\Foundation\Request\Context\PageRequestContextInterface;

public function someAction (PageRequestContextInterface $context) : Response
{
    // ...

    return $this->render("...", $context->mergeParameters([
        // ... more custom parameters
    ]);
}