Replace absolute links to relative ones Drupal 8 - drupal-8

There was a need to replace all links on the site with relative ones.
What is the best way to do this?
Tried with hook_link_alter or template_preprocess_html but I don't seem to understand how they work or can't be done with them.
I tried the "pathologic" module - also did not get any effect.
Links are set in the template (twig) by variables {{ *.url }} or simply {{ url }}.
Which way to look?

Related

{% extends 'NavigationEG/layout.html' %}

in flask templates, we combine HTML pages using extends, but my view(HTML) files are in diff folder. so how u can extend that? how to set a path? eg: {% extends 'NavigationEG/layout.html' %} NavigationEG is my folder but this way not work
without a folder, it works but I want to store it in one folder.
Like bergp said in this related question on stackoverflow, flask is looking for templates in the given templates folder. If you want to reference a different folder in your template, you could remove the template_folder when initlializing the app and refering to the full path in the render_template parameters.
Depending on the reason for the different folders, blueprints can have their own template_folder as well.
I hope this helps. If not, please share your file structure and parts of the code so we can help you find the source of this problem. Good luck.

Embed PDF media file in Django template

The question is that I want to embed a PDF file through <embed>, <iframe> tag, or similar in a Django template using {{ object.file.url }}, which is presumed to be a file saved in the /MEDIA/ folder by a Django model.
The fact is that if I embed a PDF file coming from /STATIC/ folder, everything goes well. However, if I try the same with the {{ object.file.url }}, it does not work.
The curious thing is that if I simply display the {% static 'path/pdf.pdf' %} and {{ object.file.url }}, they both give me a valid link like this:
/static/img/111.pdf
/media/projects/phases-guide/111.pdf
If I try to open them separately, they work perfectly. But at the moment of embedding them in a HTML tag, it only works with the static url file.
Can someone tell me any idea why this is happening?
Anyway, is there any alternative to "EMBED FILE COMING FROM MEDIA FOLDER ROOT"?
I tried to enable read media files, but anyway it is not working.
The problem is that clickjack protection is being enabled on the MEDIA files that you are trying to embed.
You can see in the error console in your browser’s developer mode that ”X-Frame-Options” is set to ”Deny” for MEDIA files but not for STATIC files.
There are several ways of changing this per view using method or class decorators, but that didn’t work in my code, so I did a generic change in SETTINGS.py like this:
X_FRAME_OPTIONS = ”SAMEORIGIN”
Then it worked for me.

Jinja2 django 1.6 How to eliminate quotes from include tag?

Is this a bug? I have codes in template file like:
<div class="row" id="tags">
{%include 'y.html'%}
</div>
then jinja2 will render as:
But I never have any quotes in my template. And if I directly place codes in y.html in div section, this problem won't happen.
So, If I have to use include tag, How can I eliminate those annoying quotes?
EDIT:
Thanks for Daniel Roseman, the quotes only exist in Chrome tools, not in the actual html code. BUT:
If I use Chrome browser and use include tag, the layout is abnormal:
If I use Chrome browser and don't use include tag , the layout is OK:
So, there must be something wrong with jinja2 or chrome.
If it is caused by Jinja2, then how to solve this problem? Thanks.
EDIT2:
more strange things: if I move the <div class="row" id="tags"></div> into y.html, the problem will disappear even if I still use include tag.
This may be the solution, but still I don't why.
This isn't Jinja2 doing anything. This is your just your browser's developer tools. The actual HTML will be fine.

Template Partials in Symfony 2

Are there such things as partials in Symfony 2, reusable templates from anywhere, effectively?
I have found include http://twig.sensiolabs.org/doc/tags/include.html but this only allows the rendering of a template in a specific directory structure.
What I really want a folder that contains all my partial templates, rather than lumping them into my main views?
So I want to be able to do something like
{% include "Bundle:Default:Partials:view.html.twig" %}
Update
I do not want to use the enforced structure of Bundle:Controller:Template structure. I do not want to use this as it means putting all my template partials in with my main view templates. I need something that lets me do Bundle:Controller:PartialDir:Template
You can already do that. The symfony2 docs has a section describing how to do this.
http://symfony.com/doc/current/book/templating.html#including-other-templates
In Symfony 2.4 (the version I am currently using but it probably works in other 2.x versions as well) you can do the following:
{% include '::_partials/partial.html.twig' %}
or in Symfony 2.2+ using the include function
{{ include('::_partials/partial.html.twig') }}
This will look for the partial.html.twig template inside of the app/Resources/views/_partials directory. You can obviously name the _partials directory whatever you want. It also works without the '::' prefix.
I know this is old, but the way to achieve what OP asks is the following:
Rather than doing
bundle:controler:partialDir:template
we have to switch it slightly to achieve:
{% include 'Bundle:PartialDir/Controller:Template' %}
To include the controller, you'll need to refer to it using the standard string syntax for controllers (i.e. bundle:controller:action):
{{ render(controller(
'App\\Controller\\ArticleController::recentArticles',
{ 'max': 3 }
)) }}

How to run JSLint / JSHint in a file with templates?

I'm trying to introduce some tools at work to improve code quality. An obvious solution that I'd used at a previous company was to run jslint before checking in code.
The problem is that we are using Django to do our templating (though, I assume we would have a similar problem with other templating languages).
How is it possible to take code like the below, and have it JSLint/JSHint properly while ignoring the template tags?
var a = { "test" : "test"};
{% comment %}
{% endcomment %}
{{ my_variable }}
window.x = "y";
I've seen this question specifically regarding JSHint, which looks like it could handle some cases, but it doesn't address inlining variables, like {{ my_variable }} above.
Is there any way to ignore certain lines using JSHint/JSLint, or otherwise get the linting to execute correctly?
Much like linting coffeescript with these tools, you're trying to lint content outside it's pervue. Probably easiest to run the template with dummy values, and JSHint that resulting file.
Probably duplicate of How to run JSHint on files with Django template markup in them?