This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.0 » component and forward
icon5.gif  component and forward [message #98477] Tue, 04 May 2010 11:25 Go to next message
skywalker  is currently offline skywalker
Messages: 334
Registered: March 2008
Location: Norway
Faithful Member
Hello,

Just wonder if components has some sort of forward mechanism like action does?

The problem is that on one page I use a few components that display data from the database. But in some cases a component may return sfView::NONE. This will look like an empty space on the web page.

My idea was that instead of returning sfView::NONE, it used some kind of fallback component to generate something else instead of the empty space.


May the source be with you
Re: component and forward [message #98489 is a reply to message #98477 ] Tue, 04 May 2010 12:12 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Forward is an entirely different thing - it sends a redirect to the browser, which components cannot do, since they are not connected directly to the routing/request.

But if you want to grab a default component, you can instead of returning sfView::NONE call include_component() in your component's template. Or I think you can still use sfView::NONE and use the symfony API to render a component and return the result in the component action, though you'll have to consult the API on how to do that.


Remember Palestine
Re: component and forward [message #98490 is a reply to message #98477 ] Tue, 04 May 2010 12:15 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
Checking some of my 1.0 code, I think you could do:

sfLoader::loadHelpers('Partial');
$this->getResponse()->setContent(
  get_component('mod', 'action', array())
);
return sfView::NONE;


Remember Palestine
Re: component and forward [message #98491 is a reply to message #98490 ] Tue, 04 May 2010 12:21 Go to previous messageGo to next message
skywalker  is currently offline skywalker
Messages: 334
Registered: March 2008
Location: Norway
Faithful Member
Nice... this looks exactly what I was looking for Smile

I'll try it.
Thanx a lot.


May the source be with you
Re: component and forward [message #98540 is a reply to message #98490 ] Wed, 05 May 2010 09:35 Go to previous messageGo to next message
skywalker  is currently offline skywalker
Messages: 334
Registered: March 2008
Location: Norway
Faithful Member
halfer wrote on Tue, 04 May 2010 12:15

Checking some of my 1.0 code, I think you could do:

sfLoader::loadHelpers('Partial');
$this->getResponse()->setContent(
  get_component('mod', 'action', array())
);
return sfView::NONE;



I tried this, but it does not work as expected. The get_component do return the HTML code from the requested component, but it does not seem to be sent to the client.


May the source be with you
Re: component and forward [message #98547 is a reply to message #98477 ] Wed, 05 May 2010 13:17 Go to previous messageGo to next message
halfer  is currently offline halfer
Messages: 9535
Registered: January 2006
Location: West Midlands, UK
Faithful Member
setContent() works fine for me, in 1.0.x - I have several things relying on it. Make sure your layout contains the default 1.0 symfony code to echo it, of course.


Remember Palestine
Re: component and forward [message #98552 is a reply to message #98547 ] Wed, 05 May 2010 13:38 Go to previous messageGo to next message
skywalker  is currently offline skywalker
Messages: 334
Registered: March 2008
Location: Norway
Faithful Member
In action code the setContent do work fine for me to. But not in this case. Nothing is echoed. I have checked the class tree and it looks OK.

I also checked what get_component returned, and it returned the html code as expected.

What I have done is something like this:

class moduleOneComponents extends sfComponents
{
  public function executeSomeComponent()
  {
    $someObject = ObjectPeer::getAnObject();
    if (!$someObject) {
      sfLoader::loadHelpers('Partial');
      $html = get_component('moduleTwo', 'someOtherComponent', array());
      $this->getResponse()->setContent($html);
      return sfView::NONE;
    }
    // ... more code
  }
}

class moduleTwoComponents extends sfComponents
{
  public function extecuteSomeotherComponent()
  {
    $this->image = function_to_get_image_object();
  }
}

// moduleTwo/templates/_someotherComponent.php:
<img src="<?php echo url_for('@display_image?clean_name=' . $image->getCleanName()) ?>" />


If I examine $html in the first component, it contain the code shown in the template. But it the moduleOneComponent someComponent return nothing - it's empty.

BTW, the action for the display_image route also use the setContent, like this:

{
  $imageName = $this->getRequestParameter('clean_name');
  $image = ImagePeer::getImageByCleanName($imageName);
  
  if ($image) {
    $this->getResponse()->setHttpHeader('Content-type', $image->getMimeType());
    $this->getResponse()->setHttpHeader('Content-length', $image->getImageSize();
    $this->getResponse()->setContent(file_get_contents($image->getFilePath()));
  }

  return sfView::NONE;
}


and it work as expected.

[Updated on: Wed, 05 May 2010 13:42]


May the source be with you
Re: component and forward [message #98554 is a reply to message #98552 ] Wed, 05 May 2010 14:11 Go to previous message
skywalker  is currently offline skywalker
Messages: 334
Registered: March 2008
Location: Norway
Faithful Member
I did a small test:

class moduleOneComponents extends sfComponents
{
  public function executeSomeComponent()
  {
    $someObject = ObjectPeer::getAnObject();
    if (!$someObject) {
      sfLoader::loadHelpers('Partial');
      $this->html = get_component('moduleTwo', 'someOtherComponent', array());
      return sfView::SUCCESS;
    }
    // ... more code
  }
}

// in the template:
<?php if (isset($html)) : ?>
  <?php echo $sf_data->getRaw('html') ?>
<?php else : ?>
  // rest of the template
<?php endif; ?>


and it worked. But I would like to get the first version to work so that I don't have to modify the templates. I find it a bit more elegant Smile


May the source be with you
Previous Topic:Save the array alue
Next Topic:plugin disable enable per application
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software