Skip to content

Add file_relative function to the Twig reference #10115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 25, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions reference/twig_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,27 @@ file_link
Generates a link to the provided file and line number using
a preconfigured scheme.

file_relative
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse me for the nitpicking ... but wouldn't |relative_path be a better name than |file_relative for this filter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, but that one already exists for urls.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@yceruto yceruto Jul 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least it is consistent with |file_link filter. Maybe should be |file_relative_path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant, even if there is no name conflict between functions and filters, it could be confusing to have two helpers with the same name and different goals.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Let's keep this name then. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about relative_file_path instead?

~~~~~~~~~~~~~

.. code-block:: twig

{{ file|file_relative }}

``file``
**type**: ``string``

Returns the relative path from the passed absolute file path. For example,
assume you've a project in the following directory: ``/var/www/blog/``.

.. code-block:: twig

{{ '/var/www/blog/templates/admin/index.html.twig'|file_relative }}
{# templates/admin/index.html.twig #}

If the passed path doesn't match the project directory, a ``null`` value
will be returned.

.. _reference-twig-tags:

Tags
Expand Down