### Installing Spitfire from Source (Shell)
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireInstallationInstructions.md
Provides shell commands to clone the Spitfire repository from GitHub, change into the directory, and build/install the project using `setup.py`. Requires Git, Python, and setuptools.
```Shell
$ git clone https://github.com/youtube/spitfire.git spitfire-read-only
$ cd spitfire-read-only
$ python ./setup.py build
$ python ./setup.py install
```
--------------------------------
### Installing Spitfire (Bash)
Source: https://github.com/re-masashi/spitfire/blob/master/docs/quickstart.rst
Installs the Spitfire library using the Python package installer, pip.
```bash
python -m pip install spitfire3
```
--------------------------------
### Rendering Spitfire Template (Python)
Source: https://github.com/re-masashi/spitfire/blob/master/docs/quickstart.rst
Initializes a Spitfire environment, loads templates from the 'templates' directory, and renders the 'fruits' template with sample data, printing the resulting HTML.
```python
import spitfire
env = spitfire.Environment('templates')
# 'templates' folder becomes the "home" of this Environment
env.load_dir() # no parameters on load_dir() loads the "home" directory
print(env.render_template('fruits', {"list": [
{"name": "apple", "price": '$0.3'},
{"name": "kiwi", "price": "$0.7"},
{"name": "banana", "price": "$0.5"},
]}))
```
--------------------------------
### Compiling a Spitfire Template (Shell)
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireInstallationInstructions.md
Shows the shell command to run the `spitfire-compiler` executable on the `example.spt` file. This command generates a Python file (`example.py`) from the template. Requires the Spitfire compiler to be installed.
```Shell
spitfire-compiler example.spt
```
--------------------------------
### Creating Template Directory (Bash)
Source: https://github.com/re-masashi/spitfire/blob/master/docs/quickstart.rst
Creates a directory named 'templates' which will serve as the home for Spitfire templates.
```bash
mkdir templates
```
--------------------------------
### Running Python Script (Bash)
Source: https://github.com/re-masashi/spitfire/blob/master/docs/quickstart.rst
Executes the Python script 'main.py' from the terminal to perform the template rendering.
```bash
$ python main.py
```
--------------------------------
### Defining Spitfire Template (Spitfire)
Source: https://github.com/re-masashi/spitfire/blob/master/docs/quickstart.rst
Defines a simple Spitfire template ('fruits.spf') that iterates over a list of fruit objects and displays their names and prices within list items.
```spitfire
#for $fruit in $list
- $fruit.name costs $fruit.price
#end for
```
--------------------------------
### Defining a Spitfire HTML Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireInstallationInstructions.md
Defines a simple HTML template file (`example.spt`) using Spitfire syntax. It includes a placeholder `$name` that will be substituted during rendering.
```HTML
Spitfire example
Hello, $name!
```
--------------------------------
### Rendering a Compiled Spitfire Template (Python)
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireInstallationInstructions.md
Demonstrates how to import the compiled Python template module (`example.py`), instantiate the template class, pass data via a `search_list`, and call the `main()` method to render the template. The output is printed to the console. Requires the compiled `example.py` file to be in the Python path.
```Python
import example
data = example.example(search_list=[{'name':"Trurl"}]).main()
print data
```
--------------------------------
### Extended Placeholders and Filters - Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
Examples showing the syntax for extended placeholders in Spitfire, including applying specific filters (filter=, raw) and format strings (format_string=).
```Spitfire Template
${placeholder_expression}
${placeholder_expression|filter=html_escape}
${placeholder_expression|raw}
${placeholder_expression|format_string='%3.3f'}
```
--------------------------------
### Example Spitfire HTML Template
Source: https://github.com/re-masashi/spitfire/blob/master/README.md
This snippet shows a basic HTML template using Spitfire syntax, including variable substitution ($title, $user.url, $user.name) and a loop directive (#for, #end for) to iterate over a list of users.
```html
$title
```
--------------------------------
### Array Slicing (Not Allowed) - Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
Shows an example of array slicing within a loop directive, which is legal in Cheetah but explicitly disallowed in Spitfire to encourage data fetching logic in the backend.
```Spitfire Template
#for $user in $recent_users[:10]
...
#end for
```
--------------------------------
### Initializing Spitfire Environment in Python
Source: https://github.com/re-masashi/spitfire/blob/master/docs/index.rst
This snippet demonstrates how to initialize the Spitfire environment, load templates from a directory, and render a template with context data. It shows the basic workflow for using Spitfire in a Python application.
```python
import spitfire
env = spitfire.Environment()
env.load_dir('templates')
print(env.render('ample', {"name": "John", "age": 10}))
```
--------------------------------
### Creating a Basic Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/docs/index.rst
This snippet shows a simple Spitfire template file (.spf) demonstrating variable substitution ($name, $age), variable assignment (#set), and conditional logic (#if, #else, #end if). It illustrates the basic syntax for creating dynamic content.
```spitfire
$name is $age years old
#set $gap = 18- $age
#if ($age >= 18)
$name can drive a car or a bike.
#else
$name can only ride a bicycle for now. Please wait for $gap years.
#end if
```
--------------------------------
### Using Conditionals (if/elif/else) in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Illustrates the syntax for implementing conditional logic in Spitfire templates using '#if', '#elif', '#else', and '#end' directives to control content rendering based on conditions.
```spitfire
#if condition1
true value
#elif condition2
elseif value
#else
else value
#end
```
--------------------------------
### Calling Macros in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Shows the simple syntax for invoking a previously defined macro within a Spitfire template, passing the required arguments.
```spitfire
macro_name(1,2)
```
--------------------------------
### Defining Macros in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Explains how to define reusable blocks of code, similar to functions, using the '#def' and '#end def' directives. Macros can accept positional and keyword arguments.
```spitfire
#def macro_name($arg1, $arg2, $keyword_arg='default value')
Hey, you had $arg1, $arg2, $keyword_arg
#end def
```
--------------------------------
### Importing Modules in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Explains how to import Python modules or specific objects from modules into the Spitfire template environment using '#from' or '#import' directives, making them available for use.
```spitfire
#from pprint import pprint
## or
#import json
```
--------------------------------
### Multiple Extends Directives - Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
Demonstrates the use of multiple #extends directives in a single Spitfire template, which is allowed and encouraged, unlike Cheetah's default behavior.
```Spitfire Template
#extends base_html ## sets up a standard html page
#extends layout.three_column ## sets up some more html and gives you three defined areas
#def column1()
Hello column one!
#end def
```
--------------------------------
### Passing Keyword Arguments (Incorrect - Dollar Sign) - Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
Demonstrates the incorrect syntax for passing keyword arguments with a '$' prefix on the keyword name, which is not allowed in Spitfire.
```Spitfire Template
$function($keyword='value')
```
--------------------------------
### Stripping Lines (Condensation) in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Demonstrates how to use the '#strip_lines' and '#end strip_lines' directives to remove newlines and unnecessary whitespace from the enclosed content, useful for reducing output size.
```spitfire
#strip_lines
Your content here
#end strip_lines
```
--------------------------------
### Extending a Base Template in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Shows how a template (`page.spf`) can inherit from a base template (`base.spf`) using the '#extends' directive and override specific blocks defined in the base template.
```spitfire
#extends base
#def title()
Tales of a Loner
#end def
```
--------------------------------
### Spitfire Performance Benchmark Results
Source: https://github.com/re-masashi/spitfire/blob/master/README.md
Output from a benchmark script comparing Spitfire's rendering performance against other Python template engines (Genshi, Mako, Jinja2) and standard Python string operations (StringIO, list concat). Shows Spitfire's performance with different optimization levels (-O1, -O2, -O3).
```text
# Python 3.10.9 (main, Dec 19 2022, 17:35:49) [GCC 12.2.0] on linux
$ python tests/benchmarks/render_benchmark.py
Running benchmarks 1000 times each...
Genshi tag builder 646.05 ms
Genshi template 226.59 ms
Genshi template + tag builder 448.92 ms
Mako Template 58.38 ms
Spitfire template 33.79 ms
Spitfire template -O1 20.66 ms
Spitfire template -O2 12.94 ms
Spitfire template -O3 8.19 ms
StringIO 7.95 ms
cStringIO 9.73 ms
list concat 7.32 ms
Jinja2 templates 15.84 ms
```
--------------------------------
### Basic For Loop in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Shows the fundamental syntax for iterating over a list or iterable in Spitfire using the '#for' and '#end for' directives. Each item in the list is assigned to a specified variable within the loop.
```spitfire
#for $item_name in $list_name
$item_name
#end for
```
--------------------------------
### Applying Filters (e.g., escape_html) in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Shows how to import and apply filters to content using the '#filter' directive. Filters are used to sanitize or modify output, such as preventing XSS attacks by escaping HTML.
```spitfire
#from spitfire.runtime.filters import escape_html
#filter escape_html
Bad input values such as
```
--------------------------------
### Setting Variables in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Shows how to define and assign values to variables directly within the Spitfire template using the '#set' directive. These variables can then be accessed like any other variable.
```spitfire
#set $number = 21*2
```
--------------------------------
### Adding Comments in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Illustrates the use of '##' for adding single-line comments in Spitfire templates. Comments are ignored during template rendering.
```spitfire
## this is a comment
## this too!
```
--------------------------------
### Passing Keyword Arguments (Incorrect - Whitespace) - Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
Demonstrates the incorrect syntax for passing keyword arguments with extra whitespace around the '=' sign, which will cause a parsing error in Spitfire.
```Spitfire Template
$function(keyword = 'value')
```
--------------------------------
### Passing Keyword Arguments (Correct) - Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
Shows the correct syntax for passing keyword arguments to a function or directive in Spitfire, requiring no whitespace around the '=' and no '$' on the keyword name.
```Spitfire Template
$function(keyword='value')
```
--------------------------------
### Nested For Loops in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Demonstrates how to structure nested loops in Spitfire templates. This allows iterating over inner collections for each item in an outer collection.
```spitfire
#for $outer_item_name in $outer_list_name
#for $inner_item_name in $inner_list_name
\... something
#end for
#end for
```
--------------------------------
### Error: Dollar Sign in Keyword Arguments - Text
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
The error message displayed by Spitfire when a '$' prefix is used on a keyword argument name.
```Text
keyword arg cant be complex expression: PlaceholderNode keyword on line 15:
> $function($keyword='value')
> ^
```
--------------------------------
### Accessing Variables in Spitfire
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Demonstrates how to access variables passed to the template using the '$variable_name' syntax. These variables are typically provided when rendering the template from Python code.
```spitfire
My name is $name
```
--------------------------------
### Rendering Search Results with Spitfire Template
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireSearchResultsExample.md
This snippet is an HTML template utilizing Spitfire syntax to dynamically display search results. It includes directives for importing filters, iterating over results, and embedding variables like query string, result counts, timings, and individual result properties.
```HTML
#from spitfire.runtime.filters import escape_html
#filter escape_html
search results - "$qstring"
Showing 1 - $num_results of $total_hits
(q:${query_time|format_string='%.3f'} t:$total_time)
#for $result in $results
$result.title
${result.snippet|raw}
$result.short_path
-
$result.size
-
$result.time_modified
#end for
```
--------------------------------
### Defining a Block in a Base Template (Spitfire)
Source: https://github.com/re-masashi/spitfire/blob/master/docs/language overview.rst
Illustrates how a base template (`base.spf`) defines a named block using '#block' and '#end' directives. Extending templates can override the content within these blocks, or the default content is used.
```spitfire
#block title
Hewwo ## default value
#end title
```
--------------------------------
### Error: Whitespace in Keyword Arguments - Text
Source: https://github.com/re-masashi/spitfire/blob/master/doc/SpitfireVsCheetah.md
The error message displayed by Spitfire when extra whitespace is found around the '=' in keyword arguments.
```Text
Trying to find CLOSE_PAREN on line 15:
> $function(keyword = 'value')
> ^
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.