Page 1 of 1

Returning multiple content from a module

Posted: Sun May 29, 2011 1:16 pm
by brippon
This is a problem I've faced time and time again over years...

I'd like my module to return more than one content block, e.g. the main content and a sidebar. So a product page might have the product description in one div, and links to similar products in another.

I could have my module's template return both items together, but then I'm coding my overall page layout into the module template.

I could call my module twice on the page - {mymodule action='sidebar'} ... {mymodule action='main_content'}, but it's cumbersome, and in any case doesn't work for a page invoked using CreateLink...

What I think I used to do was put the sidebar into a Smarty variable:

Code: Select all

$this->smarty->assign('sidebar', $this->ProcessTemplate('sidebar.tpl'));
then something in my page template like:

Code: Select all

{if isset($sidebar)}{$sidebar}{else}{content block='Sidebar'}{/if}
Not nice, but it worked (did it?), but not I suspect since 1.9.

Can I still access my Smarty variables in a template? Is there a better way of doing all this?

As an aside, I sometimes want a page (linked from a CreateLink, or called via a URL defined by RegisterRoute) to be rendered in a specific page template. For example, I might want a Products page to link to ProductDetails pages, with the latter then rendered using the ProductDetails template. I think I have to set the returnid to the id of a (dummy) page which uses the required template. Is this right, and if so how do I work out what it is? Or is there a better way?

Thanks to anyone who understands my issues and can shed any light.

Brian

Re: Returning multiple content from a module

Posted: Sun May 29, 2011 7:01 pm
by brippon
OK I think I've figured it with the help of calguy's description at
viewtopic.php?t=42898#p203281

If I put

Code: Select all

{content assign=content_block}
at the very top of the template used to render my module's output, and

Code: Select all

{$content_block}
at the point I want the main body of my output to appear (replacing the original {content}), in my module I can say

Code: Select all

$this->smarty->assign('sidebar', "This is my new sidebar");
and anywhere in the template I can put

Code: Select all

{if isset($sidebar)}{$sidebar}{else}{content block='Sidebar'}{/if}
If I use the template to create a nomal page I can set the sidebar by hand, but if it's used to render output from my module, I can specify what goes there. In the same way, the module can specify what to use for the page title, the meta tags, or anything else outside the main page content.

I think!

Any observations or suggestions for better ways to achieve this welcome.

I'd still like to know how to set a specific returnid for a page (i.e. I want to use a different template for a page for which I create the link than that used for the referring page). I this this is the same question posed by mydom in
viewtopic.php?f=6&t=53765

I'm looking for a function called something like GetIdFromAlias, I suppose. The News module allows you to select a detail page from the back end - I'll have to look at this.

Brian

Re: Returning multiple content from a module

Posted: Sun May 29, 2011 7:34 pm
by Jos
In the announcement of CMSms version 1.8 I read:
http://forum.cmsmadesimple.org/viewtopi ... =1&t=45360
New {content_module} tag
There's a new {content_module} tag that allows modules to provide different types of content blocks for use in Content pages. This is a huge feature, it means that a module developer could write a specific type of content block to provide specific type of content, and that content is stored with the page, not with the module (so copying the page will still work etc). Currently the CGContentUtils and Uploads modules support the {content_module} tag, but soon enough more modules will support it.
I didn't get into this, but seems like this is what you are after?
brippon wrote:I'd still like to know how to set a specific returnid for a page (i.e. I want to use a different template for a page for which I create the link than that used for the referring page). [...] The News module allows you to select a detail page from the back end - I'll have to look at this.
The News module is indeed a good example. Check the first lines of the action.default.php file

Re: Returning multiple content from a module

Posted: Thu Jun 16, 2011 11:30 am
by psy
Does my post http://forum.cmsmadesimple.org/viewtopi ... =4&t=54717 about the Smarty replace modifier help?

In the sidebar, you could capture the tag and replace the returnid with the one you want.

psy

Re: Returning multiple content from a module

Posted: Thu Jun 16, 2011 4:56 pm
by brippon
Thanks psy, I managed to work out how to get the returnid.

However your smarty replace tip is very useful - there will be times when that will solve a problem.

Cheers
Brian