marcoshoo / phptal-laravel by marcoshoo

PHPTAL for Laravel 5.x
307
1
1
Package Data
Maintainer Username: marcoshoo
Maintainer Contact: marcos.hoo@gmail.com (Marcos Soares)
Package Create Date: 2015-09-05
Package Last Update: 2016-02-07
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-12-14 15:06:14
Package Statistics
Total Downloads: 307
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 1
Total Forks: 2
Total Open Issues: 1

Allows you to use PHPTAL seamlessly in Laravel

Installation

Add MarcosHoo\LaravelPHPTAL as a requirement to composer.json:

{
    "require": {
        "phptal/phptal": "dev-master",
        "marcoshoo/phptal-laravel": "dev-master"
    }
}

Update your packages with composer update or install with composer install.

Once Composer has installed or updated your packages you need to register PHPTAL with Laravel. Open up app/config/app.php and find the providers key towards the bottom and add:

MarcosHoo\LaravelPHPTAL\PHPTALServiceProvider::class,

Next step is copy example config to your config directory.

php ./artisan vendor:publish --provider 'MarcosHoo\LaravelPHPTAL\PHPTALServiceProvider'

Configuration

Currently PHPTAL is set by default to HTML5, however, you can change this setting in its config.php file, XHTML, or XML.

Filters

PHPTAL also has a nice feature which you can use called filters, for example I have one that bust the cache for images, js, css, this is configurable via the config file.

'preFilters' => [
    'bustCache',
    'minimizeJs',
    'adderJs'
]

#Usage

You call the PHPTAL template like you would any other view:

view('hello', [...])

Translation

PHPTALTranslator class uses Laravel translation service, so the translation files should be placed in the same translation files directory of the framework. Examples of how to use the a translation with id 'welcome' contained in a file called 'messages.php':

<div i18n:domain="messages" i18n:translate="string:welcome"></div>
<div i18n:domain="messages" i18n:translate="">welcome</div>
<div i18n:translate="string:messages.welcome"></div>
<div i18n:translate="">messages.welcome</div>

Pluralization

Translation file:

<?php
# File 'messages.php'
return [
    'apples' => 'There is one apple|There are many apples',
    'oranges' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',    
];
<div 18n:domain="messages" i18n:translate="string:apples|5">
    <!--There are many apples -->
</div>
<span tal:define="q php:22" i18n:translate="string:messages.oranges|${q}">
    <!--There are many -->
</span> oranges

Interpolation

<?php
# File 'messages.php'
return [
    'welcome_ex1' => 'Welcome ${user}, you have ${mails} unread mails.',
    'welcome_ex2' => 'Welcome :name!',
    'welcome_ex3' => 'Welcome :anonymous!',
    'anonymous' => 'anonymous user',
];
<div 18n:domain="messages" i18n:translate="string:welcome_ex1">
    <span i18name="name" tal:replace="user/name"/>
    <span i18name="name" tal:replace="user/nbrMails"/>
</div>
<div 18n:domain="messages" i18n:translate="string:welcome_ex1||user=${user/name}|mails=${user/nbrMails}">    
</div>
<div tal:define="name user/name" 18n:domain="messages" i18n:translate="string:welcome_ex2">    
</div>
<div 18n:domain="messages" i18n:translate="string:welcome_ex3">
    <!-- Welcome anonymous user! -->    
</div>