Drupal 8 Twig Template: get relative or absolute path from URI - templates

I have in a twig template an image url, like "public://path/to/image.png". I want to get "sites/default/files/path/to/image.png" to use it in the template.
I thought it should work with something like this:
{{ url ('public://path/to/image.png') }}
or
{{ file_uri_scheme ('public://path/to/image.png') }}
But all I get are errors. I know that the second one is a PHP function, but is there any chance to do it with TWIG?

You can use the twig function file_url to get a relative path:
{{ file_url('public://path/to/image.png') }}
Checkout the documentation under https://www.drupal.org/node/2486991
It accepts a uri and creates a relative path to the file. At least in Drupal 8.1.

Retrieving a URL to a page from a "path" is deprecated in D8. https://www.drupal.org/node/2073811
EDIT:
nah im wrong and just missunderstood the spelling ;/
the correct answer is: they are working on a twig extension.
i was searching for a way to get my image uri / urls and achieved it with
{{ node.field_image[delta].entity.url }}

Related

How do i get site url dynamically in admin twig file in opencart?

I am trying to get my site url like; https://www.example.com/ in admin twig file for my custom module.
Can anyone help me please?
I tried this, but no result;
{{ HTTPS_CATALOG }}
In your corresponding controller file define:
$data['site_url'] = $_SERVER['HTTP_HOST'];
in corresponding twig file you can call this data:
{{ site_url }}
If the answer are useful for you, can you mark it "This answer is useful"

Cannot link to static files with Hugo

I have this in config.toml:
baseURL = "https://my-username.github.io/blog/"
and there is a static file at static/img/foo.png.
Now, in content/posts/bar.md, I have the following content:
---
title: "Bar"
---
![foo](img/foo.png)
The picture isn't showing after I started the hugo server, so I inspected elements, and found out that Hugo generated the following URL for it:
http://localhost:1313/blog/posts/bar/img/hireme.png
This is not what I expect; it should be
http://localhost:1313/blog/img/hireme.png
When I use ![foo](/blog/img/foo.png), the picture is displayed correctly, but this is quite strange: /blog/ is part of baseURL, why do I need to type it again?
I think you should use <base> tag to make baseURL for static files.
Add the <base> tag into <head>:
<base href="{{ .Site.BaseURL }}">
And then you can insert image in post like this:
![Foo image](img/foo.jpg)
References:
https://www.w3schools.com/tags/tag_base.asp
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
In a simple setup where your site lives in the domain's root, a absolute reference to the image would be a standard practice. However, in your case where the site is nested, a more crafty solution is necessary.
I suggest using a shortcode to solve your dilemma. Just simply create a shortcode that takes in a resource file path and spits out the desired absolute path every time. The advantage to this approach is flexibility for future resource relocation to a cdn for example.
Your markdown code will be something like this:
![foo]({{< resource url="img/foo.png" >}})
Your shortcode template will be something like this:
{{ .Site.BaseURL }}{{ .Get "url" }}
Create a file named 'resource.html' in 'layouts/shortcodes/' folder, and drop in the shortcode template code above.
So when you decide you want to switch to a cdn you could easily do this:
://cdn.example.com/{{ .Get "url" }}

django - how to apply custom template filter to absolute url

I am trying to write a small urlshortener app.
everything is set up well so far. now I am trying to apply the filter to the full url:
{{ request.build_absolute_uri }}{{ request.get_absolute_url}} gives me the full url. But if apply my filter like this:
href="{{ request.build_absolute_uri }}{{ request.get_absolute_url|shortenme}}"
my filter isnot getting anything because request.get_absolute_url is alone giving empty string.
only {{ request.build_absolute_uri }}{{ request.get_absolute_url}} is giving the full url I want. How can I apply my filter to the full url?
Requests do not have a get_absolute_url method.
You want to use request.path or request.get_full_path. The second includes any query string.
See the docs on request attributes for more info.

How to render template into subdirectory with handlebars/ember

Say I have a handlebars file stored in some_directory/some_template.handlebars
I want to render this template using render command so that I can hook it up to child controller.
I've tried:
{{ render "someDirectory/someTemplate" post }}
{{ render "some_directory/some_template" post }}
none of these works. If I move some_template.handlebars to root directory, then it works when I do:
{{ render "someTemplate" }}
but I would like to avoid this since I'm finding that the root directory is getting cluttered. It's worth mentioning I use the ember-rails gem.
how are you packaging up the templates? are you sure it's even including the template in the browser?
You can find all the template names (keys) on Em.TEMPLATES object (see console in example below)
http://emberjs.jsbin.com/ejolaWOQ/1/edit
Ok,
I figured something out that works, you want to do:
<!-- note, did this in emblem, not sure if it translates to handlebars -->
{{ render 'nameOfController' someOptionalModel templateName='someDirectory/someTemplate' }}

Django path to current page

I have a django template page, and want a link from this page, containing current URL, for example, I am on /article/11 and want link to /article/11/remove
I tried the following construction:
Remove article
But I get link to /article/remove instead of /article/11/remove
However when I change it to
<a href="{{ request.path }}">
I get link to /article/11
How can I get URL not trimmed?
I don't see why it doesn't point you to /article/11remove, which is what it sounds like it should do, but either way, you're missing a slash. Try <a href="{{ request.path }}/remove"> instead.
However, that's really not the right way to do it. You shouldbe using {% url 'name_of_remove_view' %} to get the url, not assuming it's going to be wherever you are plus /remove.
Edit: In that case, your problem is probably that {{ request.path }} is not outputting anything at all. That would explain why just having "remove" would take you to /article/remove, and having "" would take you to where you currently are, due to the way that relative URLs work. You might want to make sure that you have a request object at all in your template environment.