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

Already on GitHub? Sign in to your account

Added: View::startIfEmpty($name) #930

Merged
merged 3 commits into from Dec 1, 2012

Conversation

Projects
None yet
5 participants
Contributor

LiquidityC commented Oct 30, 2012

Ticket: 3315

Added function View::startIfEmpty($name) that captures content to a block only if it has no existing content.

Contributor

LiquidityC commented Oct 31, 2012

Removed the line from .gitignore

@ADmad ADmad commented on an outdated diff Nov 1, 2012

lib/Cake/View/ViewBlock.php
@@ -69,12 +78,38 @@ public function start($name) {
}
/**
+ * Start capturing output for a 'block' if it is empty
+ *
+ * Blocks allow you to create slots or blocks of dynamic content in the layout.
+ * view files can implement some or all of a layout's slots.
+ *
+ * You can end capturing blocks using View::end(). Blocks can be output
+ * using View::get();
+ *
+ * @param string $name The name of the block to capture for.
+ * @return void
+ * @see ViewBlock::startIfEmpty()
@ADmad

ADmad Nov 1, 2012

Member

This line needs to be removed. A @see to itself doesn't make sense.

Contributor

LiquidityC commented Nov 2, 2012

I dropped that comment line now.

Contributor

bar commented Nov 3, 2012

Just to be able to track the whole PR flow, from: #925 and #929

Owner

markstory commented Nov 14, 2012

Anyone have objections to this? I think it provides a useful enhancement to the block system that better allows default content alongside the existing append behavior.

Contributor

bar commented Nov 14, 2012

👍

Owner

lorenzo commented Nov 14, 2012

I still don't find any good use for this, but I'm going with you guys 👍

Member

ADmad commented Nov 14, 2012

Yeah this new method doesn't make much sense to me either. Let hope someone doesn't call $this->startIfEmpty('foo'), echo some content and finally call $this->end() and then wonder why he/she got an empty block.

Owner

markstory commented Nov 14, 2012

@ADmad @lorenzo The primary use case would be when:

  • You have blocks in a layout that has a parent.
  • You want to define default content for blocks in the extended layout.
  • You want to be able to define content for those same blocks in a view, but not get the current appending behavior that blocks have.

In this situation, you could use startIfEmpty() to define the fallback content in the parent view or child layout. This allows your code to stay dry as you don't need to duplicate the block content through the various child views.

Owner

lorenzo commented Nov 14, 2012

Can you safely call end() after doing startIfEmpty() ? Will we need endIfStarted() ?

Contributor

LiquidityC commented Nov 14, 2012

You can safely call end(). If content exists when this new method is called
we just store it and dump it when end is called. If there is no content in
the block it acts exactly as start() does. start() is actually called in
the latter case.
On 14 Nov 2012 19:01, "José Lorenzo Rodríguez" notifications@github.com
wrote:

Can you safely call end() after doing startIfEmpty() ? Will we need
endIfStarted() ?


Reply to this email directly or view it on GitHubhttps://github.com/cakephp/cakephp/pull/930#issuecomment-10376527.

Member

ADmad commented Nov 14, 2012

$this->startIfEmpty('test');
echo "Block content 1";
$this->end();
echo $this->fetch('test');

The test block fetched in this case would be empty isn't it?

Member

ADmad commented Nov 14, 2012

Nvm, the block won't be empty in above code i showed, i misread.

@ADmad ADmad commented on the diff Nov 14, 2012

lib/Cake/Test/Case/View/ViewTest.php
+ * Test block with startIfEmpty
+ *
+ * @return void
+ */
+ public function testBlockCaptureStartIfEmpty() {
+ $this->View->startIfEmpty('test');
+ echo "Block content 1";
+ $this->View->end();
+
+ $this->View->startIfEmpty('test');
+ echo "Block content 2";
+ $this->View->end();
+
+ $result = $this->View->fetch('test');
+ $this->assertEquals('Block content 1', $result);
+ }
@ADmad

ADmad Nov 14, 2012

Member

Perhaps you should add 1 more test where you first use start() for a new block and then startIfEmpty().

Contributor

LiquidityC commented Nov 18, 2012

@ADmad I did as you suggested.

Owner

lorenzo commented Dec 1, 2012

Votes for/against this?

lorenzo added a commit that referenced this pull request Dec 1, 2012

@lorenzo lorenzo merged commit 99e84b8 into cakephp:2.3 Dec 1, 2012

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