11

Click here to load reader

ZF2 View Helpers

Embed Size (px)

Citation preview

Page 1: ZF2 View Helpers

ZF2 View Helpers

by Bo Andersen

Page 2: ZF2 View Helpers

What are view helpers?

Helpers that perform certain operations, which can be used within views

View helpers are particularly useful for operations that need to be performed

repeatedly

E.g. formatting dates, generating links, stripping output, pagination, menus, etc.

ZF2 ships with a number of useful view helpers for common use cases

Accessible within views like this: $this->someHelper(‘parameter’)

As the above example suggests, it is possible to write your own view helpers

Page 3: ZF2 View Helpers

Example: Zend\View\Helper\EscapeHtml

$output = ‘<script

type=”text/javascript”>alert(“XSS!”);</script>’;

echo $this->escapeHtml($output); // Good

echo $output; // Bad!

Page 4: ZF2 View Helpers

Example: Zend\i18n\View\Helper\Translate

echo $this->translate(‘This text will be translated’);

Even if you have currently not translated your application (and even if you have no

plans of doing so), always using the translate view helper can save you quite

some time later on!

Page 5: ZF2 View Helpers

Example: Zend\i18n\View\Helper\DateFormat

echo $this->dateFormat(

new \DateTime(),

IntlDateFormatter::MEDIUM, // Date

IntlDateFormatter::MEDIUM, // Time

‘en_US’

);

Output: "Oct 9, 2015 5:19:37 PM"

Page 6: ZF2 View Helpers

Example: Zend\i18n\View\Helper\DateFormatecho $this->dateFormat(

new \DateTime(),

IntlDateFormatter::LONG, // Date

IntlDateFormatter::NONE, // Time

‘en_US’

);

Output: "October 9, 2015"

Page 7: ZF2 View Helpers

Example: Zend\i18n\View\Helper\DateFormat

echo $this->dateFormat(

new \DateTime(),

IntlDateFormatter::NONE, // Date

IntlDateFormatter::SHORT, // Time

‘en_US’

);

Output: "5:19 PM"

Page 8: ZF2 View Helpers

Other view helpers

Zend\View\Helper\HeadLink

Add links for output in the <head> section within a layout, e.g. links to stylesheets

Zend\View\Helper\HeadScript

Add inline scripts or links to JavaScript files for output within a layout

Zend\View\Helper\HeadMeta

Add meta entries for a page for output within a layout (e.g. meta description)

Zend\View\Helper\FlashMessenger

Render “flash messages” stored within a session, e.g. success or error messages

Page 9: ZF2 View Helpers

Even more view helpersZend\View\Helper\Url

Generate URLs for configured routes within the application

Avoid hardcoding URLs and easily change them by simply updating the route configuration

Zend\View\Helper\Identity

Access information about the currently logged in user (or check if the current user is logged in)

Zend\View\Helper\Form*

View helpers that help rendering forms and other form-related operations

Zend\i18n\View\Helper\NumberFormat

Formats numbers based on the provided locale

For a complete list, please visit http://tinyurl.com/zf2-view-helpers

Page 10: ZF2 View Helpers

Writing your own view helper (optional)

This is not necessary to know at this point

If you really want to get ahead, then read my blog post on how to create a

custom view helper

http://tinyurl.com/zf2-custom-view-helper

Page 11: ZF2 View Helpers

that’s allThis presentation is part of my Zend Framework 2 online course.

Get 50% discount by navigating to the below URL!

https://www.udemy.com/zend-framework-2-from-beginner-to-

professional/?couponCode=SS50