Tag (View Helpers)


Tag - 图1

Overview

Writing and maintaining HTML markup can quickly become a tedious task because of the naming conventions and numerous attributes that have to be taken into consideration. Phalcon deals with this complexity by offering the Phalcon\Tag component which in turn offers view helpers to generate HTML markup.

This component can be used in a plain HTML+PHP view or in a Volt template.

This offers the same functionality as Phalcon\Html\TagFactory. In future versions, this component will be replaced by the TagFactory one. The reason for both components is to offer as much time as possible to developers to adapt their code, since HTML generation touches a lot of areas of the application, the view in particular.

DocType

You can set the doctype for your page using setDocType(). The method accepts one of the available constants, generating the necessary <doctype> HTML. The method returns the Tag component and thus the call can be chained.

  • HTML32
  • HTML401_STRICT
  • HTML401_TRANSITIONAL
  • HTML401_FRAMESET
  • HTML5
  • XHTML10_STRICT
  • XHTML10_TRANSITIONAL
  • XHTML10_FRAMESET
  • XHTML11
  • XHTML20
  • XHTML5
  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setDocType(Tag::XHTML20);
  4. echo Tag::getDocType();

The above example will produce:

  1. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 2.0//EN'
  2. 'http://www.w3.org/MarkUp/DTD/xhtml2.dtd'>

The default value is HTML5 which generates:

  1. <!DOCTYPE html>

You can output the doctype using getDocType() in your views:

  1. <?php echo $this->tag->getDocType(); ?>

or in Volt:

  1. {{ get_doctype() }}

Title

Phalcon\Tag offers methods to set the tag of the resulting page or HTML sent to the user. There are several methods available:

appendTitle()

Appends text to the current title. The method accepts either a string or an array.

If a string is supplied, it will be added to the internal collection holding the append title text. If however you supply an array the internal collection will be replaced.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setTitle('Phalcon');
  4. echo Tag::getTitle(); // 'Phalcon'
  5. Tag::appendTitle(' Framework');
  6. Tag::appendTitle(' Rocks');
  7. echo Tag::getTitle(); // 'Phalcon Framework Rocks'
  8. Tag::appendTitle('Will be replaced');
  9. Tag::appendTitle(
  10. [
  11. ' Framework',
  12. ' Rocks',
  13. ]
  14. );
  15. echo Tag::getTitle(); // 'Phalcon Framework Rocks'

friendlyTitle()

Converts text to URL-friendly strings. It accepts the following parameters:

  • text - The text to be processed
  • parameters - Array of parameters to generate the friendly titleThe parameters can be:

  • lowercase - bool Whether to convert everything to lowercase or not

  • separator - string - The separator. Defaults to -
  • replace - array - Key value array to replace characters with others. This uses [str_replace][str_replace] internally for this replacement
  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::friendlyTitle('Phalcon Framework');
  4. // 'Phalcon-Framework';
  5. echo Tag::friendlyTitle(
  6. 'Phalcon Framework',
  7. [
  8. 'separator' => '_',
  9. 'lowercase' => true,
  10. ]
  11. ); // 'phalcon_framework
  12. echo Tag::friendlyTitle(
  13. 'Phalcon Framework',
  14. [
  15. 'separator' => '_',
  16. 'lowercase' => true,
  17. 'replace' => [
  18. 'a' => 'x',
  19. 'e' => 'x',
  20. 'o' => 'x',
  21. ]
  22. ]
  23. ); // 'phxlcxn_frxmxwxrk

getTitle()

Returns the current title. The title is automatically escaped. The method accepts two parameters:

  • prepend - bool Whether to output any text set with prependTitle()
  • append - bool Whether to output any text set with appendTitle()Both parameters are true by default.
  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setTitleSeparator(' ');
  4. Tag::prependTitle('Hello');
  5. Tag::setTitle('World');
  6. Tag::appendTitle('from Phalcon');
  7. echo Tag::getTitle(); // 'Hello World from Phalcon';
  8. echo Tag::getTitle(false); // 'World from Phalcon';
  9. echo Tag::getTitle(true, false); // 'Hello World';
  10. echo Tag::getTitle(false, false); // 'World';

getTitleSeparator()

Returns the current title separator. The default value is an empty string.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::getTitleSeparator(); // ''

prependTitle()

Prepends text to the current title. The method accepts either a string or an array.

