Template code Chunk output
{#example_1}
<p>
The Java code tab above demonstrates
how to write your own custom filter.
</p>

<h2>Right now around the world</h2>

<ul>
 <li> UTC: {$now|date(yyyy-MM-dd HH:mm Z z)}</li>
 <li> SF: {$now|date(yyyy-MM-dd HH:mm Z z, America/Los_Angeles)}</li>
 <li> Manila: {$now|date(yyyy-MM-dd HH:mm Z z, Asia/Manila)}</li>
 <li> Calcutta: {$now|date(yyyy-MM-dd HH:mm Z z, Asia/Calcutta)}</li>
 <li> Moscow: {$now|date(yyyy-MM-dd HH:mm Z z, Europe/Moscow)}</li>
 <li> Paris: {$now|date(yyyy-MM-dd HH:mm Z z, Europe/Paris)}</li>
 <li> Buenos Aires: {$now|date(yyyy-MM-dd HH:mm Z z, America/Buenos_Aires)}</li>
</ul>
{#}
package com.myapp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.x5.template.Chunk;
import com.x5.template.ChunkLocale;
import com.x5.template.filter.FilterArgs;
import com.x5.template.filter.ObjectFilter;

/**
 * Just call theme.registerFilter(new com.myapp.DateTimeFilter())
 * before rendering to use this handy |date filter.
 */
public class DateTimeFilter extends ObjectFilter
{
    private static final String DEFAULT_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

    public String getFilterName()
    {
        return "date";
    }

    public Object transformObject(Chunk chunk, Object obj, FilterArgs arg)
    {
        if (!(obj instanceof Date)) {
            return "ERR: Not a java.util.Date";
        }

        Date date = (Date)obj;

        String format = null;
        String timezone = "UTC";

        String[] args = arg.getFilterArgs(chunk);
        if (args != null) {
            if (args.length == 1) {
                format = args[0];
            } else if (args.length > 1) {
                format = args[0];
                timezone = args[1].trim();
            }
        }

        if (format == null || format.trim().length() == 0) format = DEFAULT_FORMAT;

        try {
            ChunkLocale chunkLocale = chunk.getLocale();
            Locale javaLocale = chunkLocale == null ? null : chunkLocale.getJavaLocale();
            SimpleDateFormat formatter = javaLocale == null
                ? new SimpleDateFormat(format)
                : new SimpleDateFormat(format, javaLocale);
            formatter.setTimeZone(TimeZone.getTimeZone(timezone));
            return formatter.format(date);
        } catch (IllegalArgumentException e) {
            return e.getMessage();
        }
    }
}
<p>
The Java code tab above demonstrates
how to write your own custom filter.
</p>

<h2>Right now around the world</h2>

<ul>
 <li> UTC: 2024-10-04 17:20 +0000 UTC</li>
 <li> SF: 2024-10-04 10:20 -0700 PDT</li>
 <li> Manila: 2024-10-05 01:20 +0800 PST</li>
 <li> Calcutta: 2024-10-04 22:50 +0530 IST</li>
 <li> Moscow: 2024-10-04 20:20 +0300 MSK</li>
 <li> Paris: 2024-10-04 19:20 +0200 CEST</li>
 <li> Buenos Aires: 2024-10-04 14:20 -0300 ART</li>
</ul>

The Java code tab above demonstrates how to write your own custom filter.

Right now around the world

  • UTC: 2024-10-04 17:20 +0000 UTC
  • SF: 2024-10-04 10:20 -0700 PDT
  • Manila: 2024-10-05 01:20 +0800 PST
  • Calcutta: 2024-10-04 22:50 +0530 IST
  • Moscow: 2024-10-04 20:20 +0300 MSK
  • Paris: 2024-10-04 19:20 +0200 CEST
  • Buenos Aires: 2024-10-04 14:20 -0300 ART
Template code Chunk output
{#example_2}
<p>
The Java code tab above demonstrates how to contribute
and register your own custom filter.
</p>

<p>
Visit the _[Full Docs] link in the top nav to learn more
about writing your own filter.
</p>

<pre>
*{$value_with_spaces|ltrim}*
</pre>
{#}
import com.x5.template.Theme;
import com.x5.template.Chunk;
import com.x5.template.filters.BasicFilter;
import com.x5.template.filters.ChunkFilter;
import com.x5.template.filters.FilterArgs;

//...

Theme theme = new Theme("examples");

// add our custom filter to the theme
theme.registerFilter(new LeftTrimFilter());

// Fetch template from this file: themes/examples/contrib.chtml
// Inside that file there is a template "snippet" named #example_1
Chunk html = theme.makeChunk("contrib#example_1");
html.set("value_with_spaces", "   hello   ");

html.render(out);

//...

public class LeftTrimFilter extends BasicFilter implements ChunkFilter
{

    /**
     * LeftTrimFilter is a simple, no-arg filter: {$tag|ltrim}.
     *
     * Visit the full documentation to learn how filter args get parsed
     * and passed into transformText(...)
     *
     */
    public String transformText(Chunk chunk, String text, FilterArgs args)
    {
        if (text == null) return null;
        
        int i=0;
        while (i < text.length() && Character.isWhitespace(text.charAt(i))) i++;

        return (i == 0) ? text : text.substring(i);
    }

    public String getFilterName()
    {
        return "ltrim";
    }
    
}
<p>
The Java code tab above demonstrates how to contribute
and register your own custom filter.
</p>

<p>
Visit the Full Docs link in the top nav to learn more
about writing your own filter.
</p>

<pre>
*hello   *
</pre>

The Java code tab above demonstrates how to contribute and register your own custom filter.

Visit the Full Docs link in the top nav to learn more about writing your own filter.

*hello   *
Template code Chunk output
{#example_3}
{!--
  -- Starting in 2.1, you can use a template to define your own filter.
  --
  --   {% $tag|filter(#x_template) %}
  --
  -- This filter will bind the value of $tag to {$x} and execute the
  -- specified template in place, right along the filter chain.
  --
  -- Scroll down to see the {#rtrim_bullet} filter definition snippet.
  --
  -- Important: If the input is null/undefined, the template will still
  -- be executed.  Make sure you account for the possibility that $x is
  -- null/undefined in your #x_template definition.
  --
  --}

<ul>
 {% $text|filter(#rtrim_bullet) %}
</ul>

<p>Exec can do this too, but you have to bind x explictly:</p>

<ul>
{% exec #rtrim_bullet %}
 {$x=}{$text}{=}
{% endexec %}
</ul>
{#}
Theme theme = new Theme("examples");

// Fetch template from this file: themes/examples/contrib.chtml
// Inside that file there is a template "snippet" named #example_2
Chunk html = theme.makeChunk("contrib#example_2");

html.set("text", "Remember to floss.      ");

html.render( out );
<ul>
 <li> Remember to floss.</li>
</ul>

<p>Exec can do this too, but you have to bind x explictly:</p>

<ul>
 <li> Remember to floss.</li>
</ul>

  • Remember to floss.

Exec can do this too, but you have to bind x explictly:

  • Remember to floss.
Template code
{#rtrim_bullet}<li> {% $x:|s/\s*$// %}</li>{#}