Skip to content

Instantiating a Snippet from a partial xml code #14

@ruralbrewer

Description

@ruralbrewer

Currently in order to create a Snippet from partial XML (i.e list items without the ul or ol wrappers) the TempladoSnippet can be used. However it requires that the user knows to wrap the partial in code in the Templado namespace:

<page:snippet xmlns:page="https://templado.io/snippets/1.0" xmlns="http://www.w3.org/1999/xhtml" id="whatever">
    ... partial code ...
</page:snippet>

A DOMNode must be created, the partial code must be wrapped, and the wrapped code then loaded into the DOMNode.

It would be a nice convenience if this process were moved under the hood. Perhaps a factory method could be added to the TempladoSnippet:

const TEMPLADO_NAMESPACE_OPENER =  '<page:snippet xmlns:page="https://templado.io/snippets/1.0" xmlns="http://www.w3.org/1999/xhtml" id="contentHeader">';
const TEMPLADO_NAMESPACE_CLOSER = '</page:snippet>';

public static function fromPartialCode(string $targetId, string $partialCodeString) 
{
    $partialBlock = new \DOMDocument();
    $wrappedPartial = self::TEMPLADO_NAMESPACE_OPENER . $partialCodeString .  self::TEMPLADO_NAMESPACE_CLOSER;
    $partialBlock->loadXML($wrappedPartial);
    return new self($targetId, $partialBlock);
}

Or perhaps this code could be added to the Snippet loader.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions