Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiating a Snippet from a partial xml code #14

Open
ruralbrewer opened this issue Jul 9, 2019 · 3 comments
Open

Instantiating a Snippet from a partial xml code #14

ruralbrewer opened this issue Jul 9, 2019 · 3 comments

Comments

@ruralbrewer
Copy link

ruralbrewer commented Jul 9, 2019

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.

@theseer
Copy link
Member

theseer commented Aug 29, 2019

While I like the idea, I'm not sure where this functionality belongs.

Adding it to the TempladoSnippet sounds like a good fit at first glance. But not having a general fromString feels incomplete. That though would lead to having a fromString method on TextSnippet and SimpleSnippet as well. That in turn would lead to duplicate code as at least TempladoSnippet and SimpleSnippet need to ensure the given string translates into valid (=parsable) XML.

Adding it to the SnippetLoader feels wrong as this is not actually "loading" anything, more like mapping.

So, does that mean we should add a SnippetMapper that can translate strings into their respective best matching type of snippet? Or more like explicit map calls?

Still pondering what is best here...

@ruralbrewer
Copy link
Author

What about the idea of creating a simple wrapper class (i.e. Templado\Engine\SnippetPartial) that is instantiated with a string and internally wraps it with the namespace. Then TempladoSnippet takes that as a dependency.

@theseer
Copy link
Member

theseer commented Aug 11, 2023

The concept of "snippets" got removed from Templado 5. Everything is a document now.

That doesn't exactly solve the problem of this issue though. Still pondering whether we should have a wrapper here..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants