### Install Gonja v2 Source: https://github.com/nikolalohinski/gonja/blob/master/README.md Install or update the Gonja library using go get. This command fetches the latest version of the v2 package. ```bash go get github.com/nikolalohinski/gonja/v2 ``` -------------------------------- ### String Upper Method Example Source: https://github.com/nikolalohinski/gonja/blob/master/docs/methods.md Demonstrates the usage of the .upper() method on a string literal. ```gonja {{ "hello".upper() }} ``` -------------------------------- ### Iterating Over a List with For Loop Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Loop over items in a sequence, such as a list, to render content for each item. This example demonstrates iterating over a list of user objects. ```html ``` -------------------------------- ### Sum sequence with optional attribute Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Returns the sum of a sequence of numbers plus a starting value (defaults to 0). If the sequence is empty, it returns the start value. It can also sum specific attributes of objects in the sequence. ```jinja Total: {{ items | sum(attribute='price') }} ``` -------------------------------- ### In Test Examples Source: https://github.com/nikolalohinski/gonja/blob/master/docs/tests.md Illustrates the 'in' test for checking membership within strings, lists, and dictionaries. This is versatile for validating if a value exists within a collection or substring. ```html {{ "foo" is in "foobar" }} // True {{ 4 is in [1, 2, 3] }} // False {{ "key" is in {"key": "value"} }} // True {{ "value" is in {"key": "value"} }} // False ``` -------------------------------- ### DivisibleBy Test Example Source: https://github.com/nikolalohinski/gonja/blob/master/docs/tests.md Checks if a number is divisible by another number using the 'divisibleby' test. Useful for calculations or conditional display based on divisibility. ```html {% if 2048 is divisibleby 512 %} Yes it is modulo 4 {% endif %} ``` -------------------------------- ### Basic Template Execution in Go Source: https://github.com/nikolalohinski/gonja/blob/master/README.md Demonstrates how to load a template from a string, execute it with data, and write the output to standard output. Ensure proper error handling for template parsing and execution. ```golang package main import ( "os" "fmt" "github.com/nikolalohinski/gonja/v2" "github.com/nikolalohinski/gonja/v2/exec" ) func main() { template, err := gonja.FromString("Hello {{ name | capitalize }}!") if err != nil { panic(err) } data := exec.NewContext(map[string]interface{}{ "name": "bob", }) if err = template.Execute(os.Stdout, data); err != nil { // Prints: Hello Bob! panic(err) } } ``` -------------------------------- ### Calling a Macro for Input Fields Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Demonstrates calling the 'input' macro to generate username and password fields. Macros are called like functions. ```html

{{ input('username') }}

{{ input('password', type='password') }}

``` -------------------------------- ### Formatting Strings with printf-style Arguments Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Shows how to use the `format` filter to apply values to a printf-style format string. This is useful for constructing dynamic strings with placeholders. ```html {{ "%s, %s!"|format(greeting, name) }} Hello, World! ``` -------------------------------- ### Conditional Rendering with Tests Source: https://github.com/nikolalohinski/gonja/blob/master/docs/tests.md Demonstrates how to use various tests like 'string', 'sequence' for conditional logic in templates. This is useful for displaying different content based on variable types. ```html {% if variable is string %} This was a string: {{ variable }} {% elif variable is sequence %} This was a list: {{ variable | join(",") }} {% end if%} ``` -------------------------------- ### Creating a Namespace Object Source: https://github.com/nikolalohinski/gonja/blob/master/docs/global_functions.md Initializes a namespace object and assigns properties to it within the template scope. ```html {% set ns = namespace() %} {% set ns.foo = 'bar' %} ``` -------------------------------- ### Batching Items for Table Display Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Illustrates how to use the `batch` filter to group items into sublists of a specified size, often used for rendering data in tables. The `fille_with` argument can be used to pad incomplete batches. ```html {%- for row in items|batch(3, ' ') %} {%- for column in row %} {%- endfor %} {%- endfor %}
{{ column }}
``` -------------------------------- ### Run Unit Tests Source: https://github.com/nikolalohinski/gonja/blob/master/README.md Execute all unit tests for the Gonja project using the ginkgo test runner. The -p flag enables parallel test execution. ```bash ginkgo run -p ./... ``` -------------------------------- ### Map users to their usernames using map and join filters Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Applies a filter to each item in a sequence, useful for extracting specific attributes. The result is then joined into a comma-separated string. ```html Users on this page: {{ users | map(attribute='username') | join(', ') }} ``` -------------------------------- ### Including Other Templates Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Use the include control structure to embed the rendered content of another template file into the current template. This promotes modularity and reusability. ```jinja {% include 'header.html' %} Body {% include 'footer.html' %} ``` -------------------------------- ### Conditional Logic with If/Elif/Else Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Use the if control structure for conditional rendering based on template variables. It supports elif and else for multiple conditions. ```jinja {% if kenny.sick %} Kenny is sick. {% elif kenny.dead %} You killed Kenny! You bastard!!! {% else %} Kenny looks okay --- so far {% endif %} ``` -------------------------------- ### Chaining Filters: Split, ToJSON, and Integer Conversion Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Demonstrates chaining multiple filters to process string and numeric data. The `split` filter breaks a string into a list, `tojson` converts it to a JSON string, and `int` converts a string to an integer. ```html {{ "a,b,c" | split(",") | tojson }} {% set number = "123" | int %} ``` -------------------------------- ### Iterating Over Dictionary Values Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Shows how to iterate over the values of a dictionary using the `values` filter. This is useful when the order of dictionary keys is not important for the iteration. ```html {% for x in {"first": 1, "second": 2} | values %} {{- x }} {% endfor %} ``` -------------------------------- ### Importing Specific Macros Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Imports specific macros ('input' and 'textarea') from 'forms.html' into the current namespace with optional aliasing. Imported templates have access to the active context. ```html {% from 'forms.html' import input as input_field, textarea %}
Username
{{ input_field('username') }}
Password
{{ input_field('password', type='password') }}

