Content (DTO)

When passing on data to your view and partials the data gain MaplePHP´s DTO traversal functionality.

Example

Set partial

Let´s say we pass on this data to a new template named family.

$this->provider->view()->setPartial("main.family", [
    "tagline" => "The Doe`s",
    "family" => [
        "x" => ["firstname" => "<em>john</em>", "lastname" => "doe"],
        "y" => ["firstname" => "<em>jane</em>", "lastname" => "doe"],
        "children" => [
            ["firstname" => "David", "lastname" => "Doe"],
            ["firstname" => "Sara", "lastname" => "Doe"]
        ]
    ]
]);

Create partial file

Let´s take a quick look at the advanced DTO traverse example bellow.

How does it work?

When you pass data to the template engine it will make it possible for you to easily traverse the data. you can then use one of the Handlers to modify the data when you have traversed to the right position.

If you compare the setPartial method example above with bellow you will see that you can very easily traverse the data with object and also format the strings.

Traversing the feed

You can even traverse arrays:

Handlers

You can access some Handler to make your life easier. There is different ways to access a DTO handler and I will start showing how you can access the Str handler in 2 ways.

Using the last traverse column argument

Using the the format method

DTO Handlers you can use:

  • Str: Modify/format strings

  • Num: Modify/format numbers

  • Arr: Modify/format arrays

  • DateTime: Access the PSR Clock and PHP DateTime class object

  • Encode: Encode string

  • Dom: Access MaplePHP Dom

Handler examples

Using a DTO library in PHP provides benefits such as encapsulating data, enforcing immutability, validating data, facilitating data transformation, maintaining API compatibility, reducing coupling, improving code readability.

Static access

You can also just access the handlers statically:

Example

Last updated

Was this helpful?