I am new with Shopware 6 and its template engine Twig, but I need relative URL from current URL in theme. The problem is that almost all data in database are binary, so if I use:
{{ app.request.pathInfo }}
or
{{ app.request.requesturi }}
I always get such output:
/detail/eccbc87e4b5ce2fe28308fd9f2a7baf3
but the current URL in browser is a search friendly URL.
With:
{{ seoUrl('frontend.detail.page', { productId: page.product.id }) }}
I get the absolute URL in a search friendly format, but every try to replace host and scheme fails and I get an error. I can only replace /, detail or eccbc87e4b5ce2fe28308fd9f2a7baf3, but not host and not scheme.
Does anybody know what to do to get relative URL?
Solution: {{ app.request.attributes.get('sw-original-request-uri') }}
Related
I'm building a site with Wordpress + Timber and need to output an uploaded SVG image as code.
Here's what I've got, but it's not working:
{% for block in section.blocks %}
<img src="{{ Image(block.icon).src }}"> <!-- this works as exptected -->
{{ function('file_get_contents', Image(block.icon).src) }} <!-- this fails -->
{% endfor %}
The error generated is "failed to open stream", but the URL that Image(block.icon).src provides is a full, valid URL (ie I can go to it in my browser and it loads).
What am I missing? Or is there a better approach?
{{ function('file_get_contents', Image(map.flag).src) }} works for me. Should work for you if you are using advance custom fields in WordPress.
There is two way to load external code in twig file.
Include method :
{% include 'my.svg.twig' %}
Twig source function :
{{ source(my.svg.svg) }}
Also, you can try WP_PLUGIN_DIR constant with file_get_contents + your file url.
{{ function('file_get_contents', 'WP_PLUGIN_DIR'.Image(block.icon).src) }}
PS. Source function works like include.
More info about twig source function is here : https://twig.symfony.com/doc/2.x/functions/source.html
I can use Timber pagination in archive-law.php for the archive page (/law/page/2) of my custom post type, "law", but with the same codes in single-law.php, the second page (/law/cpt-post-slug/page/2) shows the 404 error. How can I fix that?
That’s how it’s supposed to work. Pagination only works for archive templates like archive.php or home.php, where you have a query with multiple results that can be paginated. It will not work for singular templates like single.php.
However, you could use next_post_link() and previous_post_link(), which display a link to the next or previous post which exists in chronological order from the current post.
In Timber, these functions are available as post.next and post.prev. Here’s an example for how you could use it in in a singular template in Twig:
{% if post.next %}
<h3>Next Article</h3>
{{ post.next.title }}
{% endif %}
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')) }}
I have issues to understand the Apostrophe CMS. I follow the tutorial in [Creating your first project][1] and stop just before the point where things become interesting.
I can open my basic/default website with localhost:3000 and see the content of the home.html file, while localhost:3000/home throws an error. Here is my home.html file:
my-project\lib\modules\apostrophe-pages\views\pages\home.html
{#
This is an example home page template. It inherits and extends a layout template
that lives in lib/modules/apostrophe-templates/views/outerLayout.html
#}
{% extends data.outerLayout %}
{% block title %}{{ super() }} | Home{% endblock %}
{% block main %}
<div class="main-content">
<h3>Hello world!
{% if not data.user %}
<a class="login-link" href="/login">Login</a>
{% endif %}
</h3>
<p>This is a very barebones Apostrophe project.
Now, get to work and make a real website!</p>
</div>
{% endblock %}
Where is the setting that apostrophe knows that home.html is the first (start) page which needs to be rendered?
And vice versa, I have seen a page when I call localhost:3000, apostrophe redirects directly to localhost:3000/home. Where is the setting that I tell apostrophe move automatically from localhost:3000 to localhost:3000/home?
I'm the lead developer of Apostrophe at P'unk Avenue.
Generally speaking, on a web site the "home page" is so named because it appears at "/" (at the root), not because it actually has the URL "/home". In Apostrophe the home page is always at "/" (*). Generally speaking this looks more professional than giving the home page a longer URL.
But if you really need "/home" to work for some reason, for instance to support legacy URLs and keep links from breaking, you can use the apostrophe-redirects module to add a redirect from /home and any other legacy URLs.
You could also make a subpage of the home page and set its slug to /home, but that would be a strange solution because it wouldn't be... well, it wouldn't be the home page. (The slug of the home page itself cannot be edited in Apostrophe.)
A ticket could be opened to add the ability for the home page to not have the slug "/", and for "/" to redirect to whatever the slug of the home page has been set to, but a good case would have to be made for why the feature is necessary.
(*) The only exception would be when using the apostrophe-workflow module, in which case you might have separate home pages for different languages, but you still wouldn't have "/home" in this scenario.
In the Yii 2.0 guide it says you can register Javascript code in a twig template like this:
{registerJs key='show' position='POS_LOAD'}
$("span.show").replaceWith('<div class="show">');
{/registerJs}
Tried this but it will simply output the whole snippet {registerJs ... as text on the page without adding it to the page's Javascript Code.
Similar commands like registerJsFile or registerCss aren't working either.
Am I missing something? Thanks!
EDIT:
As Mihai P. noted below, {registerJs} is syntax for Smarty templates. So, the question is: is there a similar way to register inline JS in Twig templates? The documentation only mentions registering assets.
You can use this in your Twig template, which is the current view, to call registerJs.
{% set script %}
var app = new Vue({
el: '#vue-widget',
data: {
message: 'Hello Vue!'
}
})
{% endset %}
{{ this.registerJs(script,4,'vue-widget') }}
The question is 3 years old, I put it here for someone runs into a similar problem.
You can use this in the Twig template with View constant like below:
{{ this.registerJs('$("span.show").replaceWith(\'<div class="show">\');', constant('\\yii\\web\\View::POS_HEAD')) }}
And, you can also call registerJs inside the Yii2 controller, which I think it makes the code cleaner
$this->getView()->registerJs('$("span.show").replaceWith(\'<div class="show">\');', \yii\web\View::POS_HEAD);
Your example is copied from the smarty section not the twig. There are some examples in the twig section that you might want to try.