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

Simplify data return type and permit inheritance of data over layout and fetch #321

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public function __toString()
/**
* Assign or get template data.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider to change this documentation to:

Assign or merge with existing template data.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, should be even :
(Assign|merge with existing) or get template data.

?

* @param array $data
* @return mixed
* @return array
*/
public function data(array $data = null)
{
if (is_null($data)) {
return $this->data;
}

$this->data = array_merge($this->data, $data);
return $this->data = array_merge($this->data, $data);
}

/**
Expand Down Expand Up @@ -196,7 +196,7 @@ public function render(array $data = array())
public function layout($name, array $data = array())
{
$this->layoutName = $name;
$this->layoutData = $data;
$this->layoutData = array_merge($this->data, $data);
}

/**
Expand Down Expand Up @@ -311,9 +311,9 @@ public function section($name, $default = null)
* @param array $data
* @return string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$only is not documented. The name should is not yet optimal and clear to me.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the same name variable as twig - see https://twig.symfony.com/doc/3.x/tags/include.html#:~:text=only

Is it enough clear ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a second commit and rename it $useTemplateData

*/
public function fetch($name, array $data = array())
public function fetch($name, array $data = array(), bool $useTemplateData = false)
{
return $this->engine->render($name, $data);
return $this->engine->render($name, $useTemplateData ? array_merge($this->data, $data) : $data);
}

/**
Expand All @@ -322,9 +322,9 @@ public function fetch($name, array $data = array())
* @param array $data
* @return null
*/
public function insert($name, array $data = array())
public function insert($name, array $data = array(), bool $useTemplateData = false)
{
echo $this->engine->render($name, $data);
echo $this->fetch($name, $data, $useTemplateData);
}

/**
Expand Down