{{ textarea('comment') }}

``` -------------------------------- ### Importing a Macro Module Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Imports all macros and variables from a 'forms.html' template into a 'forms' namespace. This allows accessing imported elements using dot notation. ```html {% import 'forms.html' as forms %}
Username
{{ forms.input('username') }}
Password
{{ forms.input('password', type='password') }}

{{ forms.textarea('comment') }}

``` -------------------------------- ### Providing a Default Value for Undefined Variables Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Demonstrates the `default` (or `d`) filter, which provides a fallback value when a variable is undefined. This prevents errors and ensures a default output. ```html {{ my_variable | d('my_variable is not defined') }} ``` -------------------------------- ### Accessing Gonja Version Source: https://github.com/nikolalohinski/gonja/blob/master/docs/global_variables.md Access the version of the Gonja library using the global 'gonja' object. This is available by default in the root scope. ```html {{ gonja.version }} ``` -------------------------------- ### Reject users by email using rejectattr filter with a test Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Filters a sequence of objects by rejecting those where a specified attribute passes a given test. Here, it rejects users whose email attribute is none. ```html {{ users | rejectattr("email", "none") }} ``` -------------------------------- ### Filter sequence by test (reject) Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Filters a sequence of objects by applying a test and rejecting those where the test succeeds. If no test is specified, objects are evaluated as booleans. ```jinja {{ numbers|reject("odd") }} ``` -------------------------------- ### Base HTML Template with Blocks Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Defines a base HTML structure with placeholders for content that child templates can override. Used for creating reusable page layouts. ```html {% block head %} {% block title %}{% endblock %} - My Webpage {% endblock %}
{% block content %}{% endblock %}
``` -------------------------------- ### Looping with Range Source: https://github.com/nikolalohinski/gonja/blob/master/docs/global_functions.md Iterates a specified number of times using the range function. ```html {% for index in range(10) %} counting {{ index + 1 }} {% endfor %} ``` -------------------------------- ### Alternating List Item Classes with Cycler Source: https://github.com/nikolalohinski/gonja/blob/master/docs/global_functions.md Cycles through 'odd' and 'even' classes for list items using the cycler function. ```html {% set row_class = cycler("odd", "even") %} ``` -------------------------------- ### Iterating Over a Dictionary with For Loop Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Iterate over key-value pairs in a dictionary using the for control structure. This allows access to both the keys and their corresponding values. ```html ``` -------------------------------- ### Using Namespace in a Loop Source: https://github.com/nikolalohinski/gonja/blob/master/docs/global_functions.md Carries a boolean value (found) from within a loop to an outer scope using a namespace object. ```html {% set ns = namespace(found=false) %} {% for item in items %} {% if item.check_something() %} {% set ns.found = true %} {% endif %} * {{ item.title }} {% endfor %} Found item having something: {{ ns.found }} ``` -------------------------------- ### Variable Assignment with Set Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Assign values to variables within Jinja templates using the set control structure. This is useful for preparing data for later use or for complex string manipulations. ```jinja {% set groceries = ["eggs", "milk", "vegetables"] %} {% set csv = groceries | join(",") } ``` -------------------------------- ### Filter sequence by test (select) Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Filters a sequence of objects by applying a test to each object, selecting only those where the test succeeds. If no test is specified, objects are evaluated as booleans. ```jinja {{ numbers | select("odd") }} ``` ```jinja {{ numbers | select("odd") }} ``` ```jinja {{ numbers | select("divisibleby", 3) }} ``` ```jinja {{ numbers | select("lessthan", 42) }} ``` ```jinja {{ strings | select("equalto", "mystring") }} ``` -------------------------------- ### Conditional Section Joining with Joiner Source: https://github.com/nikolalohinski/gonja/blob/master/docs/global_functions.md Joins multiple conditional sections with a pipe character, ensuring the pipe only appears between sections. ```html {% set pipe = joiner("|") %} {% if categories %} {{ pipe() }} Categories: {{ categories|join(", ") }} {% endif %} {% if author %} {{ pipe() }} Author: {{ author() }} {% endif %} {% if can_edit %} {{ pipe() }} EditCreatePipelineService {% endif %} ``` -------------------------------- ### Defining a Reusable Input Macro Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Defines a macro named 'input' for generating HTML input fields. Macros promote code reusability and follow the DRY principle. ```html {% macro input(name, value='', type='text', size=20) -%} {%- endmacro %} ``` -------------------------------- ### Creating Scopes with With Statement Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md The with control structure creates a new, isolated scope for variables. Variables defined within this scope are not accessible outside of it. ```jinja {% with foo = 42 %} {{ foo }} {% endwith %} ``` ```jinja {% with %} {% set foo = 42 %} {{ foo }} {% endwith %} ``` -------------------------------- ### Reject users by active status using rejectattr filter Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Filters a sequence of objects by rejecting those where a specified attribute evaluates to true. The attribute's value is evaluated as a boolean by default. ```html {{ users | rejectattr("is_active") }} ``` -------------------------------- ### Slice an iterator into columns Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Slices an iterator and returns a list of lists, useful for creating columns. A second argument can be used to fill missing values on the last iteration. ```jinja
{%- for column in items|slice(3) %} {%- endfor %}
``` -------------------------------- ### Child HTML Template Extending Base Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Extends a base template and overrides specific blocks to customize content. The `{% extends %}` tag must be the first in the template. ```html {% extends "base.html" %} {% block title %}Index{% endblock %} {% block head %} {{ super() }} {% endblock %} {% block content %}

