Directives ใน Freemarker คือคำสั่งที่ใช้เพื่อกำหนดการแสดงผลแบบเฉพาะเจาะจงใน templates. Directives สามารถมีผลกับความยืดหยุ่นและความสามารถในการแสดงผลของ template ได้.
ต่อไปนี้เป็นรายละเอียดของ Directives ที่ใช้บ่อยใน Freemarker:
<#list ...> ... </#list>
ใช้สำหรับการวนซ้ำผ่านรายการ (list) หรือ map.
<#list items as item>
 ${item} </#list> 
<#if ...> ... 
<#elseif ...> ... 
<#else>
 ... 
</#if>
ใช้สำหรับการตรวจสอบเงื่อนไข.
<#if condition>
 It's true. 
<#else>
 It's false. 
</#if> 
<#assign ...>
ใช้สำหรับการกำหนดค่าให้กับตัวแปร.
<#assign x = "Hello"> 
<#include ...>
ใช้สำหรับการแทรกเนื้อหาจาก template อื่น.
<#include "header.ftl"> 
<#macro ...> ... 
</#macro> และ 
<@ ... >
Macros ใช้สร้างส่วนที่สามารถใช้ซ้ำได้ใน templates. และคุณสามารถเรียกใช้ macro ด้วย <@ ... >.
<#macro sayHello name>
 Hello, ${name}! 
</#macro> 
<@sayHello name="John" /> 
<#import ...>
ใช้สำหรับการนำเข้า macros จาก template อื่น.
<#import "macros.ftl" as myMacros> 
<@myMacros.someMacro /> 
<#break>
ใช้สำหรับการหยุดการวนซ้ำทันที.
<#list items as item> 
<#if item == "stop"> 
<#break> 
</#if> 
${item} 
</#list> 
<#switch ...> ... 
<#case ...> ... 
<#default> ... 
</#switch>
ใช้สำหรับการตรวจสอบเงื่อนไขหลายๆ แบบ.
<#switch value> 
<#case "A"> This is A. 
<#case "B"> This is B. 
<#default> Neither A nor B. 
</#switch> 
Directives ใน Freemarker มีความสามารถที่เป็นประโยชน์ในการจัดการการแสดงผลของเทมเพลต และสามารถใช้งานร่วมกับข้อมูลที่ถูกส่งมาจากภาษาโปรแกรมอื่นๆ เพื่อสร้างเอาต์พุตตามที่คุณต้องการ.
| จำนวนคนดู : 2,049 | วันที่สร้าง : 08/09/2023 |