Công cụ thành viên

Công cụ trang web


programming4:template-engine

Khác biệt

Đây là những khác biệt giữa hai phiên bản của trang.

Liên kết đến bản xem so sánh này

Phiên bản trước của cả hai bênPhiên bản trước
Phiên bản sau
Phiên bản trước
programming4:template-engine [2018/04/18 14:46] vuthaoprogramming4:template-engine [2018/04/18 16:52] (hiện tại) – [Xử lý các hàm toán học trong template] vuthao
Dòng 5: Dòng 5:
 Từ phiển bản 4.4 Chúng tôi xây dựng giao diện mới dựa trêm Smarty, và chuyển dần từ Xtemplate sang Smarty 3 Từ phiển bản 4.4 Chúng tôi xây dựng giao diện mới dựa trêm Smarty, và chuyển dần từ Xtemplate sang Smarty 3
  
-Tài liệu hướng dẫn sử dụng Smarty 3: https://www.smarty.net/docs/en/ +Tài liệu hướng dẫn sử dụng Smarty 3:  
 +  * https://www.smarty.net/crash_course 
 +  * https://www.smarty.net/docs/en/
  
 ===== Chuyển đổi cách viết từ Xtemplate sang Smarty ===== ===== Chuyển đổi cách viết từ Xtemplate sang Smarty =====
Dòng 93: Dòng 94:
  
 **Chuyển sang Smarty code HTML** **Chuyển sang Smarty code HTML**
 +<code html>
 +{foreach $contacts as $contact}
 +        <br>
 +        phone: {$contact.phone}
 +        <br>
 +
 +        fax: {$contact.fax}
 +        <br>
 +
 +        cell: {$contact.cell}
 +        <br>
 +    {/foreach}
 +</code>
 +
 +Hoặc viết theo cách
 <code html> <code html>
     {section name=sec1 loop=$contacts}     {section name=sec1 loop=$contacts}
Dòng 111: Dòng 127:
 Ví dụ biến {$tile} Trong template cần xử lý ta có thể xử lý như sau: Ví dụ biến {$tile} Trong template cần xử lý ta có thể xử lý như sau:
 <code> <code>
-    {$tile|upper} viết hoa $$itle+{$tile|upper} viết hoa $itle
  
-    {$tile|lower} viết thường $$itle+{$tile|lower} viết thường $itle
  
-    {$tile|capitalize} viết hoa đầu từ $$itle+{$tile|capitalize} viết hoa đầu từ $itle
  
-    {$tile|strip_tags} Định dạng bỏ kí tự đặc biệt $$itle+{$tile|strip_tags} Định dạng bỏ kí tự đặc biệt $itle
  
-    {$date|date_format} Định dạng ngày thàng $$ate+{$date|date_format} Định dạng ngày thàng $ate
 </code> </code>
 +
 +Ngoài các ví dụ trên smarty còn hỗ trợ các phương thức xử lý biến sau:
 +<code>
 +capitalize
 +cat
 +count_characters
 +count_paragraphs
 +count_sentences
 +count_words
 +date_format
 +default
 +escape
 +from_charset
 +indent
 +lower
 +nl2br
 +regex_replace
 +replace
 +spacify
 +string_format
 +strip
 +strip_tags
 +to_charset
 +truncate
 +unescape
 +upper
 +wordwrap
 +</code>
 +
 +===== Xử lý các hàm toán học trong template =====
 +Ví dụ:
 +<code>
 +{$foo+1}
 +
 +{$foo*$bar}
 +
 +{* some more complicated examples *}
 +
 +{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}
 +
 +{if ($foo+$bar.test%$baz*134232+10+$b+10)}
 +
 +{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}
 +
 +{assign var="foo" value="`$foo+$bar`"}
 +</code>
 +
 +Mặc dù Smarty có thể xử lý một số biểu thức và cú pháp rất phức tạp, Nếu bạn thấy cú pháp khuôn mẫu của bạn trở nên quá phức tạp, bạn nên di chuyển vào PHP  để xử lý thông qua các block, module, plugins của NukeViet
 +
 +===== Xử lý Cấu trúc điều khiển  {if},{elseif},{else}  =====
 +<code html>
 +{if $name eq 'Fred'}
 +    Welcome Sir.
 +{elseif $name eq 'Wilma'}
 +    Welcome Ma'am.
 +{else}
 +    Welcome, whatever you are.
 +{/if}
 +
 +{* an example with "or" logic *}
 +{if $name eq 'Fred' or $name eq 'Wilma'}
 +   ...
 +{/if}
 +
 +{* same as above *}
 +{if $name == 'Fred' || $name == 'Wilma'}
 +   ...
 +{/if}
 +
 +
 +{* parenthesis are allowed *}
 +{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
 +   ...
 +{/if}
 +
 +
 +{* you can also embed php function calls *}
 +{if count($var) gt 0}
 +   ...
 +{/if}
 +
 +{* check for array. *}
 +{if is_array($foo) }
 +   .....
 +{/if}
 +
 +{* check for not null. *}
 +{if isset($foo) }
 +   .....
 +{/if}
 +
 +
 +{* test if values are even or odd *}
 +{if $var is even}
 +   ...
 +{/if}
 +{if $var is odd}
 +   ...
 +{/if}
 +{if $var is not odd}
 +   ...
 +{/if}
 +
 +
 +{* test if var is divisible by 4 *}
 +{if $var is div by 4}
 +   ...
 +{/if}
 +
 +
 +{*
 +  test if var is even, grouped by two. i.e.,
 +  0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc.
 +*}
 +{if $var is even by 2}
 +   ...
 +{/if}
 +
 +{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}
 +{if $var is even by 3}
 +   ...
 +{/if}
 +</code>
 +
 +Hoặc ví dụ:
 +<code html>
 +{if isset($name) && $name == 'Blog'}
 +     {* do something *}
 +{elseif $name == $foo}
 +    {* do something *}
 +{/if}
 +
 +{if is_array($foo) && count($foo) > 0}
 +    {* do a foreach loop *}
 +{/if}
 +</code>
 +
 +===== Lấy giá trị của hằng đã được định nghĩa  =====
 +
 +Ví dụ template muốn lấy 1 hằng đã được định nghĩa có thể dùng biến sau:
 +{$smarty.const.CONST_NAME}
 +
 +Ví dụ: NV_ROOTDIR: {$smarty.const.NV_ROOTDIR}
 +
programming4/template-engine.1524037586.txt.gz · Sửa đổi lần cuối: 2018/04/18 14:46 bởi vuthao