Index

Welcome to my awesome homepage.

{% endblock %} ``` -------------------------------- ### Filter sequence by attribute test (selectattr) Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Filters a sequence of objects by applying a test to a specific attribute. Only objects where the test succeeds are selected. If no test is specified, the attribute's value is evaluated as a boolean. ```jinja {{ users | selectattr("is_active") }} ``` ```jinja {{ users | selectattr("email", "none") }} ``` -------------------------------- ### Group users by city using groupby filter Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Groups a sequence of objects by a specified attribute. Useful for rendering lists grouped by common values. ```html ``` -------------------------------- ### Applying Filters to Template Sections Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md The filter control structure applies Jinja filters to an entire block of template content. This is useful for bulk transformations like converting text to uppercase. ```jinja {% filter upper %} This text becomes uppercase {% endfilter %} ``` -------------------------------- ### Disabling Jinja Parsing with Raw Source: https://github.com/nikolalohinski/gonja/blob/master/docs/control_structures.md Use the raw control structure to prevent Jinja from parsing specific sections of template content. This is essential when including code or text that should be displayed literally. ```html {% raw %} {% endraw %} ``` -------------------------------- ### Unique Items from List Source: https://github.com/nikolalohinski/gonja/blob/master/docs/filters.md Returns a list containing only unique items from the input iterable. By default, it is case-insensitive. An attribute can be specified to filter based on a specific object attribute. ```jinja {{ ['foo', 'bar', 'foobar', 'FooBar'] | unique }} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.