Pimcore Placeholders are usefull when you have a text (e.g. a wysiwyg editor) and you want to embedd dynamic values within this text.
Most of the time you will use Placeholders in combination with Email Documents and the Pimcore_Mail Class.
The Syntax of a Placeholder is "%PLACEHOLDERNAME(PARAMETERKEY,JSON-CONFIG);"
| PLACEHOLDERNAME | This is the last Part of the Placeholder class name E.g.: To use the Placeholder Pimcore_Placeholder_Object you would type %Object(key,params); |
| PARAMETERKEY | This is the key of the parameter-array that you pass as second parameter to the "replacePlaceholders" method. |
| JSON-CONFIG | You can pass a Json-Config to the Placeholder. (It depends on the Placeholder if the config is used) |
Example usage
Lets assume you have a text "Thank you for the order of "PRODUCTNAME"" and you want replace "PRODUCTNAME" with the real Product name.
The following code-snippet replaces the Placeholder "%Object(...);" with the Product name.
$text = 'Thank you for the order of "%Object(object_id,{"method" : "getName"});"'; $placeholder = new Pimcore_Placeholder(); $params = array('object_id' => 73613, 'locale' => 'de_DE'); echo $replaced = $placeholder->replacePlaceholders($text,$params);
More detailed:
The replacePlaceholders() method accepts 3 parameter:
| First one | The text that contains the placeholders which sould be replaced or a document (the document is rendered to html) |
| Second | The dynamic parameter for replacement |
| Thrid | A document. This parameter is usefull when you pass a plain text and you need values from a document in the placeholder object. In the placeholder object you can access the document with $this->getDocument() |
When you call the replacePlaceholders() method, it determines the placeholders and creates the corresponding placeholder object.
Create your own Placeholders
You can also create your own placeholder. By default it is assumed that your individual placehodlers are located in /website/lib/Website/Placeholder/YourPlaceholder.php .
If you want to change the default placeholder location you can call "Pimcore_Placeholder::setWebsiteClassPrefix(...)" to change the location (make sure you set them before any individual placeholder is called).
E.g. "Pimcore_Placeholder::setWebsiteClassPrefix('Website_Tool_');" -> the placeholder location would be "/website/lib/Website/Tool/YourPlaceholder.php"
Now we assuem that we want to create a simple Placeholder that replaces the data of a person.
Therefore we create a new class that extends "Pimcore_Placeholder_Abstract". Our class has to implement at least 2 methods "getTestValue()" and "getReplacement()".
Note: The getTestValue() is currently not in use but in upcoming releases it will be used for test replacements.
The "getReplacement()" method has to return the value that sould be used for the replacement of the placeholder.
The "getTestValue()" method sould return a value for testing purpose.
<?php class Website_Placeholder_Person extends Pimcore_Placeholder_Abstract { public function getTestValue() { $value = ''; switch ($this->getPlaceholderKey()) { case 'firstName' : $value = 'Homer'; break; case 'lastName' : $value = 'Simpson'; break; //... } return '<span class="testValue">' . $value . ' </span>'; } public function getReplacement() { $value = $this->getValue(); //$document would contain the document that we passed as third parameter to the replacePlaceholders() method $document = $this->getDocument(); switch ($this->getPlaceholderKey()) { case 'firstName' : return ucfirst($value); break; case 'lastName' : return ucfirst($value); break; case 'salutation' : if ($value == 'mr') { return 'Dear mr.'; } elseif ($value == 'mrs') { return 'Dear mrs.'; } break; } } }
Example usage:
public function ownPlaceholderAction(){ $this->disableViewAutoRender(); $text = '%Person(salutation); %Person(firstName); %Person(lastName); thank you for your order.'; $placeholder = new Pimcore_Placeholder(); $params = array('firstName' =>'Pim', 'lastName' => 'Core', 'salutation' => 'mr'); $replaced = $placeholder->replacePlaceholders($text,$params,Document::getById(1)); echo $replaced; }
As all Placeholders must extend the class Pimcore_Placeholder_Abstract there are some getters you can use in your Placeholders.
| Method | Description |
|---|---|
| getPlaceholderKey() | Returns the key from the dynamic parameters. E.g. If you take a look at the "Create your own Placeholder" example. When the placeholder "%Person(salutation);" is procecces the key would be "salutation" When the placeholder "%Person(firstName);" is processed the key would be "firstName" ... |
| getValue() | Returns the value from the dynamic parameter. |
| getPlaceholderConfig() | Returns the Json-Config of the placeholder (if it was set), otherwise returns an empty JSON-Config object |
| getParams() | Returns the array that you passed to the replacePlaceholders() as second parameter. |
| getParam($key) | Returns a specfic parameter form the array that you passed to the replacePlaceholders() as second parameter. |
| getPlaceholderString() | Returns the placeholder string (raw text) |
| getLocale() / getLanguage() | Returns the current locale / language |
| getEmptyValue() | If the getReplacement() method returns an empty value the "getEmptyValue()" is called and the value which is returned by the "getEmptyValue()" method is used for the replacement. |