2017 © Pedro Peláez
 

library placeholders

Batch replace placeholders in a (HTML) string.

image

okneloper/placeholders

Batch replace placeholders in a (HTML) string.

  • Tuesday, November 8, 2016
  • by okneloper
  • Repository
  • 1 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Batch replace placeholders in a (HTML) string., (*1)

It gives you the full list of placeholders to resolve before replacing. This is handy when you have a lot of placeholders and you want to only fetch those that you need., (*2)

For instance, if your data is stored in a database or returned by a remote API, generally you want to fetch all data in one query/request., (*3)

Usage

$content = '


:#:home.h1:#:

:#:home.h2:#:

'; $content = $processor->process($content); echo $content; /*

Welcome to the site!

Slogan goes here

*/

See the full example for details of implementation., (*4)

Example

/**
 * Define a placeholder translator by implementing Okneloper\Placeholders\Translator interface
 */
class ExamplePlaceholderTranslater implements Translator
{
    /**
     * @param PlaceholderCollection $placeholders
     * @return array
     */
    public function translate($placeholders)
    {
        $keys = $placeholders->keys();
        // fetch placeholders using keys from database
        // ...
        $data = [
            'home.h1' => 'Welcome to the site!',
            'home.h2' => 'Slogan goes here',
        ];

        $replacements = [];
        foreach ($placeholders as $placeholder) {
            $replacements[$placeholder->placeholder] = $data[$placeholder->key];
            // optionally apply filters found in $placeholder->filters
        }

        return $replacements;
    }
}

// Batch replace placeholders in a response
    /**
     * Handle the event.
     *
     * @param Request $request
     * @param Response $response
     */
    public function handle(Request $request, Response $response)
    {
        $content = $response->getContent();

        $processor = new Processor(new ExamplePlaceholderTranslater());

        $content = $processor->process($content);

        $response->setContent($content);
    }

The Versions

08/11 2016

dev-master

9999999-dev https://github.com/okneloper/placeholders

Batch replace placeholders in a (HTML) string.

  Sources   Download

MIT

The Development Requires

by Aleksey Lavrinenko

placeholder replace placeholders

08/11 2016

v0.1.0

0.1.0.0 https://github.com/okneloper/placeholders

Batch replace placeholders in a (HTML) string.

  Sources   Download

MIT

The Development Requires

by Aleksey Lavrinenko

placeholder replace placeholders

08/11 2016

v1.0.0

1.0.0.0 https://github.com/okneloper/placeholders

Batch replace placeholders in a (HTML) string.

  Sources   Download

MIT

The Development Requires

by Aleksey Lavrinenko

placeholder replace placeholders