Smarty
![]() | |
| Nhà phát triển | Monte Ohrt, Messju Mohr |
|---|---|
| Phiên bản ổn định | 4.3.2
/ 19 tháng 7 năm 2023[1] |
| Kho mã nguồn | |
| Thể loại | Hệ thống tạo mẫu web |
| Giấy phép | LGPL |
| Website | smarty.net |
Smarty là một hệ thống tạo mẫu web (web template system) được viết bằng ngôn ngữ lập trình PHP, thường được quảng bá như một công cụ phân tách mã nguồn sao cho mỗi phần chỉ phụ trách một vấn đề trong ứng dụng.[2] Hệ thống này cho phép thay đổi phần giao diện (front-end) của một trang web mà không tác động đến phần xử lý dữ liệu (back-end). Điều này giúp giảm thiểu công sức và chi phí cần thiết cho việc bảo trì phần mềm.
Smarty sử dụng các thẻ (tag) đặc biệt trong tài liệu có sẵn để tạo nội dung web. Các thẻ này được xử lý và chuyển đổi thành mã khác. Chúng là các chỉ thị đặc biệt cho Smarty và được đặt trong các dấu phân cách mẫu. Các chỉ thị có thể là biến (ký hiệu bằng dấu đô la $), hàm, câu lệnh logic và lệnh vòng lặp. Smarty cho phép lập trình viên PHP tạo ra các hàm tùy chỉnh có thể truy cập thông qua các thẻ Smarty.
Ví dụ
Vì Smarty tách mã PHP ra khỏi HTML, nên thường có hai tệp: một tệp là HTML, được dùng để quản lý phần hiển thị. Trong tệp HTML này, các biến và thẻ Smarty ({$title_text|escape} {$body_html}) được sử dụng và có thể trông như thế này:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{$title_text|escape}</title>
</head>
<body> {* This is a little comment that won't be visible in the HTML source *}
{$body_html}
</body> <!-- this is a little comment that will be seen in the HTML source -->
</html>
Cách sử dụng mẫu Smarty ở trên trong logic nghiệp vụ (business logic) có thể như sau:
define('SMARTY_DIR', 'smarty-2.6.22/');
require_once(SMARTY_DIR . 'Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = './templates/';
$smarty->compile_dir = './templates/compile/';
$smarty->assign('title_text', 'TITLE: This is the Smarty basic example ...');
$smarty->assign('body_html', '<p>BODY: This is the message set using assign()</p>');
$smarty->display('index.tpl');
Tham khảo
- ^ "v4.3.2". Truy cập ngày 7 tháng 9 năm 2023.
- ^ Parr, Terence John (2004). Enforcing strict model-view separation in template engines. Proceedings of the 13th international conference on World Wide Web. ISBN 1-58113-844-X.
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.










