source

HTML을 포함하는 문자열을 twig 템플릿에 표시하는 방법

goodcode 2023. 1. 19. 21:27
반응형

HTML을 포함하는 문자열을 twig 템플릿에 표시하는 방법

HTML 태그를 포함하는 문자열을 twig 템플릿에 표시하려면 어떻게 해야 합니까?

내 PHP 변수에는 다음 html과 텍스트가 포함되어 있습니다.

$word = '<b> a word </b>';

이 작업을 나뭇가지 템플릿으로 수행할 경우:

{{ word }}

알겠네.

&lt;b&gt; a word &lt;b&gt;

대신 이걸 원해요

<b> a word </b>

이렇게 쉽게 구할 수 있을까요?

raw 키워드 http://twig.sensiolabs.org/doc/api.html#escaper-extension 를 사용합니다.

{{ word | raw }}

다음 항목도 사용할 수 있습니다.

{{ word|striptags('<b>')|raw }}

그래서만<b>태그가 허용됩니다.

{{ word|striptags('<b>,<a>,<pre>')|raw }}

여러 태그를 허용하려면

변수가 필요 없는 경우 텍스트 정의
translations/translations.en.translations:
CiteExampleHtmlCode: "<b> my static text </b>"

나뭇가지와 함께 사용합니다.
templates/about/index.twig
… {{ 'CiteExampleHtmlCode' }}
저처럼 여러 개의 혈관이 필요한 경우:
… {{ 'CiteExampleHtmlCode' | trans }}

번역 사용에 대한 자세한 내용은 https://symfony.com/doc/current/translation.html을 참조하십시오.

언급URL : https://stackoverflow.com/questions/8355123/how-to-display-string-that-contains-html-in-twig-template

반응형