Newscoop 4 Cookbook

Tips and tricks for the template novice

This chapter is a collection of snippets offering a quick overview of the inclusion of files, modification of strings, working with date and time, and managing line breaks. The following chapters will bring these snippets to life. For more modifications, you can consult the documentation.

Including files 

Templates and sub-templates are included using the template path starting inside the templates folder:

{{ include file="set_thejournal/_tpl/article-comments.tpl" }}

Date and time

For full date and time formatting options, see the template reference at the end of this manual.

Print current time (e.g. format: 25 April 2011, 16:20:45)

{{$|camp_date_format:"%e %M %Y, %H:%i:%S"}} 

Print date and time of publication of an article

{{ $gimme->article->publish_date|camp_date_format:"%e %M %Y, %H:%i:%S" }} 

Modifying strings

Stripping tags from WYSIWYG content

{{ $gimme->article->intro|strip_tags }}

Truncate string to specific length

{{ $gimme->article->intro|truncate:70 }}

Escaping HTML and stripping tags (e.g. for meta description)

{{ $gimme->article->intro|strip_tags|escape:'html' }}

Upper and lower case

{{ $gimme->article->name|upper }} {{ $gimme->article->name|lower }}


Replacing and chopping strings

Replacing parts of the string (example: whitespace into underscore)

{{ $gimme->article->name|replace:' ':'_' }}

You can also use a regular expression with regex_replace

{{ $gimme->browser->moz_data.2|regex_replace:"/\./":"-" }}

|regex_replace:"/\./":"-" replaces all dots with dashes. This can be useful in JavaScript elements. The following line uses regex_replace to replace fancy quotes like » « ” and so on with "


Trim a string (chop off whitespace at the beginning and end)

{{ $gimme->article->seo_title|trim }} 

Counting and statistics

Counting characters, words, sentences and paragraphs:

Headline "{{ $gimme->article->name }}" has  {{ $gimme->article->name|count_characters }} characters and {{ $gimme->article->name|count_words }} words. The full article has {{ $gimme->article->full_text|count_sentences }} sentences and {{ $gimme->article->full_text|count_paragraphs }} paragraphs. 

Useful conditions

Checking if variable is empty

{{ if $gimme->article->seo_title|strip_tags|trim !== "" }}

Using the if condition with these string modifiers makes sure that no HTML tags or whitespace is in the field. 

Avoiding line breaks

Anything within {{strip}} {{/strip}} tags is stripped of extra spaces or carriage returns at the beginnings and ends of the lines before they are displayed. Let's say you had a publication where the value of the seo_title field often contained leading spaces, sometimes editors even get HTML tags into the field:

                   <strong>Newscoop</strong> Rocks! 

The following example will output a single line starting and ending with quotes, but without leading spaces, like "STARTNewscoop Rocks!END". Any HTML code will be stripped out.

"{{ strip }}START {{ if $teststring|strip_tags|trim !== "" }}   {{ $teststring|strip_tags|trim }} {{ /if }}END {{ /strip }}"

If you want to keep whitespace between elements, use a workaround with:

 {{ textformat wrap=200 }}

This will turn everything into a single line with a space between each element, and applying a line break every 200 characters. Adjust the value of 200 to meet your needs. The example below will list various values in one line inside the body tag:

<body class="{{ textformat wrap=200 }} {{ $gimme->browser->browser_working }} {{ $gimme->browser->ua_type }} {{ $gimme->browser }} {{ /textformat }}" >