How to discover "app" in a twig template? - twig

I have a Twig template that starts by setting a variable:
{%
set currentPath = path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params'))
%}
app isn't one of the parameters passed into the template, so what is it? How does it get set/found by Twig?

If you're using symfony (or silex), the variable app will be injected as a global automatically to the template
reference

Related

How override a template?

Following the documentation to override catalog.html.twig, I see that the "Hello World!" display does not work.
I cleared the cache.
I also tried to override view.html.twig with the following logic:
file path :
/modules/foo/views/PrestaShop/Admin/Sell/Order/Cart/view.html.twig
the content :
{% extends 'PrestaShopBundle:Admin/Sell/Order/Cart:view.html.twig' %}
{% block cart_summary %}
Hello world!
{% endblock %}
But unfortunately nothing works.
Do you have any ideas ?
overriding the admin templates require installed module.
Simply create and install your module via 'module manager' in admin panel, then inside of the module create this path "views/PrestaShop/Admin/Sell/Order/Cart/view.html.twig".
It should work now.
You have to create a module, I think that I have to put the file inside the override folder, but no.
You have to create a module whit that tools of prestashop and the create the path in views like the other answer say.
The module can be generic and doesn't matter the hook.

How to set permission to twig template

I have created a twig template using which I am displaying the some content on the front end. I want this template to be accessible only for a particular role. could anyone please tell me how can I set the permission.
This should be done in controller where you render this view.
However you could wrap template in condition
{% if is_granted('ROLE_ADMIN') %}
{# template content #}
{% endif %}

Render Jinja Markup in Passed Variables

I am moving my blog over from Jekyll to Flask. In my Jekyll blog I use Liquid includes to process overhead heavy items like youtube videos and images and to ensure uniformity so that they can all be updated in the same place if there's a style or code change to the site.
In Flask is there a way to have the Jinja2 template process Liquid variables in the body of the blog article when it's passed to render_template()?
Example:
#route('/article')
def article():
blog_body = "<h1>My Video</h1>{% include '_media.html' type='youtube' id='a2343lj' %}"
return render_template('home.html.j2', content = blog_body)
And then in home.html.j2:
{% block page_content %}
{{ content }}
{% endblock %}
When testing, the template renders the literal string and does not process the include in the variable. Is there any way to get the Jinja template to render the Liquid syntax in the variable passed to it?
While there does not appear to be any way to render a template string using Jinja2 syntax, there is a workaround. At the time the variable is set, you can process it using render_template_string from Flask.
Example:
from flask import render_template, render_template_string
#route('/article')
def article():
blog_body = render_template_string("<h1>My Video</h1>{% include '_media.html' type='youtube' id='a2343lj' %}")
return render_template('home.html.j2', content = blog_body)
Then at least the variable will be rendered at the time it is set, even if not whenever the template is rendered.

Drupal 8 Twig User ID?

I'm new to Drupal & Twig and all I need is in my custom theme a twig expression to output the current user's ID. I can't find anything in the template comments, only if a user is logged in true / false.
Is there a simple way to get the ID of the current user? I'm not sure how to implement custom methods in a theme.
thanks!
Hello bobomoreno,
I would suggest you use the module Bamboo Twig.
The Bamboo Twig module provides some Twig extensions with some useful functions and filters aimed to improve the development experience.
You could then enable the sub-module Bamboo Twig - Loaders:
drush en bamboo_twig_loader -y
Finally, you will be able to use the Twig function bamboo_load_currentuser:
<!-- Get Current User -->
{% set user = bamboo_load_currentuser() %}
<div>{{ user.name.value }}</div>
<div>{{ user.uid.value }}</div>
You can find the complete official documentation there.
In your theme find file yourthemename.theme and add following code:
function yourthemename_preprocess(&$vars, $hook)
{
$vars['uid'] = \Drupal::currentUser()->id();
}
now if you edit your twig template for html, page, region, block, field, form element... you can use 'uid' token in your twig. It works for all hooks
If you only need the ID in user.html.twig, it's {{ user.id }}
Here's how D8 now works, in two lines of executable code:
<?php
// This code returns the current user ID.
$account = \Drupal::currentUser();
return $account->id();
The display name is not a field you can configure in {{ content }}. You can get it directly from the user entity:
{{ user.displayname }}
Reference for the php method: AccountInterface::getDisplayName
The Twig Tweak module is very small and yet very powerful. You can get the current user id with drupal_token('current-user:uid') I am using it to pass the current user id to a view like this:
{{ drupal_view('view_name', 'embed_1', drupal_token('current-user:uid')) }}

Patternlab twig includes with params

I am using the node version of patternlab with twig as template engine. I am using twig because my codebase is written in twig - so using mustache it not an option.
I simple try to include an pattern:
<button type="button" class="btn {{ buttonClass }}">{{ cta }}</button>
in an other pattern with:
{% include "base-button" with {'buttonClass': 'btn-primary btn-xs'} %}
This does not work. The pattern is included but the variable buttonClass is not set.
Any Ideas?
This seems to be an issue with the twig integration in this repo https://github.com/pattern-lab/patternengine-node-twig.
There are other issues with twig core features described here: https://github.com/pattern-lab/patternlab-node/issues/554
I finally decided to switch to the php/twig edition and the same twig-patterns are working as expected. If anybody have to use the node edition, i recommend to use mustache.
I believe there are plans to pass the Twig rendering in Pattern Lab Node to PHP, however, this isn't ready yet, but using The Twig engine for Patternlab/Node you can pass a variable via an include. It doesn't work exactly the same as normal twig but it does work.
{% set buttonClass = 'btn-primary btn-xs' %}
{% include "components-button" with buttonClass %}
You can also pass multiple variables:
{% include "components-button" with buttonClass anotherVariable %}

Resources