If a string is supplied, it will be added to the internal collection holding the prepend title text. If however you supply an array the internal collection will be replaced.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setTitle('Rocks');
  4. echo Tag::getTitle(); // 'Phalcon'
  5. Tag::prependTitle('Phalcon ');
  6. Tag::prependTitle('Framework ');
  7. echo Tag::getTitle(); // 'Phalcon Framework Rocks'
  8. Tag::prependTitle('Will be replaced');
  9. Tag::prependTitle(
  10. [
  11. 'Phalcon ',
  12. 'Framework ',
  13. ]
  14. );
  15. echo Tag::getTitle(); // 'Phalcon Framework Rocks'

renderTitle()

Returns the current title wrapped in <title> tags. The title is automatically escaped. The method accepts two parameters:

  • prepend - bool Whether to output any text set with prependTitle()
  • append - bool Whether to output any text set with appendTitle()Both parameters are true by default.
  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setTitleSeparator(' ');
  4. Tag::prependTitle('Hello');
  5. Tag::setTitle('World');
  6. Tag::appendTitle('from Phalcon');
  7. echo Tag::renderTitle();
  8. // '<title>Hello World from Phalcon</title>';
  9. echo Tag::renderTitle(false);
  10. // '<title>World from Phalcon</title>';
  11. echo Tag::renderTitle(true, false);
  12. // '<title>Hello World</title>';
  13. echo Tag::renderTitle(false, false);
  14. // '<title>World</title>';

setTitle()

Sets the title text.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setTitle('World');

setTitleSeparator()

Set the separator of the title.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setTitleSeparator(' ');

Input

checkField()

Builds a HTML input[type='check'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::checkField(
  4. [
  5. 'terms',
  6. 'value' => 'Y',
  7. ]
  8. );
  9. // <input type='checkbox' id='terms' name='terms' value='Y' />

HTML syntax:

  1. <?php echo $this->tag->checkField(
  2. [
  3. 'terms',
  4. 'value' => 'Y',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ check_field('terms', 'value': 'Y') }}

colorField()

Builds a HTML input[type='color'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::colorField(
  4. [
  5. 'background',
  6. 'class' => 'myclass',
  7. ]
  8. );
  9. // <input type='color' id='background' name='background' class='myclass' />

HTML syntax:

  1. <?php echo $this->tag->colorField(
  2. [
  3. 'background',
  4. 'class' => 'myclass',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ color_field('background', 'class': 'myclass') }}

dateField()

Builds a HTML input[type='date'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::dateField(
  4. [
  5. 'born',
  6. 'value' => '1980-01-01',
  7. ]
  8. );
  9. // <input type='date' id='born' name='born' value='1980-01-01' />

HTML syntax:

  1. <?php echo $this->tag->dateField(
  2. [
  3. 'born',
  4. 'value' => '1980-01-01',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ date_field('born', 'value': '1980-01-01') }}

dateTimeField()

Builds a HTML input[type='datetime'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::dateTimeField(
  4. [
  5. 'born',
  6. 'value' => '1980-01-01 01:02:03',
  7. ]
  8. );
  9. // <input type='datetime' id='born' name='born'
  10. // value='1980-01-01 01:02:03' />

HTML syntax:

  1. <?php echo $this->tag->dateTimeField(
  2. [
  3. 'born',
  4. 'value' => '1980-01-01 01:02:03',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ date_time_field('born', 'value': '1980-01-01') }}

dateTimeLocalField()

Builds a HTML input[type='datetime-local'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::dateTimeLocalField(
  4. [
  5. 'born',
  6. 'value' => '1980-01-01 01:02:03',
  7. ]
  8. );
  9. // <input type='datetime-local' id='born' name='born'
  10. // value='1980-01-01 01:02:03' />

HTML syntax:

  1. <?php echo $this->tag->dateTimeLocalField(
  2. [
  3. 'born',
  4. 'value' => '1980-01-01 01:02:03',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ date_time_local_field('born', 'value': '1980-01-01 01:02:03') }}

fileField()

Builds a HTML input[type='file'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::fileField(
  4. [
  5. 'document',
  6. 'class' => 'input',
  7. ]
  8. );
  9. // <input type='file' id='document' name='document' class='input' />

HTML syntax:

  1. <?php echo $this->tag->fileField(
  2. [
  3. 'document',
  4. 'class' => 'input',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ file_field('document', 'class': 'input') }}

hiddenField()

Builds a HTML input[type='hidden'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::hiddenField(
  4. [
  5. 'id',
  6. 'value' => '1234',
  7. ]
  8. );
  9. // <input type='hidden' id='id' name='id' value='1234' />

HTML syntax:

  1. <?php echo $this->tag->hiddenField(
  2. [
  3. 'id',
  4. 'value' => '1234',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ hidden_field('id', 'value': '1234') }}

imageInput()

Builds a HTML input[type='image'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::imageInput(
  4. [
  5. 'src' => '/img/button.png',
  6. ]
  7. );
  8. // <input type='image' src='/img/button.png' />

HTML syntax:

  1. <?php echo $this->tag->imageInput(
  2. [
  3. 'src' => '/img/button.png',
  4. ]
  5. ); ?>

Volt syntax:

  1. {{ image_input('src': '/img/button.png') }}

monthField()

Builds a HTML input[type='month'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::monthField(
  4. [
  5. 'month',
  6. 'value' => '04',
  7. ]
  8. );
  9. // <input type='month' id='month' name='month' value='04' />

HTML syntax:

  1. <?php echo $this->tag->monthField(
  2. [
  3. 'month',
  4. 'value' => '04',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ month_field('month', 'value': '04') }}

numericField()

Builds a HTML input[type='number'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::numericField(
  4. [
  5. 'price',
  6. 'min' => '1',
  7. 'max' => '5',
  8. ]
  9. );
  10. // <input type='number' id='price' name='price' min='1' max='5' />

HTML syntax:

  1. <?php echo $this->tag->numericField(
  2. [
  3. 'price',
  4. 'min' => '1',
  5. 'max' => '5',
  6. ]
  7. ); ?>

Volt syntax:

  1. {{ numeric_field('price', 'min': '1', 'max': '5') }}

radioField()

Builds a HTML input[type='radio'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::radioField(
  4. [
  5. 'gender',
  6. 'value' => 'Male',
  7. ]
  8. );
  9. // <input type='radio' id='gender' name='gender' value='Male' />

HTML syntax:

  1. <?php echo $this->tag->radioField(
  2. [
  3. 'gender',
  4. 'value' => 'Male',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ radio_field('gender', 'value': 'Male') }}

rangeField()

Builds a HTML input[type='range'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::rangeField(
  4. [
  5. 'points',
  6. 'min' => '0',
  7. 'max' => '10',
  8. ]
  9. );
  10. // <input type='range' id='points' name='points' min='0' max='10' />

HTML syntax:

  1. <?php echo $this->tag->rangeField(
  2. [
  3. 'points',
  4. 'min' => '0',
  5. 'max' => '10',
  6. ]
  7. ); ?>

Volt syntax:

  1. {{ range_field('points', 'min': '0', 'max': '10') }}

searchField()

Builds a HTML input[type='search'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::searchField(
  4. [
  5. 'search',
  6. 'q' => 'startpage',
  7. ]
  8. );
  9. // <input type='search' id='search' name='search' q='startpage' />

HTML syntax:

  1. <?php echo $this->tag->searchField(
  2. [
  3. 'search',
  4. 'q' => 'startpage',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ search_field('search', 'q': 'startsearch') }}

submitButton()

Builds a HTML input[type='submit'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::submitButton(
  4. [
  5. 'Save',
  6. ]
  7. );
  8. // <input type='submit' value='Save' />

HTML syntax:

  1. <?php echo $this->tag->submitButton(
  2. [
  3. 'Save',
  4. ]
  5. ); ?>

Volt syntax:

  1. {{ submit_button('Save') }}

telField()

Builds a HTML input[type='tel'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::telField(
  4. [
  5. 'mobile',
  6. 'size' => '12',
  7. ]
  8. );
  9. // <input type='tel' id='mobile' name='mobile' size='12' />

HTML syntax:

  1. <?php echo $this->tag->telField(
  2. [
  3. 'mobile',
  4. 'size' => '12',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ tel_field('mobile', 'size': '12') }}

passwordField()

Builds a HTML input[type='text'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::textField(
  4. [
  5. 'name',
  6. 'size' => '30',
  7. ]
  8. );
  9. // <input type='text' id='name' name='name' size='30' />

HTML syntax:

  1. <?php echo $this->tag->textField(
  2. [
  3. 'name',
  4. 'size' => '30',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ text_field('name', 'size': '30') }}

timeField()

Builds a HTML input[type='time'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::timeField(
  4. [
  5. 'start',
  6. 'size' => '5',
  7. ]
  8. );
  9. // <input type='time' id='start' name='start' size='5' />

HTML syntax:

  1. <?php echo $this->tag->timeField(
  2. [
  3. 'start',
  4. 'size' => '5',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ time_field('start', 'size': '5') }}

urlField()

Builds a HTML input[type='url'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::urlField(
  4. [
  5. 'homepage',
  6. ]
  7. );
  8. // <input type='url' id='homepage' name='homepage' />

HTML syntax:

  1. <?php echo $this->tag->urlField(
  2. [
  3. 'homepage',
  4. ]
  5. ); ?>

Volt syntax:

  1. {{ url_field('homepage') }}

weekField()

Builds a HTML input[type='week'] tag. Accepts an array with the attributes of the element. The first element of the array is the name of the element.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::weekField(
  4. [
  5. 'week',
  6. 'size' => '2',
  7. ]
  8. );
  9. // <input type='week' id='week' name='week' size='2' />

HTML syntax:

  1. <?php echo $this->tag->weekField(
  2. [
  3. 'week',
  4. 'size' => '2',
  5. ]
  6. ); ?>

Volt syntax:

  1. {{ week_field('week', 'size': '2') }}

Elements

image()

Builds a HTML image tag. Accepts an array with the attributes of the element. The first element of the array is the src of the element. The method accepts a second boolean parameter, signifying whether this resource is local or not.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::image(
  4. [
  5. 'img/hello.gif',
  6. 'alt' => 'alternative text',
  7. ]
  8. );
  9. // <img alt='alternative text' src='/your-app/img/hello.gif'>
  10. echo Tag::image(
  11. 'http://static.mywebsite.com/img/bg.png',
  12. false
  13. );
  14. // <img src='http://static.mywebsite.com/img/bg.png'>

HTML syntax:

  1. <?php echo $this->tag->image(
  2. [
  3. 'img/hello.gif',
  4. 'alt' => 'alternative text',
  5. ]
  6. ); ?>
  7. <?php echo $this->tag->image(
  8. 'http://static.mywebsite.com/img/bg.png',
  9. false
  10. ); ?>

Volt syntax:

  1. {{ image('img/hello.gif', 'alt': 'alternative text') }}
  2. {{ image('http://static.mywebsite.com/img/bg.png', false) }}

select()

select() is a helper that allows you to create a <select> element based on a Phalcon\Mvc\Model resultset. You will need to have a valid database connection set up in your DI container for this method to produce the correct HTML. The component requires parameters and data to operate.

  • parameters - string/array. If a string is passed, it will be the name of the element. If an array is passed, the first element will be the name of the element. There available parameters are:
    • id - string - sets the id of the element
    • using - array - required a two element array defining the key and value fields of the model to populate the select
    • useEmpty - bool - defaults to false. If set, it will add an empty option to the select box
    • emptyText - string - the text to display for the empty option (i.e. Choose an option)
    • emptyValue - string/number - the value to assign for the empty option
    • any additional HTML attributes in a key/value format
  • data - Resultset the resultset from the model operation.
  1. <?php
  2. use MyApp\Constants\Status;
  3. use MyApp\Models\Invoices;
  4. use Phalcon\Tag;
  5. $resultset = Invoices::find(
  6. [
  7. 'conditions' => 'inv_status_flag = :status:',
  8. 'bind' => [
  9. 'status' => Status::UNPAID,
  10. ]
  11. ]
  12. );
  13. echo Tag::select(
  14. [
  15. 'invoiceId',
  16. $resultset,
  17. 'using' => [
  18. 'inv_id',
  19. 'inv_title',
  20. ],
  21. 'useEmpty' => true,
  22. 'emptyText' => 'Choose an Invoice to pay',
  23. 'emptyValue' => '0',
  24. ]
  25. );
  26. // <select id='invoiceId' name='invoiceId'>
  27. // <option value='0'>Choose an Invoice to pay</option>
  28. // <option value='24'>Chocolates 24oz box</option>
  29. // <option value='77'>Sugar 1 bag</option>
  30. // </select>

HTML syntax:

  1. <?php echo $this->tag->select(
  2. [
  3. 'invoiceId',
  4. $resultset,
  5. 'using' => [
  6. 'inv_id',
  7. 'inv_title',
  8. ],
  9. 'useEmpty' => true,
  10. 'emptyText' => 'Choose an Invoice to pay',
  11. 'emptyValue' => '0',
  12. ]
  13. ); ?>

Volt syntax:

  1. {{ select(
  2. [
  3. 'invoiceId',
  4. $resultset,
  5. 'using' : [
  6. 'inv_id',
  7. 'inv_title',
  8. ],
  9. 'useEmpty' : true,
  10. 'emptyText' : 'Choose an Invoice to pay',
  11. 'emptyValue' : '0',
  12. ]
  13. ) }}

selectStatic()

This helper is similar to select(), but it uses a PHP array as the source. The component requires parameters and data to operate.

  • parameters - string/array. If a string is passed, it will be the name of the element. If an array is passed, the first element will be the name of the element. There available parameters are:
    • id - string - sets the id of the element
    • useEmpty - bool - defaults to false. If set, it will add an empty option to the select box
    • emptyText - string - the text to display for the empty option (i.e. Choose an option)
    • emptyValue - string/number - the value to assign for the empty option
    • any additional HTML attributes in a key/value format
  • data - array the array of data with key as the id and value as the text
  1. <?php
  2. use MyApp\Constants\Status;
  3. use MyApp\Models\Invoices;
  4. use Phalcon\Tag;
  5. $resultset = [
  6. 24 => 'Chocolates 24oz box',
  7. 77 => 'Sugar 1 bag',
  8. ];
  9. echo Tag::selectStatic(
  10. [
  11. 'invoiceId',
  12. $resultset,
  13. 'useEmpty' => true,
  14. 'emptyText' => 'Choose an Invoice to pay',
  15. 'emptyValue' => '0',
  16. ]
  17. );
  18. // <select id='invoiceId' name='invoiceId'>
  19. // <option value='0'>Choose an Invoice to pay</option>
  20. // <option value='24'>Chocolates 24oz box</option>
  21. // <option value='77'>Sugar 1 bag</option>
  22. // </select>

HTML syntax:

  1. <?php echo $this->tag->selectStatic(
  2. [
  3. 'invoiceId',
  4. $resultset,
  5. 'useEmpty' => true,
  6. 'emptyText' => 'Choose an Invoice to pay',
  7. 'emptyValue' => '0',
  8. ]
  9. ); ?>

Volt syntax:

  1. {{ select(
  2. [
  3. 'invoiceId',
  4. $resultset,
  5. 'useEmpty' : true,
  6. 'emptyText' : 'Choose an Invoice to pay',
  7. 'emptyValue' : '0',
  8. ]
  9. ) }}

tagHtml()

Phalcon offers a generic HTML helper that allows the generation of any kind of HTML element. It is up to the developer to produce a valid HTML element name to the helper. The accompanying tagHtmlClose() can be used to close the tag if necessary.

The tagHtml() accepts the following parameters

  • name - string - the name of the element
  • attributes - array - any attributes
  • selfClose - bool - whether this is a self closing element or not
  • onlyStart - bool - whether to produce only the opening part of the tag (i.e. <tag> vs. <tag></tag>)
  • useEol - bool - add a PHP_EOL at the end of the generated string or not
  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::tagHtml(
  4. 'canvas',
  5. [
  6. 'id' => 'canvas1',
  7. 'width' => '300',
  8. 'class' => 'cnvclass',
  9. ],
  10. false,
  11. true,
  12. true
  13. );
  14. echo 'This is my canvas';
  15. echo Tag::tagHtmlClose('canvas');
  16. // <canvas id='canvas1' width='300' class='cnvclass'>
  17. // This is my canvas
  18. // </canvas>

HTML syntax:

  1. <?php
  2. echo $this->tag->tagHtml(
  3. 'canvas',
  4. [
  5. 'id' => 'canvas1',
  6. 'width' => '300',
  7. 'class' => 'cnvclass',
  8. ],
  9. false,
  10. true,
  11. true
  12. );
  13. echo 'This is my canvas';
  14. echo $this->tag->tagHtmlClose('canvas');
  15. ?>

Volt syntax:

  1. {{ tag_html('canvas', ['id': 'canvas1', width': '300', 'class': 'cnvclass'], false, true, true) }}
  2. This is my canvas
  3. {{ tag_html_close('canvas') }}

Assets

Phalcon\Tag offers helper methods to generate stylesheet and javascript HTML tags.

The first parameter a string or an array is the parameters necessary to construct the element. The second parameter is a boolean, dictating whether the link is pointing to a local asset or a remote.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::stylesheetLink('css/style.css');
  4. // <link rel='stylesheet' href='/css/style.css'>
  5. echo Tag::stylesheetLink(
  6. 'https://fonts.googleapis.com/css?family=Rosario',
  7. false
  8. );
  9. // <link rel='stylesheet'
  10. // href='https://fonts.googleapis.com/css?family=Rosario'
  11. // type='text/css'>

HTML syntax

  1. <?php echo $this->tag->stylesheetLink('css/style.css'); ?>
  2. <?php
  3. echo $this->tag->stylesheetLink(
  4. 'https://fonts.googleapis.com/css?family=Rosario',
  5. false
  6. ); ?>

Volt Syntax:

  1. {{ stylesheet_link('css/style.css') }}
  2. {{ stylesheet_link('https://fonts.googleapis.com/css?family=Rosario', false) }}

javascriptInclude()

The first parameter a string or an array is the parameters necessary to construct the element. The second parameter is a boolean, dictating whether the link is pointing to a local asset or a remote.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::javascriptInclude('js/jquery.js');
  4. // <script src='/js/jquery.js' type='text/javascript'></script>
  5. echo Tag::javascriptInclude(
  6. 'https://code.jquery.com/jquery/jquery.min.js',
  7. false
  8. );
  9. // <script src='https://code.jquery.com/jquery/jquery.min.js'
  10. // type='text/javascript'></script>

HTML syntax

  1. <?php echo $this->tag->javascriptInclude('js/jquery.js'); ?>
  2. <?php
  3. echo $this->tag->javascriptInclude(
  4. 'https://fonts.googleapis.com/css?family=Rosario',
  5. false
  6. ); ?>

Volt Syntax:

  1. {{ javascript_include('js/jquery.js') }}
  2. {{ javascript_include('https://code.jquery.com/jquery/jquery.min.js', false) }}

A common task in any web application is to show links that help with the navigation from one area to another. Phalcon\Tag offers linkTo() to help with this task. The method accepts three parameters.

  • parameters - array/string - The attributes and parameters of the element. If a string is passed it will be treated as the target URL for the link. If an array is passed, the following elements can be sent:
    • action - the URL. If the action is an array, you can reference a named route defined in your routes using the for element
    • query - the base query for the URL
    • text - the text of the link
    • local - whether this is a local or remote link
    • additional key/value attributes for the link
  • text - string - the text of the link
  • local - bool - whether this is a local or remote link
  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::linkTo('signup/register', 'Register Here!');
  4. // <a href='/signup/register'>Register Here!</a>
  5. echo Tag::linkTo(
  6. [
  7. 'signup/register',
  8. 'Register Here!',
  9. 'class' => 'btn-primary',
  10. ]
  11. );
  12. // <a href='/signup/register' class='btn-primary'>Register Here!</a>
  13. echo Tag::linkTo('http://phalcon.io/', 'Phalcon', false);
  14. // <a href='http://phalcon.io/'>Phalcon</a>
  15. echo Tag::linkTo(
  16. [
  17. 'http://phalcon.io/',
  18. 'Phalcon Home',
  19. false,
  20. ]
  21. );
  22. // <a href='http://phalcon.io/'>Phalcon Home</a>

HTML syntax:

  1. <?php
  2. echo $this->tag->linkTo('signup/register', 'Register Here!');
  3. echo $this->tag->linkTo(
  4. [
  5. 'signup/register',
  6. 'Register Here!',
  7. 'class' => 'btn-primary',
  8. ]
  9. );
  10. echo $this->tag->linkTo('http://phalcon.io/', 'Phalcon', false);
  11. echo $this->tag->linkTo(
  12. [
  13. 'http://phalcon.io/',
  14. 'Phalcon Home',
  15. false,
  16. ]
  17. );
  18. ?>

Volt syntax:

  1. {{ link_to('signup/register', 'Register Here!') }}
  2. {{ link_to(
  3. 'signup/register',
  4. 'Register Here!',
  5. 'class': 'btn-primary'
  6. ) }}
  7. {{ link_to('http://phalcon.io/', 'Phalcon', false) }}
  8. {{ link_to(
  9. 'http://phalcon.io/',
  10. 'Phalcon Home',
  11. false
  12. ) }}

If you have named routes, you can use the for keyword in your parameter array to reference it. Phalcon\Tag will resolve the route internally and produce the correct URL using Phalcon\Url.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::linkTo(
  4. [
  5. [
  6. 'for' => 'invoice-view',
  7. 'title' => 12345,
  8. 'name' => 'invoice-12345'
  9. ],
  10. 'Show Invoice'
  11. ]
  12. );

HTML syntax:

  1. <?php
  2. echo $this->tag->linkTo(
  3. [
  4. [
  5. 'for' => 'invoice-view',
  6. 'title' => 12345,
  7. 'name' => 'invoice-12345'
  8. ],
  9. 'Show Invoice'
  10. ]
  11. );
  12. ?>

Volt syntax:

  1. {{ link_to('signup/register', 'Register Here!') }}
  2. {{ link_to(
  3. [
  4. 'for' : 'invoice-view',
  5. 'title' : 12345,
  6. 'name' : 'invoice-12345'
  7. ],
  8. 'Show Invoice',
  9. 'class': 'edit-btn'
  10. ) }}

Forms

Forms play an important role in any web application, since they are used to collect input from the user. Phalcon\Tag offers the form() and endForm() methods, which create <form> elements.

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::form(
  4. [
  5. '/admin/invoices/create',
  6. 'method' => 'post',
  7. 'class' => 'input'
  8. ]
  9. );
  10. // <form action='admin/invoices/create' method='post' class='input'>
  11. // ...
  12. echo Tag::endForm();
  13. // </form>

HTML syntax:

  1. <?php
  2. echo $this->tag->form(
  3. [
  4. '/admin/invoices/create',
  5. 'method' => 'post',
  6. 'class' => 'input'
  7. ]
  8. );
  9. // ...
  10. echo $this->tag->endForm();
  11. ?>

Volt syntax:

  1. {{ form(
  2. [
  3. '/admin/invoices/create',
  4. 'method' : 'post',
  5. 'class' : 'input'
  6. ]
  7. );
  8. {{ end_form() }}

Phalcon also provides a form builder to create forms in an object-oriented manner.

Data

setDefault()

You can use setDefault() to pre populate values for elements generated by Phalcon\Tag. The helpers of this component will retain the values between requests. This way you can easily show validation messages without losing entered data. Every form helper supports the parameter value. With it you can specify a value for the helper directly. When the parameter is present, any preset value using setDefault() or via request will be ignored.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setDefault('framework', 'Phalcon');
  4. echo Tag::textField(
  5. [
  6. 'framework',
  7. 'class' => 'input'
  8. ]
  9. );
  10. // <input type='text' id='framework' name='framework'
  11. // value='Phalcon' class='class' />

setDefaults()

setDefaults() allows you to specify more than one value to be set in elements of your form, by passing a key value array. The method can be called more than one time and each time it is called it will overwrite the data set in the previous call. You can however specify the second parameter as true so that the values are merged.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setDefaults(
  4. [
  5. 'framework' => 'Phalcon',
  6. 'version' => '4.0',
  7. ]
  8. );
  9. echo Tag::textField(
  10. [
  11. 'framework',
  12. 'class' => 'input'
  13. ]
  14. );
  15. // <input type='text' id='framework' name='framework'
  16. // value='Phalcon' class='class' />
  17. echo Tag::textField(
  18. [
  19. 'version',
  20. 'class' => 'input'
  21. ]
  22. );
  23. // <input type='text' id='version' name='version'
  24. // value='4.0' class='class' />

getValue()

This method is called from every helper in this component, to find whether a value has been set for an element wither by having used setDefault() before or in the $_POST superglobal.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setDefaults(
  4. [
  5. 'framework' => 'Phalcon',
  6. 'version' => '4.0',
  7. ]
  8. );
  9. echo Tag::getValue('framework'); // 'Phalcon'
  10. $_POST = [
  11. 'framework' => 'Phalcon',
  12. 'version' => '4.0',
  13. ];
  14. echo Tag::getValue('framework'); // 'Phalcon'

hasValue()

This method checks if a value in an element has already been set using setDefault() or is in the $_POST superglobal.

  1. <?php
  2. use Phalcon\Tag;
  3. Tag::setDefaults(
  4. [
  5. 'framework' => 'Phalcon',
  6. 'version' => '4.0',
  7. ]
  8. );
  9. echo Tag::hasValue('framework'); // 'true'
  10. $_POST = [
  11. 'framework' => 'Phalcon',
  12. 'version' => '4.0',
  13. ];
  14. echo Tag::hasValue('framework'); // 'true'

Escaping

Phalcon\Tag automatically escapes text supplied for its helpers. If your application requires it, you can disable automatic escaping by using setAutoEscape().

  1. <?php
  2. use Phalcon\Tag;
  3. echo Tag::textField(
  4. [
  5. 'framework',
  6. 'value' => '<h1>hello</h1>',
  7. ]
  8. );
  9. // <input type="text" id="framework" name="framework"
  10. // value="&lt;h1&gt;hello&lt;/h1&gt;" />
  11. Tag::setAutoescape(false);
  12. echo Tag::textField(
  13. [
  14. 'framework',
  15. 'value' => '<h1>hello</h1>',
  16. ]
  17. );
  18. // <input type="text" id="framework" name="framework"
  19. // value="<h1>hello</h1>" />

Dependency Injection

If you use the Phalcon\Di\FactoryDefault container, the Phalcon\Tag is already registered for you with the name tag.

An example of the registration of the service as well as accessing it is below:

Direct

  1. <?php
  2. use Phalcon\Di;
  3. use Phalcon\Tag;
  4. $container = new Di();
  5. $container->set(
  6. 'tag',
  7. function () use {
  8. return new Tag();
  9. }
  10. );

You can always implement your own tag helper and register it in the place of tag in the Di container.

Accessing the service from any component that implements the Phalcon\Di\Injectable is as simple as accessing the tag property.

  1. <?php
  2. use Phalcon\Mvc\Controller;
  3. use Phalcon\Tag;
  4. /**
  5. * @property Tag $tag
  6. */
  7. class SessionController extends Controller
  8. {
  9. public function indexAction()
  10. {
  11. $this->tag->setTitle('Phalcon Framework');
  12. }
  13. }

Custom

You can easily extend this functionality and create your own helpers.

  • First create a new directory in your application’s file system that the helper files will be stored.
  • Name it something that will represent it. For instance in this example we use customhelpers.
  • Create a file called MyTags.php in your customhelpers directory.
  • Extend the Phalcon\Tag class and implement your own methods.
  1. <?php
  2. namespace MyApp;
  3. use Phalcon\Tag;
  4. class MyTags extends Tag
  5. {
  6. /**
  7. * Generates a widget to show a HTML5 audio tag
  8. *
  9. * @param array
  10. * @return string
  11. */
  12. public static function audioField($parameters)
  13. {
  14. // Converting parameters to array if it is not
  15. if (true !== is_array($parameters)) {
  16. $parameters = [$parameters];
  17. }
  18. // Determining attributes 'id' and 'name'
  19. $parameters[0] = $parameters[0] ?? $parameters['id'];
  20. $id = $parameters[0];
  21. $parameters['name'] = $parameters['name'] ?? $id;
  22. // Determining widget value,
  23. // \Phalcon\Tag::setDefault() allows to set the widget value
  24. if (true === isset($parameters['value'])) {
  25. $value = $parameters['value'];
  26. unset($parameters['value']);
  27. } else {
  28. $value = self::getValue($id);
  29. }
  30. // Generate the tag code
  31. $code = sprintf(
  32. '<audio id="%s" value="%s" ',
  33. $id,
  34. $value
  35. );
  36. foreach ($parameters as $key => $attributeValue) {
  37. if (!is_integer($key)) {
  38. $code .= sprintf('%s="%s" ', $key, $attributeValue);
  39. }
  40. }
  41. $code.=' />';
  42. return $code;
  43. }
  44. }

After creating our custom helper, we will autoload the new directory that contains our helper class from our index.php located in the public directory.

  1. <?php
  2. use Phalcon\Loader;
  3. use Phalcon\Mvc\Application;
  4. use Phalcon\Di\FactoryDefault();
  5. use Phalcon\Exception as PhalconException;
  6. try {
  7. $loader = new Loader();
  8. $loader->registerDirs(
  9. [
  10. '../app/controllers',
  11. '../app/models',
  12. '../app/customhelpers', // Add the new helpers folder
  13. ]
  14. );
  15. $loader->register();
  16. $di = new FactoryDefault();
  17. // Assign our new tag a definition so we can call it
  18. $di->set(
  19. 'MyTags',
  20. function () {
  21. return new MyTags();
  22. }
  23. );
  24. $application = new Application($di);
  25. $response = $application->handle(
  26. $_SERVER['REQUEST_URI']
  27. );
  28. $response->send();
  29. } catch (PhalconException $e) {
  30. echo 'PhalconException: ', $e->getMessage();
  31. }

Now you are ready to use your new helper within your views:

  1. <?php
  2. echo MyTags::audioField(
  3. [
  4. 'name' => 'test',
  5. 'id' => 'audio_test',
  6. 'src' => '/path/to/audio.mp3',
  7. ]
  8. );
  9. ?>

You can also check out Volt a faster template engine for PHP, where you can use a more developer friendly syntax for helpers provided by Phalcon\Tag.