Template code Chunk output
{#example_1}
<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;

//...

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, String[] 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_2}
{!--
  -- 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.
  --
  -- 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}{=}
{/exec}
</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>{#}