Template resources

Template

Using resources in template

This tutorial gives a quick example of how to get and use different kind of information in template -code.

Getting data

Using preset data

Usually you have the data already set in $kansio and $sivu -variables.

First page is: {$sivu->getTitle()}
{foreach $kansio as $sivu}
  {$sivu->getTitle()}
{/foreach}

Getting pages from table

Load multiple pages:

{$Customers = $this->get('pages','customer')->addWhere('region','Pirkanmaa')}

Load a sigle page:

{$Customer = $this->get('page','customer','1001')}
{$Customer = $this->get('page','customer','customer_number=1234')}

Links

You can create a link using link -resource. For url structure, see Routing names.

<a href='{$this->get('link','table/customer/add')}'>Add customer</a>

Link to certain Sivu

If you have a Page -object, you can create a link to edit that page with Page::getLink() -method.

<a href='{$Page->getLink()}'>Edit this page</a>

Or set link text as a parameter:

{$Page->getLink('Edit this page')}

Dates

You can create a date using Date -resource

Today is: {$this->get('date')}
Next friday is: {$this->get('date','Next friday')}

Modifying the date.

Week from now is: {$this->get('date')->addWeek()}
7 days from now is: {$this->get('date')->addDays(7)}

Getting date information:

Today is: {$this->get('date')->getWeekday()}
Now is the year: {$this->get('date')->getYear()}

Comparing date values:

{if $this->get('date')->addDays(2)->isBefore('Next friday')}
    Day after tomorrow is before next friday.
{/if}
{if $this->get('date')->addWeek()->isSame('Today','month')}
    Week from now is same month as today.
{/if}

Getting range of dates:

{foreach $this->get('date')->getWeekDates() as $date}
    {$date->getDay()}.{$date->getMonth()}. {$date->getWeekday()}
{/foreach}

Date fieldtype

You reference directly to the Date object methods in Date fieldtype. Under the hood, call will bridge to Field::getDateReference() method.

Check if date has gone:

{if $Order->delivery_date->isBefore('Today')} Delivered! {/if}

Manipulate value in field:

{$Order->delivery_date->addWeek()}

Get a new Date object with same value as Field. This will NOT modify the value in field:

{$Order->delivery_date->getDate()->addWeek()}

Sending Email

Emails can be sent using mail -resource. If sender is not defined, active users mail address will be used.

{$this->get('mail')
   ->setFrom('mymail@mail.com')
   ->setTo('yourmail@mail.com')
   ->setTitle('Sending mail')
   ->setBody('Just to test')
   ->send()}

Email action

Using predefind Email action:

{$this->get('action','order_confirmation')->send($Page)}

Get action as eMail object and edit it before sending:

{$this->get('action','order_confirmation')
  ->getMail($Page)
    ->setFrom('mymail@mail.com')
    ->setTo('yourmail@mail.com')
    ->setSubject('With this title')
    ->setMessage('And with this content')
    ->send()}

Or with Page::send() -method:

{$Page->send('order_confirmation')}

Both ways can take additional parameteres to override values defined by action:

{$Page->send('order_confirmation',Array(
   from    => 'mymail@mail.com',
   to      => 'yourmail@mail.com',
   subject => 'With this title',
   message => 'And with this content'
   ))}

Sending SMS

Emails can be sent using sms -resource.

{$this->get('sms')
  ->to('+358408255925')
  ->message('SMS message')
  ->send()}

SMS action

Using predefind SMS action:

{$this->get('action','notification_sms')->send($Page)}

Get action as eSms object and edit it before sending:

{$this->get('action','notification_sms')
  ->getSms($Page)
    ->setSender('Sender name ot number')
    ->setNumber('+358408255925')
    ->setMessage('Send this text')
    ->send()}

Or with Page::send() -method:

{$Page->send('notification_sms')}

Both ways can take additional parameteres to override values defined by action:

{$Page->send('order_confirmation',Array(
   sender    => 'Sender name ot number',
   number    => '+358408255925',
   message   => 'Send this text'
   ))}

User Feedback

Feedback messages can be created using Feedback -method.

{$this->get('feedback','Good work, everything OK.')}
{$this->get('feedback','Something went wrong.'         ,'error')}
{$this->get('feedback','I want to ensure you see me!'  ,'notification')}

Getting user information

Account

Account information can be get using account -resource.

Account name: {$this->get('account')->getName()}

Profile

Profile information can be get using profile -resource.

Profile id: {$this->get('profile')->getId()}
{$this->get('profile')->is(1001)} Only you can see this {/if}

User

User information can be get using user -resource.

User name: {$this->get('user')->getName()}