Download the PHP package sy/component without Composer
On this page you can find all versions of the php package sy/component. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package component
Short Description Build your application as a tree of simpler components
License MIT
Informations about the package component
sy/component
The concept of component allows you to build an application as a tree of simpler components (Composite design pattern). And each component can be reusable.
Basically we can assume that a component is a stringable object. At this point, it just use a template to generate a string that can be in any format (html, xml, json, plain text etc...). On top of this, we can build web components by adding css and js properties.
The class Sy\Component is the base class of other web components (sy/webcomponent) and html page and elements (sy/html) like form and table etc...
Component template engine
The template engine used is sy/template
Template syntax notion: slot and block
Example with setVar method for filling a slot, your PHP script:
Template file, template.tpl:
Output result:
Example with setBlock method, your PHP script:
Template file, template.tpl:
Output result:
ELSE block
When a block is not set, you can use the ELSE block to show a default content.
Set block with isolated vars
By default, when the second parameter of setBlock method is empty, it will use vars globally set for setting slots in the block scope. It is possible to use isolated vars for setting slots on the block scope with the second parameter.
Result:
Set blocks using a data array
Template file, template.html:
Output:
Alternative template syntax
It's possible to use simple PHP template syntax. You must specify that you are using a PHP template file:
PHP template file, template.tpl:
Output result:
Create a component
Create a custom class derived from Sy\Component class.
For example in Hello.php
Hello.tpl
Use your component:
Add a component in another one
Use setVar method to add a component in another one.
Component actions
The actionDispatch method help you to call action method.
This method takes 2 arguments:
- actionName: $_REQUEST variable name, index.php?action=foo
- defaultMethod: this one is optionnal, if no action is called, it will perform this method
An action method name must be suffixed by 'Action': fooAction
For example in MyComponent.php
Component translators
Translator can be added in a Component. Each Translator will load translation data from a file in a specified directory. This translation file must be named as the detected language. For example, if the detected language is "fr", the PHP Translator will try to load "fr.php". And Gettext Translator will try to load "fr.mo".
This feature is provided by the library sy/translate
Language detection
Language will be detected using these variables in this order:
- $_SESSION['sy_language']
- $_COOKIE['sy_language']
- $_SERVER['HTTP_ACCEPT_LANGUAGE']
Translation methods
- void Component::addTranslator(string $directory [, string $type = 'php', string $lang = ''])
- string Component::_(mixed $values)
Exemple:
PHP Translation file:
Template file:
Output result:
Add multiple translators
It's possible to add multiple translators in a component. The order of addition is important because the translate process will stop right after the first translation data found.
Translators transmission to inner web component
When adding a web component B in a web component A, all the translators of A will be added into B.
Translation file added in component A:
Translation file added in component C:
Output result:
The translator of A is transmitted to B and C. C will use his own translator in priority.
We need to use the mount method here to register callbacks on the mount event triggered just before the rendering stage. This is because we need to ensure that all the leaf components received the translators from their parents components.
All versions of component with dependencies
sy/template Version ^2
sy/http Version ^1
sy/debug Version ^1
sy/translate Version ^1.0