Skip to content

Instantly share code, notes, and snippets.

@royteusink
Last active October 29, 2021 09:40
Show Gist options
  • Save royteusink/4bb0cd3ae3542ea650283f7b5682d8a5 to your computer and use it in GitHub Desktop.
Save royteusink/4bb0cd3ae3542ea650283f7b5682d8a5 to your computer and use it in GitHub Desktop.
SilverStripe template include viewscope unqiue id

Installation

composer require ramsey/uuid

app/src/TemplateHelpers.php

<?php

namespace Heyday\TemplateHelpers;

use SilverStripe\View\TemplateGlobalProvider;
use SilverStripe\View\ArrayData;

class TemplateHelpers implements TemplateGlobalProvider
{
    public static function get_template_global_variables()
    {
        return [
            'ViewScope',
        ];
    }
    
    public static function ViewScope(): ArrayData
    {
        return ArrayData::create([
            'UniqueID' => \Ramsey\Uuid\Uuid::uuid4()->toString(),
        ]);
    }
}

Components/Fields/Checkbox.ss

<% with $ViewScope %>
    <div class="field min-w-5">
        <% if $Top.Value %>
            <input type="checkbox" name="$Top.Name" id="$UniqueID" value="$Top.Value" class="field-check" />
        <% else %>
            <input type="hidden" name="$Top.Name" value="0" />
            <input type="checkbox" name="$Top.Name" id="$UniqueID" value="1" class="field-check" />
        <% end_if %>
        <label for="$UniqueID" class="block pl-7 leading-6">
            $Top.Label
        </label>
    </div>
<% end_with %>

Page.ss

<% include Components/Fields/Checkbox Name='Something', Value='100', Label='Label value' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment