PHP code example of nafisc / spackle

1. Go to this page and download the library: Download nafisc/spackle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nafisc / spackle example snippets


<body>
   Hello, my name is {{name}}.
</body>

<body>
  {{>
    echo "Hello, Word!";
  <}}
</body>

<ul>
{{> 
  foreach ({{likes}} as $likeable) {
    echo '<li>';
    echo $likeable;
    echo '</li>';
  }
<}}
</ul>

class MyPlugin extends \Spackle\Plugin
{
    // The key used to notate the beginning of this element.
    public $key = 'url';

    // Parse each element found matching this plugin.
    // {{url some/data <}} woud come out to https://localhost/some/data
    public function parse($data)
    {
        return 'https://localhost/'.$data;
    }
}

. . .

// Add on a global scope
\Spackle\Plugin::add(new MyPlugin());

// Add to a specific parser
$parser->addPlugin(new MyPlugin());



Spackle\FileParser;

// The FileParser extends the TemplateParser
// and instead of cunstructing with a string
// is uses a file path.
$parser = new FileParser(
    // Template File Path
    __DIR__.'/test.spackle',

    // Optional Substitutions (add more with $parser->setSubstitution($key, $val))
    [
        'github_url' => 'https://github.com/nathan-fiscaletti/',
    ]
);

// You can set substitutions easily using the setSubstitution
// member function of a TemplateParser.
$parser->setSubstitution('name', 'Nathan Fiscaletti');

// You can also bind the Parser to a specific object.
// Anytime you reference `$this` in your code blocks
// within the template, this bound object will be
// referenced.
$bindTest = (object)[
    'bound' => 'Yes, Bound.'
];
$parser->bindTo($bindTest);

// You can use functions for substitutions.
// When the return value is an integer or
// a string, it will be directly substituted.
// Otherwise, the value will be used.
$parser->setSubstitution('likes', function() {
    return ['Coding', 'PHP', 'Trial by Combat'];
});

// The ->parse() function will parse the template
// and return the parsed value.
echo $parser->parse();
html
<h4>Welcome to Spackle!</h4>

Spackle was created by <a href="{{github_url}}" target="_blank">{{name}}</a>.<br />

Some things that {{name}} likes are:<br />
<ul>

{{> 
    foreach ({{likes}} as $likeable) {
        echo '<li>';
        echo $likeable;
        echo '</li>';
    }
<}}

</ul>

<b>Bound: {{> echo $this->bound; <}}