Skip to content
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

Add documentation on how to use lang functions in template code #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
67 changes: 67 additions & 0 deletions development/extensions/tutorial_templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ You can also define simple (boolean, integer or double) variables from inside th
if you dont want to copy & paste complex ``if`` expressions over and over again:

.. code-block:: twig
:force:

{% if expr1 %}
{% DEFINE $COLSPAN = 3 %}
Expand Down Expand Up @@ -372,3 +373,69 @@ User variables can be cleared (unset) using:
.. code-block:: twig

{% UNDEFINE $COLSPAN %}

Language variables
------------------

You can use and interact with language variables in multiple ways. phpBB extends the standard twig syntax with the
following functions to be able to use language variables within twig based template code:

- ``lang``
Get output for a language variable similar to using ``{{ L_FOO }}``.
The following two lines output the same result:

.. code-block:: twig
:force:

{{ lang('FOO') }}
{{ L_FOO }}

It supports the same parameter syntax as ``$language->lang()``:

.. code-block:: twig

{{ lang('SOME_VAR', param1, param2) }}

For language variables in Javascript, please use ``lang_js``.

- ``lang_js``
Get output for language variable in JS code. This should be used instead of the previously used ``LA_`` prefix.
The following two lines output the same result:

.. code-block:: twig
:force:

{{ lang_js('FOO') }}
{{ LA_FOO }}

It supports the same parameter syntax as ``$language->lang()``:

.. code-block:: twig

{{ lang_js('SOME_VAR', param1, param2) }}

Essentially this is the equivalent of:

.. code-block:: twig

{{ lang('SOME_VAR', param1, param2) | escape('js') }}

- ``lang_defined``
Checks whether a language variable is defined. Can be used to check for existence of a language variable before
calling ``lang`` or ``lang_js``.

.. code-block:: twig

{% if lang_defined('MY_LANG_VAR') %}
<span>{{ lang('MY_LANG_VAR') }}</span>
{% endif %}

- ``lang_raw``
Outputs the raw value of a language variable, e.g. a string or array. It can be used to access entries of a language
array in the template code.

.. code-block:: twig

{% for step in lang_raw('EXTENSION_UPDATING_EXPLAIN') %}
<li>{{ step }}</li>
{% endfor %}
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 get_class?

Copy link
Member Author

@marc1706 marc1706 Jul 7, 2024

Choose a reason for hiding this comment

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

Is that a lang function? 😄

Copy link
Member

Choose a reason for hiding this comment

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

Loading