Skip to content

Commit

Permalink
Remove custom loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Dec 11, 2024
1 parent 736e7a2 commit b36fd43
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 488 deletions.
72 changes: 3 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,30 @@
[![PHPUnit](https://github.com/salesforce/handlebars-php/actions/workflows/ci.yml/badge.svg)](https://github.com/salesforce/handlebars-php/actions/workflows/ci.yml)

---

#handlebars-php

---

#### A simple, logic-less, yet powerful templating engine for PHP

---

Name: **handlebars-php**

License: MIT

Requirements: PHP >= 7.4

---


## About Handlebars

Handlebars provides the power necessary to let you build semantic templates effectively with no frustration,
that keep the view and the code separated like we all know they should be.


Fork of: [Handlebars.php by XaminProject](https://github.com/mardix/Handlebars)

Handlebars, is the PHP port of [Handlebars.js](http://handlebarsjs.com/)

---

## Install Handlebars


You can just download Handlebars.php as is, or with Composer.

To install with composer, add the following in the require key in your **composer.json** file

`"salesforce/handlebars-php": "1.*"`

composer.json

```json
{
"name": "myapp/name",
"description": "My awesome app name",
"require": {
"salesforce/handlebars-php": "1.*"
}
}
```

-----
composer require salesforce/handlebars-php:"^3.0"
```

## Getting Started

At the minimum, we are required to have an array model and a template string. Alternatively we can have a file containing handlebars (or html, text, etc) expression.



#### Template

Handlebars templates look like regular HTML, with embedded handlebars expressions.
Expand All @@ -79,40 +44,10 @@ The string above can be used as is in your PHP file, or be put in a file (ie: */

#### PHP file

Now the we've created our template file, in a php file (index.php) we'll create the data to passed to the model. The model is a key/value array.
Now that we've created our template file, in a php file (index.php) we'll create the data to passed to the model. The model is a key/value array.

Below we are going to create the Handlebars object, set the partials loader, and put some data in the model.

**/index.php**

```php
<?php

# With composer we can autoload the Handlebars package
require_once ("vendor/autoload.php");

use Handlebars\Handlebars;
use Handlebars\Loader\FilesystemLoader;

# Set the partials files
$partialsDir = __DIR__."/templates";
$partialsLoader = new FilesystemLoader($partialsDir,
[
"extension" => "html"
]
);

# We'll use $handlebars throughout this the examples, assuming the will be all set this way
$handlebars = new Handlebars([
"loader" => $partialsLoader,
"partials_loader" => $partialsLoader
]);

# Will render the model to the templates/main.tpl template
$model = [...];
echo $handlebars->render("main", $model);
```

#### Assign Data

The simplest way to assign data is to create an Array model. The model will contain all the data that will be passed to the template.
Expand Down Expand Up @@ -740,7 +675,6 @@ $object->{'@unknown'} = 'zucchini';
$data = ['objects' => [$object]];

$engine = new \Handlebars\Handlebars(array(
'loader' => new \Handlebars\Loader\StringLoader(),
'helpers' => new \Handlebars\Helpers(),
'enableDataVariables'=> $enabled,
));
Expand Down
139 changes: 4 additions & 135 deletions src/Handlebars/Handlebars.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,18 @@

namespace Handlebars;

use Handlebars\Loader\StringLoader;
use InvalidArgumentException;

class Handlebars
{
private static $instance = null;
const VERSION = '2.2';

const OPTION_ENABLE_DATA_VARIABLES = 'enableDataVariables';

/**
* factory method
*
* @param array $options see __construct's options parameter
*/
public static function factory(array $options = []): Handlebars
{
if (! self::$instance) {
self::$instance = new self($options);
}

return self::$instance;
}

private Tokenizer $tokenizer;

private Parser $parser;

private Helpers $helpers;

private Loader $loader;

private Loader $partialsLoader;

private array $aliases = [];

/**
Expand All @@ -61,34 +39,18 @@ public static function factory(array $options = []): Handlebars
* Handlebars engine constructor
* $options array can contain :
* helpers => Helpers object
* loader => Loader object
* partials_loader => Loader object
* enableDataVariables => boolean. Enables @data variables (default: false)
*
* @param array $options array of options to set
*
* @throws \InvalidArgumentException
*/
public function __construct(Array $options = [])
public function __construct(array $options = [])
{
if (isset($options['helpers'])) {
$this->setHelpers($options['helpers']);
}

if (isset($options['loader'])) {
$this->setLoader($options['loader']);
}

if (isset($options['partials_loader'])) {
$this->setPartialsLoader($options['partials_loader']);
}

if (isset($options['partials_alias'])
&& is_array($options['partials_alias'])
) {
$this->aliases = $options['partials_alias'];
}

if (isset($options[self::OPTION_ENABLE_DATA_VARIABLES])) {
if (!is_bool($options[self::OPTION_ENABLE_DATA_VARIABLES])) {
throw new InvalidArgumentException(
Expand All @@ -110,16 +72,6 @@ public function render(string $template, $data): string
{
return $this->loadTemplate($template)->render($data);
}
/**
* To invoke when this object is called as a function
*
* @param string $template template name
* @param mixed $data data to use as context
*/
public function __invoke(string $template, $data): string
{
return $this->render($template, $data);
}

/**
* Set helpers for current engine
Expand Down Expand Up @@ -172,52 +124,6 @@ public function removeHelper(string $name): void
$this->getHelpers()->remove($name);
}

/**
* Set current loader
*/
public function setLoader(Loader $loader): void
{
$this->loader = $loader;
}

/**
* Get current loader
*/
public function getLoader(): Loader
{
if (! isset($this->loader)) {
$this->loader = new StringLoader();
}
return $this->loader;
}

/**
* Set current partials loader
*/
public function setPartialsLoader(Loader $loader): void
{
$this->partialsLoader = $loader;
}

/**
* Get current partials loader
*/
public function getPartialsLoader(): Loader
{
if (!isset($this->partialsLoader)) {
$this->partialsLoader = new StringLoader();
}
return $this->partialsLoader;
}

/**
* Set the Handlebars Tokenizer instance.
*/
public function setTokenizer(Tokenizer $tokenizer): void
{
$this->tokenizer = $tokenizer;
}

/**
* Get the current Handlebars Tokenizer instance.
*
Expand All @@ -233,14 +139,6 @@ public function getTokenizer(): Tokenizer
return $this->tokenizer;
}

/**
* Set the Handlebars Parser instance.
*/
public function setParser(Parser $parser): void
{
$this->parser = $parser;
}

/**
* Get the current Handlebars Parser instance.
*
Expand All @@ -266,9 +164,8 @@ public function isDataVariablesEnabled(): bool
/**
* Load a template by name with current template loader
*/
public function loadTemplate(string $name): Template
public function loadTemplate(string $source): Template
{
$source = $this->getLoader()->load($name);
$tree = $this->tokenize($source);
return new Template($this, $tree, $source);
}
Expand All @@ -281,36 +178,8 @@ public function loadPartial(string $name): Template
if (isset($this->aliases[$name])) {
$name = $this->aliases[$name];
}
$source = $this->getPartialsLoader()->load($name);
$tree = $this->tokenize($source);
return new Template($this, $tree, $source);
}

/**
* Register partial alias
*/
public function registerPartial(string $alias, string $content): void
{
$this->aliases[$alias] = $content;
}

/**
* Un-register partial alias
*/
public function unRegisterPartial(string $alias): void
{
if (isset($this->aliases[$alias])) {
unset($this->aliases[$alias]);
}
}

/**
* Load string into a template object
*/
public function loadString(string $source): Template
{
$tree = $this->tokenize($source);
return new Template($this, $tree, $source);
$tree = $this->tokenize($name);
return new Template($this, $tree, $name);
}

/**
Expand Down
65 changes: 0 additions & 65 deletions src/Handlebars/HandlebarsString.php

This file was deleted.

Loading

0 comments on commit b36fd43

Please sign in to comment.