You are previewing Smarty.
O'Reilly logo
Smarty

Book Description

Smarty is a templating engine for PHP. Designers who are used to working with HTML files can work with Smarty templates, which are HTML files with simple tags while programmers work with the underlying PHP code. The Smarty engine brings the code and templates together. The result of all this is that designers can concentrate on designing, programmers can concentrate on programming, and they don't need to get in each others way so much.
Even if you are developing a site on your own, Smarty is a powerful way to make your code clearer to you and others, as well as easier to debug and modify later.

Table of Contents

  1. Smarty
    1. Copyright
    2. Credits
    3. About the Authors
    4. About the Reviewer
    5. Preface
      1. What This Book Covers
      2. Conventions
      3. Reader Feedback
      4. Customer Support
        1. Downloading the Example Code for the Book
        2. Errata
        3. Questions
    6. 1. Introduction to Smarty
      1. Templating Systems
        1. Why Use a Templating System?
        2. The Smartness of Smarty
        3. Smarty Alternatives
      2. A Rough Guide to the Software Design Process
        1. Working in Teams: Layers and Separation of Concerns
      3. Smarty—The Ultimate Templating System for PHP
        1. Is Smarty Fast?
        2. Is Smarty Secure?
      4. Smarty's Main Features
        1. Variable Modifiers
        2. Template Functions
        3. Debugging
        4. Plug-ins
        5. Filters
      5. Smarty Internals
      6. Installing and Configuring Smarty
        1. Step 1: Obtaining Smarty
        2. Step 2: Configure PHP to Find the Smarty Libraries
          1. On Windows
          2. On Linux
        3. An Alternative to Step 2: Using Smarty without Having Full Access to the System
        4. Step 3: Set Up Smarty for Your Application
        5. Step 4: Verifying the Installation
        6. Smarty Development Versions on CVS
      7. Upgrading a Smarty Site
      8. Summary
    7. 2. Smarty Site Architecture
      1. Separation of Concerns
        1. What Does Concern Mean?
        2. A Problem-Solving Perspective
        3. Cross-cutting Concerns
      2. Roles Involved in Building and Maintaining a Website
      3. Starting a Smarty Project
        1. Directory Structure
        2. Securing our Smarty Project
        3. The Data Access Layer
          1. How it Works
        4. The Business Logic Layer
          1. How it Works
        5. The Presentation Layer
          1. How it Works
        6. The Result
      4. Summary
    8. 3. What Designers Need to Know
      1. Development Team Problems: Common Scenarios
      2. Roles of a Template Designer and a Programmer
      3. Definitions and Concepts for Designers
        1. Concept of Reusability and Components
        2. Splitting into Components
        3. How to Design Table-less Layouts?
        4. Handy Built-in Tags
      4. Choosing an Editor for Template Design
      5. Collaborating with Programmers
      6. Summary
    9. 4. Creating a Template
      1. Design Concepts, from HTML to TPL
        1. profiles.tpl
        2. profiles.php
      2. Introduction to Smarty Variables
        1. sample.tpl
        2. sample.php
      3. Starting Templates
        1. Non-associative Array
        2. Associative Array
        3. Passing Arrays to Smarty Templates and Manipulating Them
          1. associative_array.php
          2. oop_style.php
      4. Basic Templating
        1. Logical Conditions
          1. sample_if.tpl
        2. Loops
          1. section
            1. sample_section.tpl
            2. sample_section2.tpl
            3. sample_section2.php
          2. index
          3. first
          4. iteration
          5. total
          6. loop
          7. foreach
            1. sample_section2.tpl
            2. sample_section2.php
      5. Templates in the Real World
        1. Calendar
          1. calendar.tpl
          2. calendar.php
        2. Database Report
          1. database_report.php
          2. database_report.tpl
        3. Data Input Forms
          1. survey.tpl
        4. Email Newsletter
          1. newsletter.tpl
          2. newsletter.php
        5. Running PHP Code Inside your Templates
          1. php_code.tpl
          2. php_code.php
      6. Summary
    10. 5. Advanced Templating
      1. Smarty under the Hood
        1. profiles.tpl
        2. %%EB^EB9^EB93DE89%%profiles.tpl.php
          1. Compile Steps
          2. Prefilters and Postfilters
          3. What is a Smarty Modifier?
      2. Stretch your Imagination with Smarty
        1. Student Transcript
          1. Table Name: Grade
          2. transcript.php
        2. Photo Gallery
          1. gallery.php
      3. Available Modifiers
        1. capitalize
          1. capitalize.tpl
          2. capitalize.php
        2. count_characters
          1. count_characters.tpl
          2. count_characters.php
        3. cat
          1. cat.tpl
          2. cat.php
        4. count_paragraphs
          1. count_paragraphs.tpl
          2. count_paragraphs.php
        5. count_sentences
        6. count_words
          1. count_words.tpl
          2. count_words.php
          3. Output
        7. date_format
          1. date_format.tpl
          2. Output
        8. default
          1. default.tpl
        9. escape
          1. escape.tpl
          2. escape.php
          3. Output
        10. indent
          1. indent.tpl
          2. Output
        11. lower
        12. upper
          1. upper_lower.tpl
          2. Output
        13. nl2br
          1. nl2br.tpl
          2. Output
        14. regex_replace
          1. regex_replace.tpl
          2. Output
        15. replace
          1. replace.tpl
          2. Output
        16. spacify
          1. spacify.tpl
        17. string_format
          1. string_format.tpl
          2. Output
        18. strip
        19. strip_tags
          1. strip_tags.tpl
          2. Output
        20. truncate
          1. truncate.tpl
          2. Output
        21. wordwrap
          1. wordwrap.tpl
          2. Output
      4. Combining Modifiers
        1. date_format.tpl
        2. Output
      5. Configuration Files
        1. sometemplate.tpl
        2. footer.tpl
      6. Summary
    11. 6. Smarty Functions
      1. Types of Smarty Functions
      2. Functions in Action
        1. index.tpl
        2. about.tpl
        3. contact.tpl
          1. Action: Re-using Page Elements with the include Function
          2. Explanation
      3. Inserting Dynamic Content
      4. Passing Variables to Included Templates
      5. Saving Variables in Configuration Files
      6. Creating Configuration Sections for Each Page
      7. Handling Lists in Templates
      8. Removing Extra White Space from Templates
      9. Handling JavaScript Code in Templates
      10. Processing Deeply Nested Arrays
      11. Cycling Through a List of Values
      12. Avoiding Spam Indexers
      13. Form-Related Functions
      14. More Form-Related Functions
      15. Summary
    12. 7. Debugging for Designers
      1. Debugging Smarty Templates
      2. Semantic Errors
      3. Common Smarty Errors
      4. Other Common Smarty Errors
      5. Smarty Debug Console
      6. Summary
    13. 8. Built-in Smarty Variables and Methods
      1. Built-in Smarty Variables
        1. $template_dir
        2. $compile_dir
        3. $config_dir
        4. $plugins_dir
        5. $debugging
        6. $error_reporting
        7. $debug_tpl
        8. $debugging_ctrl
        9. $compile_check
        10. $force_compile
        11. $caching
        12. $cache_dir
        13. $cache_lifetime
        14. $cache_modified_check
        15. $php_handling
        16. $security
        17. $secure_dir
        18. $security_settings
        19. $trusted_dir
        20. $left_delimiter
        21. $right_delimiter
        22. $request_vars_order
        23. $request_use_auto_globals
        24. $compile_id
        25. $use_sub_dirs
        26. $default_modifiers
        27. $default_resource_type
        28. $cache_handler_func
        29. $autoload_filters
        30. $config_overwrite
        31. $config_booleanize
        32. $config_read_hidden
        33. $config_fix_newlines
        34. $default_template_handler_func
        35. $compiler_file
        36. $compiler_class
        37. $config_class
          1. Handy Built-in Smarty Variables Table
      2. Built-in Smarty Methods
        1. assign
        2. assign_by_ref
        3. Example: Working of assign and assign_by_ref
        4. How it Works
        5. The Result
        6. append
        7. append_by_ref
        8. clear_assign
        9. register_function
        10. unregister_function
        11. register_object
        12. unregister_object
        13. register_block
        14. unregister_block
        15. register_compiler_function
        16. unregister_compiler_function
        17. register_modifier
        18. unregister_modifier
        19. register_resource
        20. unregister_resource
        21. register_prefilter
        22. unregister_prefilter
        23. register_postfilter
        24. unregister_postfilter
        25. register_outputfilter
        26. unregister_outputfilter
        27. load_filter
        28. clear_cache
        29. clear_all_cache
        30. is_cached
        31. clear_all_assign
        32. clear_compiled_tpl
        33. template_exists
        34. get_template_vars
        35. get_config_vars
        36. trigger_error
        37. display
        38. fetch
        39. config_load
        40. get_registered_object
        41. clear_config
      3. Summary
    14. 9. Caching and Performance
      1. Caching in Smarty
      2. Dynamically Caching Template Sections
      3. Clearing the Cache
      4. Advanced Caching Features
      5. Using Cache Groups
      6. Clearing a Cache Group
      7. Avoiding the Cache
        1. Disabling a Template Cache
        2. Using {insert} to Avoid Caching
        3. Creating a Custom Plug-in to Avoid Caching on Portions of a Template
      8. Creating a Custom Cache Handler
      9. Optimizing Smarty Applications
      10. Profiling PHP
      11. Designing Sites for Effective Caching
        1. The Last-Modified and ETag Headers
        2. The Expires Header
        3. The Cache-Control Header
      12. Tools: ApacheBench (ab)
      13. Tools: Xdebug
      14. Tools: WinCacheGrind
      15. Summary
    15. 10. Extending Smarty with Plug-ins
      1. Finding and Installing Plug-ins
      2. Useful Plug-ins
        1. HTML List Plug-in
        2. File Size Format Plug-in
        3. Google Highlight Plug-in
      3. Writing your own Plug-ins
      4. Plug-in Types
        1. Functions
        2. Modifiers
        3. Block Functions
        4. Compiler Functions
        5. Prefilters, Postfilters, and Output Filters
        6. Resources
        7. Inserts
      5. Registering Plug-ins
      6. Example Plug-in: Calendar
      7. Example Plug-in: Auto-link URLs
      8. Summary
    16. 11. Filters
      1. Prefilters
      2. Postfilters
      3. Output Filters
      4. Creating Filters
      5. Registering a Filter at Run Time
      6. Manually Loading a Filter
      7. Automatically Loading a Filter
      8. Filter #1: Remove HTML Comments
      9. Filter #2: Benchmark Information
      10. Filter #3: Compress Output with gzip
      11. Filter #4: Search Engine Highlight
      12. Summary
    17. 12. Internationalization
      1. Translation Infrastructure: Gettext
      2. Configuring PHP with Gettext
      3. Simple PHP Example
      4. Setting Up the Gettext Files
      5. Using Gettext with Smarty
      6. Generating a PO File
      7. Advanced Features of Smarty Gettext
      8. Summary