Ֆունկցիաներ - Twig կաղապարներում
Twig տրամադրում է մի շարք հարմար ֆունկցիաներ, որոնք կարելի է օգտագործել անմիջապես կաղապարներում։
Drupal միջուկը ավելացնում է մի քանի օգտվողային ֆունկցիաներ, որոնք հատուկ են Drupal-ին։ Դրանք սահմանված են TwigExtension դասում։
Դուք նաև կարող եք սահմանել ձեր սեփական Twig ֆունկցիաները օգտվողի մոդուլում (սակայն ոչ թեմայում)։ Այդ մասին օրինակի համար տեսեք այս օրինակն core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php ֆայլում։
attach_library($library)
Միացնում է ակտիվների գրադարանը կաղապարին։
{{ attach_library('classy/node') }}
create_attribute($attributes)
Ստեղծեք Attribute օբյեկտներ create_attribute() ֆունկցիայով Twig կաղապարում։ Այս օբյեկտները կարելի է մանիպուլացնել այնպես, ինչպես մյուս Attribute օբյեկտները, որոնք փոխանցվում են Twig կաղապար։
Տես փոփոխությունների գրառումը՝ https://www.drupal.org/node/2818293
{% set my_attribute = create_attribute() %} {% set my_classes = [ 'kittens', 'llamas', 'puppies', ] %} <div{{ my_attribute.addClass(my_classes).addAttribute('id', 'myUniqueId') }}> {{ content }} </div> <div{{ create_attribute({'class': ['region', 'region--header']}) }}> {{ content }} </div>
file_url($uri)
Այս օգնության ֆունկցիան ընդունում է ֆայլի URI և ստեղծում ֆայլի հարաբերական URL-ուղի։
{{ file_url(node.field_example_image.entity.uri.value) }}
link($text, $uri, $attributes)
Այս օգնության ֆունկցիան որպես առաջին պարամետր ընդունում է texturi-ն։
Օրինակներ՝
{{ link(item.title, item.uri, { 'class':['foo', 'bar', 'baz']} ) }}
path($name, $parameters, $options)
Ստեղծում է [հարաբերական] URL-ուղի՝ նշելով երթուղու անունն ու պարամետրերը։
{# Link to frontpage view. #} <a href="{{ path('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a> {# Link to user entity/profile page. #} <a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a> {# Link to node page. #} <a href="{{ path('entity.node.canonical', {'node': node.id}) }}">{{ 'View node page'|t }}</a>
url և path ֆունկցիաները սահմանված են մոտավորապես նույն վայրում, որտեղ գտնվում են \Symfony\Bridge\Twig\Extension\RoutingExtension-ում։
url($name, $parameters, $options)
Ստեղծեք հստակ URL՝ երթուղու անվան և պարամետրերի հիման վրա․
<a href="{{ url('view.frontpage.page_1') }}">{{ 'View all content'|t }}</a>
Ստեղծեք հստակ URL ընթացիկ URL-ի համար․
<a href="{{ url('<current>') }}">{{ 'Reload'|t }}</a>
Ստեղծեք հստակ URL գլխավոր էջի համար․
<a href="{{ url('<front>') }}">{{ 'Home'|t }}</a>
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.