작성자 : 박찬영

마지막 업데이트 : 2024년 05월 17일

if문

HubL if 문 및 unless 문을 사용하여 모듈 및 템플릿에 조건부 논리를 포함할 수 있습니다 . If 문에는 HubL  지원 연산자가 포함되는 경우가 많으며 표현식 테스트를 실행하는 데 사용할 수 있습니다 .   

기본 if문

HubL은 if 문을 사용하여 템플릿의 논리를 정의하는 데 도움을 줍니다. HubL if 문의 구문은 Python의 조건부 논리와 매우 유사합니다.
  • Hubl
{@% if condition %@}
    If the condition is true print this to template.
{@% endif %@}

elif와 else 사용하기

if추가 조건문을 사용하거나 조건이 거짓일 때 실행되는 규칙을 사용하면 명령문을 더욱 정교하게 만들 수 있습니다. elif문을 사용하면 이전 조건 이후에 평가될 논리에 추가 조건을 추가할 수 있습니다.
  • Hubl
{@% set number = 5 %@}

 

{@% if number <= 2 %@}

Variable named number is less than or equal to 2.

{@% elif number <= 4 %@}

Variable named number is less than or equal to 4.

{@% elif number <= 6 %@}

Variable named number is less than or equal to 6.

{@% else %@}

Variable named number is greater than 6.

{@% endif %@}

 

인라인 if 문

HubL은 인라인 if문을 지원합니다. 이는 연산자 및 표현식 테스트를 통해 간결한 방식으로 조건부 논리를 작성하는 데 사용할 수 있습니다 .
  • Hubl
{@% set color = "Blue" if is_blue is truthy else "Red" %@}     // color == "blue"  

{@{ "Blue" if is_blue is truthy else "Red" }@}     // "Blue"

{@% set dl = true %@}
<a href="http://example.com/some.pdf" {@{"download" if dl }@} >Download PDF</a>

삼항 연산자

또한 삼항 연산자를 사용하여 연산자 및 표현식 테스트 로 조건부 논리를 빠르게 작성할 수도 있습니다 .
  • Hubl
// If the variable is_blue is true, output "blue", otherwise output"red"
{@{ is_blue is truthy ? "blue" : "red" }@}

// Set the variable is_red to false if is_blue is true, otherwise set to true
{@% set is_red = is_blue is truthy ? false : true %@}

변경된 경우

if 및 unless 함수 외에도 HubL은 ifchanged 함수를 지원합니다. 이 함수는  태그를 이전에 호출한 이후 변수가 변경된 경우에만 마크업을 렌더링하는 데 사용할 수 있습니다.

unless 문

unless문은 if 역논리로 작동합니다. 
  • Hubl
{@% module "my_page_content" path="@hubspot/rich_text", label="Enter your page content", html="" export_to_template_context=true %@}

{@{ widget_data.my_page_content.html }@}

{@% unless widget_data.my_page_content.html %@}
<h1>This page is under construction.</h1>
<h3>Come back soon!</h3>
{@% endunless %@}